首页 > 代码库 > Unity3d运行时动态修改材质

Unity3d运行时动态修改材质

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;

		} 
	}

Unity3d运行时动态修改材质