using UnityEngine; using UnityEngine.UI; namespace Plugins.Animate_UI_Materials { /// /// Simply replaces a Graphic component's material /// Does not create or modify materials /// Useful for debugging, or switching the material of a Graphic that does not offer this option in some context /// [ExecuteAlways] [AddComponentMenu("UI/Animate UI Material/GraphicMaterialReplacer")] public class GraphicMaterialReplacer : MonoBehaviour, IMaterialModifier { [SerializeField] Material material; public Material Material { get => material; set { material = value; SetMaterialDirty(); } } /// /// Request Graphic to regenerate materials /// void SetMaterialDirty() { if (TryGetComponent(out Graphic graphic)) { graphic.SetMaterialDirty(); } } // On enable and disable, update the target graphic void OnEnable() => SetMaterialDirty(); void OnDisable() => SetMaterialDirty(); /// /// From IMaterialModifier /// Receives the current material before display, and returns another material if enabled /// Here, simply sends the "material" field if enabled /// /// /// A new material object public Material GetModifiedMaterial(Material baseMaterial) { // Return the base material if invalid or if this component is disabled if (!enabled || baseMaterial == null) return baseMaterial; return material; } } }