如何创建将着色器自动应用于模型中对象的材质的代码
本文关键字:模型 应用于 对象 代码 何创建 创建 | 更新日期: 2023-09-27 17:58:36
我在c#中有一个代码,它读取标记对象的材质,并根据我想要使用的着色器对其进行渲染。我想知道是否有更简单的方法。在一个模型中,有很多对象都有很多材质。创建这么多脚本,然后在uniy3D上运行它们是非常耗时的。我想创建一个脚本,自动查找模型中对象的材质并将着色器应用于它们。
将其放在父GameObject上,它会在游戏对象的子对象中找到所有Renderer
,然后指定着色器。
using UnityEngine;
using System.Collections;
public class AssignShadersToChildren : MonoBehaviour
{
public Shader shader; // This should hold the Shader you want to use
void Start()
{
// We create a list that will hold all the renderers so we can then assign the shader
List<Renderer> renderers = new List<Renderer>();
renderers = GetComponentsInChildren<Renderer>();
// For every Renderer in the list assign the materials shader to the shader
foreach (Renderer r in renderers) {
r.material.shader = shader;
}
}
}
注意:这是在没有编译器的情况下编写的,因此有可能无法运行。
如果这回答了你的问题,一定要接受这个答案。