UIButtonMessage.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Button Message (Legacy)")]
  4. public class UIButtonMessage : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this.mStarted = true;
  9. }
  10. private void OnEnable()
  11. {
  12. if (this.mStarted)
  13. {
  14. this.OnHover(UICamera.IsHighlighted(base.gameObject));
  15. }
  16. }
  17. private void OnHover(bool isOver)
  18. {
  19. if (base.enabled && ((isOver && this.trigger == UIButtonMessage.Trigger.OnMouseOver) || (!isOver && this.trigger == UIButtonMessage.Trigger.OnMouseOut)))
  20. {
  21. this.Send();
  22. }
  23. }
  24. private void OnPress(bool isPressed)
  25. {
  26. if (base.enabled && ((isPressed && this.trigger == UIButtonMessage.Trigger.OnPress) || (!isPressed && this.trigger == UIButtonMessage.Trigger.OnRelease)))
  27. {
  28. this.Send();
  29. }
  30. }
  31. private void OnSelect(bool isSelected)
  32. {
  33. if (base.enabled && (!isSelected || UICamera.currentScheme == UICamera.ControlScheme.Controller))
  34. {
  35. this.OnHover(isSelected);
  36. }
  37. }
  38. private void OnClick()
  39. {
  40. if (base.enabled && this.trigger == UIButtonMessage.Trigger.OnClick)
  41. {
  42. this.Send();
  43. }
  44. }
  45. private void OnDoubleClick()
  46. {
  47. if (base.enabled && this.trigger == UIButtonMessage.Trigger.OnDoubleClick)
  48. {
  49. this.Send();
  50. }
  51. }
  52. private void Send()
  53. {
  54. if (string.IsNullOrEmpty(this.functionName))
  55. {
  56. return;
  57. }
  58. if (this.target == null)
  59. {
  60. this.target = base.gameObject;
  61. }
  62. if (this.includeChildren)
  63. {
  64. Transform[] componentsInChildren = this.target.GetComponentsInChildren<Transform>();
  65. int i = 0;
  66. int num = componentsInChildren.Length;
  67. while (i < num)
  68. {
  69. Transform transform = componentsInChildren[i];
  70. transform.gameObject.SendMessage(this.functionName, base.gameObject, SendMessageOptions.DontRequireReceiver);
  71. i++;
  72. }
  73. }
  74. else
  75. {
  76. this.target.SendMessage(this.functionName, base.gameObject, SendMessageOptions.DontRequireReceiver);
  77. }
  78. }
  79. public GameObject target;
  80. public string functionName;
  81. public UIButtonMessage.Trigger trigger;
  82. public bool includeChildren;
  83. private bool mStarted;
  84. public enum Trigger
  85. {
  86. OnClick,
  87. OnMouseOver,
  88. OnMouseOut,
  89. OnPress,
  90. OnRelease,
  91. OnDoubleClick
  92. }
  93. }