https://blog.uwa4d.com/archives/1983.html
https://docs.unity3d.com/ScriptReference/MaterialPropertyBlock.html
https://blog.csdn.net/liweizhao/article/details/81937590
Description
a block of material values to apply.
MaterialPropertyBlock is used by Graphics.DrawMesh and Renderer.SetPropertyBlock. use it in situations where u want to draw multiple objects with the same material, but slightly different properties. for example, if u want to slightly change the color of each mesh drawn. changing the render state is not supported.
unity’s terrain engine uses MaterialPropertyBlock to draw trees; all of them use the same material, but each tree has different color, scale & wind factor.
The block passed to Graphics.DrawMesh or Renderer.SetPropertyBlock is copied, so the most efficient way of using it is to create one block and reuse it for all DrawMesh calls. Use SetFloat, SetVector, SetColor, SetMatrix, SetTexture, SetBuffer to add or replace values.
以下的例子,来自:https://blog.uwa4d.com/archives/1983.html
测试代码:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
public class TestMaterialPropertyBlock : MonoBehaviour
{
MaterialPropertyBlock prop = null;
// Use this for initialization
public int objCount = 100;
private int colorID;
GameObject[] listObj = null;
GameObject[] listProp = null;
void Start()
{
colorID = Shader.PropertyToID("_Color");
prop = new MaterialPropertyBlock();
var obj = Resources.Load("Perfabs/Sphere") as GameObject;
listObj = new GameObject[objCount];
listProp = new GameObject[objCount];
for (int i = 0; i < objCount; ++i)
{
int x = Random.Range(-6, -2);
int y = Random.Range(-4, 4);
int z = Random.Range(-4, 4);
GameObject o = Instantiate(obj);
o.name = i.ToString();
o.transform.localPosition = new Vector3(x, y, z);
listObj[i] = o;
}
for (int i = 0; i < objCount; ++i)
{
int x = Random.Range(2, 6);
int y = Random.Range(-4, 4);
int z = Random.Range(-4, 4);
GameObject o = Instantiate(obj);
o.name = (objCount + i).ToString();
o.transform.localPosition = new Vector3(x, y, z);
listProp[i] = o;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < objCount; ++i)
{
float r = Random.Range(0, 1f);
float g = Random.Range(0, 1f);
float b = Random.Range(0, 1f);
listObj[i].GetComponent<Renderer>().material.SetColor("_Color", new Color(r, g, b, 1));
}
sw.Stop();
UnityEngine.Debug.Log(string.Format("material total: {0:F4} ms", (float)sw.ElapsedTicks * 1000 / Stopwatch.Frequency));
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < objCount; ++i)
{
float r = Random.Range(0, 1f);
float g = Random.Range(0, 1f);
float b = Random.Range(0, 1f);
listProp[i].GetComponent<Renderer>().GetPropertyBlock(prop);
prop.SetColor(colorID, new Color(r, g, b, 1));
listProp[i].GetComponent<Renderer>().SetPropertyBlock(prop);
}
sw.Stop();
UnityEngine.Debug.Log(string.Format("MaterialPropertyBlock total: {0:F4} ms", (float)sw.ElapsedTicks * 1000 / Stopwatch.Frequency));
}
}
}
注意点:
1、一个地方可优化,就是GetComponent,但是两者对比都是同样的,所以相对公平。
2、如何查看profile作对比:
上图是,使用Material,SetColor的方法,可见,场景内存增加了5个额外的材质实例。
这张图,则是使用第二种方式,MaterialPropertyBlock的方式,设置颜色。不会产生额外的材质实例。
3、性能对比的时间,使用了新的类型Stopwatch方法。