Unity MaterialPropertyBlock 使用方法

亲测可用!

  • ​​​​​​推荐做法。使用 MaterialPropertyBlock 技术进行颜色修改,可以做到多模型批处理。首先代码如下,其次,需要把模型shader,设置为可实例化shader,shader中加入代码  #pragma multi_compile_instancing
MaterialPropertyBlock propertyBlock;
public MeshRenderer[] arr_mesh;
void Awake()
{
     if (propertyBlock == null)
        propertyBlock = new MaterialPropertyBlock();
}
void OnEnable()
{
   for (int i = 0; i < arr_mesh.Length; i++)//给每个mesh设置不同颜色
    {
      propertyBlock.SetColor("_Color", Color.HSVToRGB(Random.value, 1, .9f));
      arr_mesh[i].SetPropertyBlock(propertyBlock);
    }
}

shader加入可实例化之后,如下图,勾选Enable GPU Instancing

 

  •  错误做法。 在开发中,会遇到多个相同模型,不同颜色的需求,如果直接使用material.color 去设置颜色,每次设置一个颜色,都会实例化一个材质实例,如下:
var _meshRender = GetComponent<MeshRenderer>();
_meshRender.material.color = new Color(1, 0, 1);

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值