GameObjectExpand.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using UnityEngine;
  3. public static class GameObjectExpand
  4. {
  5. public static ClickButton OnClick(this GameObject obj, Action callBack)
  6. {
  7. if (obj == null) return null;
  8. ClickEvent clickEvent = GetClickEvent(obj);
  9. ClickButton button = clickEvent.OnRegister<ClickButton>(callBack);
  10. return button;
  11. }
  12. public static DoubleClickButton OnDoubleClick(this GameObject obj, Action callBack)
  13. {
  14. if (obj == null) return null;
  15. ClickEvent clickEvent = GetClickEvent(obj);
  16. DoubleClickButton button = clickEvent.OnRegister<DoubleClickButton>(callBack);
  17. return button;
  18. }
  19. public static PressButton OnPress(this GameObject obj, Action callBack)
  20. {
  21. if (obj == null) return null;
  22. ClickEvent clickEvent = GetClickEvent(obj);
  23. PressButton button = clickEvent.OnRegister<PressButton>(callBack);
  24. return button;
  25. }
  26. static ClickEvent GetClickEvent(GameObject obj)
  27. {
  28. ClickEvent clickEvent = obj.GetComponent<ClickEvent>();
  29. if (clickEvent == null) clickEvent = obj.AddComponent<ClickEvent>();
  30. return clickEvent;
  31. }
  32. }