public static MaterialSource GetMaterialSource(object src) { if (src is Renderer) { return(RendererMaterialSource.GetMaterialSource(src as Renderer)); } else if (src is UnityEngine.UI.Graphic) { return(GraphicMaterialSource.GetMaterialSource(src as UnityEngine.UI.Graphic)); } var go = com.spacepuppy.Utils.GameObjectUtil.GetGameObjectFromSource(src); var rend = go.GetComponent <Renderer>(); if (rend != null) { return(RendererMaterialSource.GetMaterialSource(rend)); } var graph = go.GetComponent <UnityEngine.UI.Graphic>(); if (graph != null) { return(GraphicMaterialSource.GetMaterialSource(graph)); } return(null); }
public static Material GetUniqueMaterial(this Renderer renderer, bool forceUnique = false) { if (renderer == null) { throw new System.ArgumentNullException("renderer"); } var source = RendererMaterialSource.GetMaterialSource(renderer); if (!source.IsUnique || forceUnique) { return(source.GetUniqueMaterial()); } else { return(source.Material); } }
/// <summary> /// Reduces obj to source type, and returns a copy of the material on it. /// Works like Renderer.material, but also for UI.Graphics. /// </summary> /// <param name="obj"></param> /// <param name="reduceFromGameObjectSource"></param> /// <returns></returns> public static Material CopyMaterialFromSource(object obj, bool reduceFromGameObjectSource = false) { if (obj is Renderer) { return((obj as Renderer).material); } else if (obj is UnityEngine.UI.Graphic) { var graph = obj as UnityEngine.UI.Graphic; var source = RendererMaterialSource.GetMaterialSource(graph); return(source.GetUniqueMaterial()); } if (reduceFromGameObjectSource) { var go = GameObjectUtil.GetGameObjectFromSource(obj); if (go == null) { return(null); } var rend = go.GetComponent <Renderer>(); if (rend != null) { return(rend.material); } var graph = go.GetComponent <UnityEngine.UI.Graphic>(); if (graph != null) { var source = RendererMaterialSource.GetMaterialSource(graph); return(source.GetUniqueMaterial()); } } return(null); }