UISyncEffect.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Coffee.UIEffects
  6. {
  7. /// <summary>
  8. /// Dissolve effect for uGUI.
  9. /// </summary>
  10. [ExecuteInEditMode]
  11. public class UISyncEffect : BaseMaterialEffect
  12. {
  13. [Tooltip("The target effect to synchronize.")] [SerializeField]
  14. private BaseMeshEffect m_TargetEffect;
  15. public BaseMeshEffect targetEffect
  16. {
  17. get { return m_TargetEffect != this ? m_TargetEffect : null; }
  18. set
  19. {
  20. if (m_TargetEffect == value) return;
  21. m_TargetEffect = value;
  22. SetVerticesDirty();
  23. SetMaterialDirty();
  24. SetEffectParamsDirty();
  25. }
  26. }
  27. protected override void OnEnable()
  28. {
  29. if (targetEffect)
  30. targetEffect.syncEffects.Add(this);
  31. base.OnEnable();
  32. }
  33. protected override void OnDisable()
  34. {
  35. if (targetEffect)
  36. targetEffect.syncEffects.Remove(this);
  37. base.OnDisable();
  38. }
  39. public override Hash128 GetMaterialHash(Material baseMaterial)
  40. {
  41. if (!isActiveAndEnabled) return k_InvalidHash;
  42. var matEffect = targetEffect as BaseMaterialEffect;
  43. if (!matEffect || !matEffect.isActiveAndEnabled) return k_InvalidHash;
  44. return matEffect.GetMaterialHash(baseMaterial);
  45. }
  46. public override void ModifyMaterial(Material newMaterial, Graphic graphic)
  47. {
  48. if (!isActiveAndEnabled) return;
  49. var matEffect = targetEffect as BaseMaterialEffect;
  50. if (!matEffect || !matEffect.isActiveAndEnabled) return;
  51. matEffect.ModifyMaterial(newMaterial, graphic);
  52. }
  53. public override void ModifyMesh(VertexHelper vh, Graphic graphic)
  54. {
  55. if (!isActiveAndEnabled) return;
  56. if (!targetEffect || !targetEffect.isActiveAndEnabled) return;
  57. targetEffect.ModifyMesh(vh, graphic);
  58. }
  59. #if UNITY_EDITOR
  60. protected override void OnValidate()
  61. {
  62. SetVerticesDirty();
  63. SetMaterialDirty();
  64. SetEffectParamsDirty();
  65. }
  66. #endif
  67. }
  68. }