SimpleButton.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class SimpleButton:MonoBehaviour {
  6. /*
  7. A button with simple actions; syntax sugar
  8. */
  9. // Properties
  10. private bool _isPointerOver;
  11. private bool _isPressed;
  12. // ================================================================================================================
  13. // MAIN EVENT INTERFACE -------------------------------------------------------------------------------------------
  14. // Gives warnings on Android:
  15. // "Game scripts or other custom code contains OnMouse_ event handlers. Presence of such handlers might impact performance on handheld devices."
  16. // Use separate events?
  17. // http://wiki.unity3d.com/index.php/OnMouseDown
  18. void OnMouseDown() {
  19. // Called when the user has pressed the mouse button while over the GUIElement or Collider.
  20. _isPressed = true;
  21. animatePress();
  22. }
  23. void OnMouseDrag() {
  24. // Called every frame when the user has clicked on a GUIElement or Collider and is still holding down the mouse (inside or not)
  25. }
  26. void OnMouseEnter() {
  27. // Called when the mouse entered the GUIElement or Collider.
  28. _isPointerOver = true;
  29. if (_isPressed) animatePress();
  30. animateOver();
  31. }
  32. void OnMouseExit() {
  33. // Called when the mouse is not any longer over the GUIElement or Collider.
  34. _isPointerOver = false;
  35. if (_isPressed) animateRelease();
  36. animateOut();
  37. }
  38. void OnMouseOver() {
  39. // Called every frame while the mouse is over the GUIElement or Collider.
  40. }
  41. void OnMouseUpAsButton() {
  42. // Only called when the mouse is released over the same GUIElement or Collider as it was pressed. This is called BEFORE OnMouseUp
  43. if (_isPressed) {
  44. performAction();
  45. animateRelease();
  46. }
  47. }
  48. void OnMouseUp() {
  49. // Called when the user has released the mouse button
  50. if (_isPressed) {
  51. _isPressed = false;
  52. }
  53. }
  54. // ================================================================================================================
  55. // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
  56. // ================================================================================================================
  57. // ACCESSOR INTERFACE ---------------------------------------------------------------------------------------------
  58. protected bool isPointerOver {
  59. get {
  60. return _isPointerOver;
  61. }
  62. }
  63. protected bool isPressed {
  64. get {
  65. return _isPressed;
  66. }
  67. }
  68. // ================================================================================================================
  69. // EXTENDABLE INTERFACE -------------------------------------------------------------------------------------------
  70. protected virtual void animatePress() {
  71. // Press
  72. }
  73. protected virtual void animateRelease() {
  74. // Release
  75. }
  76. protected virtual void animateOver() {
  77. // Pointer over
  78. }
  79. protected virtual void animateOut() {
  80. // Pointer out
  81. }
  82. protected virtual void performAction() {
  83. // Pressed and released without moving: execute action
  84. }
  85. // ================================================================================================================
  86. // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
  87. }