TextToggleIsOnTransition.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="TextToggleIsOnTransition.cs" company="Exit Games GmbH">
  3. // </copyright>
  4. // <summary>
  5. // Use this on Button texts to have some color transition on the text as well without corrupting button'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(Text))]
  18. public class TextToggleIsOnTransition : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  19. {
  20. /// <summary>
  21. /// The toggle Component.
  22. /// </summary>
  23. public Toggle toggle;
  24. Text _text;
  25. /// <summary>
  26. /// The color of the normal on transition state.
  27. /// </summary>
  28. public Color NormalOnColor= Color.white;
  29. /// <summary>
  30. /// The color of the normal off transition state.
  31. /// </summary>
  32. public Color NormalOffColor = Color.black;
  33. /// <summary>
  34. /// The color of the hover on transition state.
  35. /// </summary>
  36. public Color HoverOnColor= Color.black;
  37. /// <summary>
  38. /// The color of the hover off transition state.
  39. /// </summary>
  40. public Color HoverOffColor = Color.black;
  41. bool isHover;
  42. public void OnEnable()
  43. {
  44. _text = GetComponent<Text>();
  45. OnValueChanged (toggle.isOn);
  46. toggle.onValueChanged.AddListener(OnValueChanged);
  47. }
  48. public void OnDisable()
  49. {
  50. toggle.onValueChanged.RemoveListener(OnValueChanged);
  51. }
  52. public void OnValueChanged(bool isOn)
  53. {
  54. _text.color = isOn? (isHover?HoverOnColor:HoverOnColor) : (isHover?NormalOffColor:NormalOffColor) ;
  55. }
  56. public void OnPointerEnter(PointerEventData eventData)
  57. {
  58. isHover = true;
  59. _text.color = toggle.isOn?HoverOnColor:HoverOffColor;
  60. }
  61. public void OnPointerExit(PointerEventData eventData)
  62. {
  63. isHover = false;
  64. _text.color = toggle.isOn?NormalOnColor:NormalOffColor;
  65. }
  66. }
  67. }