zoukankan      html  css  js  c++  java
  • [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 }

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

  • 相关阅读:
    SpringBoot入门
    Java自定义注解(1)
    git集成idea
    git常用命令
    Shiro授权
    shiro认证
    shiro入门
    SpringMVC文件上传
    SpringMVC入门
    mybatis关联关系映射
  • 原文地址:https://www.cnblogs.com/lyh916/p/10787148.html
Copyright © 2011-2022 走看看