1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEditor; 5 6 [ExecuteInEditMode] 7 public class SetMaterial : MonoBehaviour { 8 9 #if UNITY_EDITOR 10 [ContextMenu("SetMaterial")] 11 void SetObjsMaterial() 12 { 13 MeshRenderer[] renders = gameObject.GetComponentsInChildren<MeshRenderer>(); 14 15 Material resMat = Resources.Load<Material>("Default_Materiall"); 16 17 foreach (MeshRenderer render in renders) 18 { 19 render.material = resMat; 20 } 21 } 22 23 private void OnGUI() 24 { 25 MeshRenderer[] renders = gameObject.GetComponentsInChildren<MeshRenderer>(); 26 27 Material resMat = Resources.Load<Material>("Default_Materiall"); 28 29 foreach (MeshRenderer render in renders) 30 { 31 render.material = resMat; 32 } 33 } 34 #endif 35 }
说明:
[ExecuteInEditMode] : 表示在编辑器模式下执行OnGUI和Update
[ContextMenu("SetMaterial")] : 在编辑器下手动执行自定义方法