Unity3d运行时动态修改材质

时间:2023-03-08 16:46:08
void Start () {
const string MainTexVariableName = "_MainTex";
var renders = gameObject.GetComponentsInChildren<Renderer>(); var resMat = Resources.Load<Material>("CharacterThrough"); foreach (var render in renders)
{
var oreginalMaterial = render.sharedMaterial;
var currentRender = render; var newMat = new Material(resMat); newMat.SetTexture(MainTexVariableName, oreginalMaterial.GetTexture(MainTexVariableName)); currentRender.material = newMat; }
}