lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

参考链接:

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 }

 

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

posted on 2019-04-28 21:54  艰苦奋斗中  阅读(3635)  评论(0编辑  收藏  举报