public static Shader Find (string name);

Description

查找具有给定 name 的着色器。

可使用 Shader.Find 切换到其他着色器,无需保持对着色器的引用。 name 是可以在任何材质的着色器弹出窗口中看到的名称,例如“Standard”、“Unlit/Texture”、“Legacy Shaders/Diffuse”等。

注意,如果着色器没有引用方,着色器可能未包含在播放器版本中!这种情况下,Shader.Find 仅在 编辑器中起作用,在播放器版本中将生成粉红色“缺失着色器”材质。因此,建议使用 着色器引用,而不是按名称查找它们。要确保着色器包含在游戏版本中,则执行以下操作之一: 1) reference it from some of the materials used in your Scene, 2) add it under "Always Included Shaders" list in ProjectSettings/Graphics or 3) put shader or something that references it (e.g. a Material) into a "Resources" folder.

另请参阅:Material 类。

using UnityEngine;

public class Example : MonoBehaviour { // Create a material from code void Start() { // Create a material with transparent diffuse shader Material material = new Material(Shader.Find("Transparent/Diffuse")); material.color = Color.green;

// assign the material to the renderer GetComponent<Renderer>().material = material; } }