[Unity优化]批处理04:MaterialPropertyBlock


参考链接:

https://blog.csdn.net/liweizhao/article/details/81937590

 

1.在场景中放一些Cube,赋予一个新材质,使用内置shader(Unlit/Color),如下图,可以看出动态批处理生效了

 

2.挂上下面的脚本

 1 using System.Collections.Generic;
 2 using UnityEngine;
 3 
 4 public class NewBehaviourScript : MonoBehaviour
 5 {
 6     public List<MeshRenderer> list;
 7 
 8     void Start()
 9     {
10         for (int i = 0; i < list.Count; i++)
11         {
12             list[i].material.color = Color.white;
13         }
14     }
15 }

 

运行后,会发现动态批处理不生效了,因为当修改材质时,unity会生成一份材质实例,从而做到不同对象身上的材质互不影响。如下,每个Cube都有各自的材质实例

 

3.修改一下脚本

 1 using System.Collections.Generic;
 2 using UnityEngine;
 3 
 4 public class NewBehaviourScript : MonoBehaviour
 5 {
 6     public List<MeshRenderer> list;
 7 
 8     void Start()
 9     {
10         MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
11         for (int i = 0; i < list.Count; i++)
12         {
13             materialPropertyBlock.SetColor("_Color", Color.white);
14             list[i].SetPropertyBlock(materialPropertyBlock);
15         }
16     }
17 }

 

运行后,动态批处理生效了,也没有生成新的材质实例


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM