GraphicToggleIsOnTransition.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ImageToggleIsOnTransition.cs" company="Exit Games GmbH">
  3. // </copyright>
  4. // <summary>
  5. // Use this on Toggle graphics to have some color transition as well without corrupting toggle's behaviour.
  6. // </summary>
  7. // <author>developer@exitgames.com</author>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using UnityEngine;
  10. using UnityEngine.EventSystems;
  11. using UnityEngine.UI;
  12. namespace Photon.Pun.UtilityScripts
  13. {
  14. /// <summary>
  15. /// Use this on toggles texts to have some color transition on the text depending on the isOn State.
  16. /// </summary>
  17. [RequireComponent(typeof(Graphic))]
  18. public class GraphicToggleIsOnTransition : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  19. {
  20. public Toggle toggle;
  21. private Graphic _graphic;
  22. public Color NormalOnColor = Color.white;
  23. public Color NormalOffColor = Color.black;
  24. public Color HoverOnColor = Color.black;
  25. public Color HoverOffColor = Color.black;
  26. private bool isHover;
  27. public void OnPointerEnter(PointerEventData eventData)
  28. {
  29. this.isHover = true;
  30. this._graphic.color = this.toggle.isOn ? this.HoverOnColor : this.HoverOffColor;
  31. }
  32. public void OnPointerExit(PointerEventData eventData)
  33. {
  34. this.isHover = false;
  35. this._graphic.color = this.toggle.isOn ? this.NormalOnColor : this.NormalOffColor;
  36. }
  37. public void OnEnable()
  38. {
  39. this._graphic = this.GetComponent<Graphic>();
  40. this.OnValueChanged(this.toggle.isOn);
  41. this.toggle.onValueChanged.AddListener(this.OnValueChanged);
  42. }
  43. public void OnDisable()
  44. {
  45. this.toggle.onValueChanged.RemoveListener(this.OnValueChanged);
  46. }
  47. public void OnValueChanged(bool isOn)
  48. {
  49. this._graphic.color = isOn ? (this.isHover ? this.HoverOnColor : this.HoverOnColor) : (this.isHover ? this.NormalOffColor : this.NormalOffColor);
  50. }
  51. }
  52. }