TextButtonTransition.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="TextButtonTransition.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.Chat.UtilityScripts
  13. {
  14. /// <summary>
  15. /// Use this on Button texts to have some color transition on the text as well without corrupting button's behaviour.
  16. /// </summary>
  17. [RequireComponent(typeof(Text))]
  18. public class TextButtonTransition : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  19. {
  20. Text _text;
  21. /// <summary>
  22. /// The selectable Component.
  23. /// </summary>
  24. public Selectable Selectable;
  25. /// <summary>
  26. /// The color of the normal of the transition state.
  27. /// </summary>
  28. public Color NormalColor= Color.white;
  29. /// <summary>
  30. /// The color of the hover of the transition state.
  31. /// </summary>
  32. public Color HoverColor = Color.black;
  33. public void Awake()
  34. {
  35. _text = GetComponent<Text>();
  36. }
  37. public void OnEnable()
  38. {
  39. _text.color = NormalColor;
  40. }
  41. public void OnDisable()
  42. {
  43. _text.color = NormalColor;
  44. }
  45. public void OnPointerEnter(PointerEventData eventData)
  46. {
  47. if (Selectable == null || Selectable.IsInteractable()) {
  48. _text.color = HoverColor;
  49. }
  50. }
  51. public void OnPointerExit(PointerEventData eventData)
  52. {
  53. if (Selectable == null || Selectable.IsInteractable()) {
  54. _text.color = NormalColor;
  55. }
  56. }
  57. }
  58. }