Version: Unity 6.2 Alpha (6000.2)
Language : English
Example of a shader that supports GPU Instancing in the Built-In Render Pipeline
Prevent Unity stripping GPU instancing shaders in the Built-In Render Pipeline

Example of changing per-instance data at runtime in the Built-In Render Pipeline

The following example demonstrates how to use MaterialPropertyBlock objects to set per-instance data for a group of GameObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
at runtime. It sets the _Color property from the above shaderA program that runs on the GPU. More info
See in Glossary
examples to a random color.

Important: MaterialPropertyBlocks break SRP Batcher compatibility. For more information, see GPU instancing: Requirements and Compatibility.

using UnityEngine;

public class MaterialPropertyBlockExample : MonoBehaviour
{
public GameObject[] objects;

void Start()
{
MaterialPropertyBlock props = new MaterialPropertyBlock();
MeshRenderer renderer;

foreach (GameObject obj in objects)
{
float r = Random.Range(0.0f, 1.0f);
float g = Random.Range(0.0f, 1.0f);
float b = Random.Range(0.0f, 1.0f);
props.SetColor("_Color", new Color(r, g, b));

renderer = obj.GetComponent<MeshRenderer>();
renderer.SetPropertyBlock(props);
}
}
}

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答
Example of a shader that supports GPU Instancing in the Built-In Render Pipeline
Prevent Unity stripping GPU instancing shaders in the Built-In Render Pipeline
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961