FlashGraphic.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. namespace SRF.UI
  2. {
  3. using Internal;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// Instantly sets colour to FlashColor on pointer down, then fades back to DefaultColour once pointer is released.
  9. /// </summary>
  10. [AddComponentMenu(ComponentMenuPaths.FlashGraphic)]
  11. [ExecuteInEditMode]
  12. public class FlashGraphic : UIBehaviour, IPointerDownHandler, IPointerUpHandler
  13. {
  14. public float DecayTime = 0.15f;
  15. public Color DefaultColor = new Color(1, 1, 1, 0);
  16. public Color FlashColor = Color.white;
  17. public Graphic Target;
  18. private bool _isHoldingUntilNextPress;
  19. public void OnPointerDown(PointerEventData eventData)
  20. {
  21. Target.CrossFadeColor(FlashColor, 0f, true, true);
  22. _isHoldingUntilNextPress = false;
  23. }
  24. public void OnPointerUp(PointerEventData eventData)
  25. {
  26. if (!_isHoldingUntilNextPress)
  27. {
  28. Target.CrossFadeColor(DefaultColor, DecayTime, true, true);
  29. }
  30. }
  31. protected override void OnEnable()
  32. {
  33. base.OnEnable();
  34. if (!_isHoldingUntilNextPress)
  35. {
  36. Target.CrossFadeColor(DefaultColor, 0f, true, true);
  37. }
  38. }
  39. #if UNITY_EDITOR
  40. protected void Update()
  41. {
  42. if (!Application.isPlaying && Target != null)
  43. {
  44. Target.CrossFadeColor(DefaultColor, 0, true, true);
  45. }
  46. }
  47. #endif
  48. public void Flash()
  49. {
  50. Target.CrossFadeColor(FlashColor, 0f, true, true);
  51. Target.CrossFadeColor(DefaultColor, DecayTime, true, true);
  52. _isHoldingUntilNextPress = false;
  53. }
  54. public void FlashAndHoldUntilNextPress()
  55. {
  56. Target.CrossFadeColor(FlashColor, 0f, true, true);
  57. _isHoldingUntilNextPress = true;
  58. }
  59. }
  60. }