Button_Sprite.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. //#define SOUND_MANAGER // Has Sound_Manager in project
  11. //#define CURSOR_MANAGER // Has Cursor_Manager in project
  12. using System;
  13. using UnityEngine;
  14. using System.Collections.Generic;
  15. using UnityEngine.EventSystems;
  16. namespace CodeMonkey.Utils {
  17. /*
  18. * Button Actions on a World BoxCollider
  19. * */
  20. public class Button_Sprite : MonoBehaviour {
  21. private static Func<Camera> GetWorldCamera;
  22. public static void SetGetWorldCamera(Func<Camera> GetWorldCamera) {
  23. Button_Sprite.GetWorldCamera = GetWorldCamera;
  24. }
  25. public Action ClickFunc = null;
  26. public Action MouseRightDownOnceFunc = null;
  27. public Action MouseRightDownFunc = null;
  28. public Action MouseRightUpFunc = null;
  29. public Action MouseDownOnceFunc = null;
  30. public Action MouseUpOnceFunc = null;
  31. public Action MouseOverOnceFunc = null;
  32. public Action MouseOutOnceFunc = null;
  33. public Action MouseOverOnceTooltipFunc = null;
  34. public Action MouseOutOnceTooltipFunc = null;
  35. private bool draggingMouseRight;
  36. private Vector3 mouseRightDragStart;
  37. public Action<Vector3, Vector3> MouseRightDragFunc = null;
  38. public Action<Vector3, Vector3> MouseRightDragUpdateFunc = null;
  39. public bool triggerMouseRightDragOnEnter = false;
  40. public enum HoverBehaviour {
  41. Custom,
  42. Change_Color,
  43. Change_Image,
  44. Change_SetActive,
  45. }
  46. public HoverBehaviour hoverBehaviourType = HoverBehaviour.Custom;
  47. private Action hoverBehaviourFunc_Enter, hoverBehaviourFunc_Exit;
  48. public Color hoverBehaviour_Color_Enter = new Color(1, 1, 1, 1), hoverBehaviour_Color_Exit = new Color(1, 1, 1, 1);
  49. public SpriteRenderer hoverBehaviour_Image;
  50. public Sprite hoverBehaviour_Sprite_Exit, hoverBehaviour_Sprite_Enter;
  51. public bool hoverBehaviour_Move = false;
  52. public Vector2 hoverBehaviour_Move_Amount = Vector2.zero;
  53. private Vector3 posExit, posEnter;
  54. public bool triggerMouseOutFuncOnClick = false;
  55. public bool clickThroughUI = false;
  56. private Action internalOnMouseDownFunc = null, internalOnMouseEnterFunc = null, internalOnMouseExitFunc = null;
  57. #if SOUND_MANAGER
  58. public Sound_Manager.Sound mouseOverSound, mouseClickSound;
  59. #endif
  60. #if CURSOR_MANAGER
  61. public CursorManager.CursorType cursorMouseOver, cursorMouseOut;
  62. #endif
  63. public void SetHoverBehaviourChangeColor(Color colorOver, Color colorOut) {
  64. hoverBehaviourType = HoverBehaviour.Change_Color;
  65. hoverBehaviour_Color_Enter = colorOver;
  66. hoverBehaviour_Color_Exit = colorOut;
  67. if (hoverBehaviour_Image == null) hoverBehaviour_Image = transform.GetComponent<SpriteRenderer>();
  68. hoverBehaviour_Image.color = hoverBehaviour_Color_Exit;
  69. SetupHoverBehaviour();
  70. }
  71. void OnMouseDown() {
  72. if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
  73. if (internalOnMouseDownFunc != null) internalOnMouseDownFunc();
  74. if (ClickFunc != null) ClickFunc();
  75. if (MouseDownOnceFunc != null) MouseDownOnceFunc();
  76. if (triggerMouseOutFuncOnClick) OnMouseExit();
  77. }
  78. public void Manual_OnMouseExit() {
  79. OnMouseExit();
  80. }
  81. void OnMouseUp() {
  82. if (MouseUpOnceFunc != null) MouseUpOnceFunc();
  83. }
  84. void OnMouseEnter() {
  85. if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
  86. if (internalOnMouseEnterFunc != null) internalOnMouseEnterFunc();
  87. if (hoverBehaviour_Move) transform.localPosition = posEnter;
  88. if (hoverBehaviourFunc_Enter != null) hoverBehaviourFunc_Enter();
  89. if (MouseOverOnceFunc != null) MouseOverOnceFunc();
  90. if (MouseOverOnceTooltipFunc != null) MouseOverOnceTooltipFunc();
  91. }
  92. void OnMouseExit() {
  93. if (internalOnMouseExitFunc != null) internalOnMouseExitFunc();
  94. if (hoverBehaviour_Move) transform.localPosition = posExit;
  95. if (hoverBehaviourFunc_Exit != null) hoverBehaviourFunc_Exit();
  96. if (MouseOutOnceFunc != null) MouseOutOnceFunc();
  97. if (MouseOutOnceTooltipFunc != null) MouseOutOnceTooltipFunc();
  98. }
  99. void OnMouseOver() {
  100. if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
  101. if (Input.GetMouseButton(1)) {
  102. if (MouseRightDownFunc != null) MouseRightDownFunc();
  103. if (!draggingMouseRight && triggerMouseRightDragOnEnter) {
  104. draggingMouseRight = true;
  105. mouseRightDragStart = GetWorldPositionFromUI();
  106. }
  107. }
  108. if (Input.GetMouseButtonDown(1)) {
  109. draggingMouseRight = true;
  110. mouseRightDragStart = GetWorldPositionFromUI();
  111. if (MouseRightDownOnceFunc != null) MouseRightDownOnceFunc();
  112. }
  113. }
  114. void Update() {
  115. if (draggingMouseRight) {
  116. if (MouseRightDragUpdateFunc != null) MouseRightDragUpdateFunc(mouseRightDragStart, GetWorldPositionFromUI());
  117. }
  118. if (Input.GetMouseButtonUp(1)) {
  119. if (draggingMouseRight) {
  120. draggingMouseRight = false;
  121. if (MouseRightDragFunc != null) MouseRightDragFunc(mouseRightDragStart, GetWorldPositionFromUI());
  122. }
  123. if (MouseRightUpFunc != null) MouseRightUpFunc();
  124. }
  125. }
  126. void Awake() {
  127. if (GetWorldCamera == null) SetGetWorldCamera(() => Camera.main); // Set default World Camera
  128. posExit = transform.localPosition;
  129. posEnter = transform.localPosition + (Vector3)hoverBehaviour_Move_Amount;
  130. SetupHoverBehaviour();
  131. #if SOUND_MANAGER
  132. // Sound Manager
  133. internalOnMouseDownFunc += () => { if (mouseClickSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseClickSound); };
  134. internalOnMouseEnterFunc += () => { if (mouseOverSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseOverSound); };
  135. #endif
  136. #if CURSOR_MANAGER
  137. // Cursor Manager
  138. internalOnMouseExitFunc += () => { if (cursorMouseOut != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOut); };
  139. internalOnMouseEnterFunc += () => { if (cursorMouseOver != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOver); };
  140. #endif
  141. }
  142. private void SetupHoverBehaviour() {
  143. switch (hoverBehaviourType) {
  144. case HoverBehaviour.Change_Color:
  145. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Enter; };
  146. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Exit; };
  147. break;
  148. case HoverBehaviour.Change_Image:
  149. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Enter; };
  150. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Exit; };
  151. break;
  152. case HoverBehaviour.Change_SetActive:
  153. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.gameObject.SetActive(true); };
  154. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.gameObject.SetActive(false); };
  155. break;
  156. }
  157. }
  158. private static Vector3 GetWorldPositionFromUI() {
  159. Vector3 worldPosition = GetWorldCamera().ScreenToWorldPoint(Input.mousePosition);
  160. return worldPosition;
  161. }
  162. private static bool IsPointerOverUI() {
  163. if (EventSystem.current.IsPointerOverGameObject()) {
  164. return true;
  165. } else {
  166. PointerEventData pe = new PointerEventData(EventSystem.current);
  167. pe.position = Input.mousePosition;
  168. List<RaycastResult> hits = new List<RaycastResult>();
  169. EventSystem.current.RaycastAll(pe, hits);
  170. return hits.Count > 0;
  171. }
  172. }
  173. }
  174. }