为什么当我访问渲染器时批次计数会增加?

Why does the batches count increase when I access the renderer?

我的场景中有 81 个游戏对象。当我玩 3 批 运行 时,统计数据是健康的。但是当我在脚本中访问渲染器时,每个对象的批次计数都会增加。在我访问所有对象的渲染器后,情况变得糟糕。

Before: Batches: 3 | Saved by batching: 80 | FPS: Nearly 400

After: Batches: 83 | Saved by batching: 0 | FPS: about 200

代码:transform.GetComponent<Renderer>().material.color = Color32.Lerp(start_color, target_color,t);

我觉得这个问题很有道理。 "Why did Unity3D did not cost an object a batch before its renderer is accessed? Why is that batch count still active even after I did my job with the game object?"


更新: 1. 即使我只是访问渲染器而不更改它,它也会花费一个批次并且永远不会减少。

start_color = transform.GetComponent<Renderer>().material.color;
  1. 我通过 lerp 改变颜色来缩放对象。在检查器中,material 被重命名为 mat(instance).

  2. 我尝试使用 sharedMaterial。现在批次计数稳定为 3,但所有 81 个对象都变为相同的颜色。 故事: 81 可以更新为具有不同灰度的低、中、高状态。我需要在更新时将它们分类。

每次更改 material 的任何属性时,都会创建 material 的实例。这就是为什么您的对象没有通过批处理保存的原因。

@VladislavKurenkov 是对的。还有另一个技巧,但它不会在所有情况下都有效 - 而不是通过 GetComponent() 引用 material。material 只需制作一个 public Material 类型的变量。然后修改该变量上的任何内容,所有转换都会受到影响。只需将 material 从您的资产中输入检查器槽即可。这将节省批次。