SCKeyboardBaseKey.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace SC.XR.Unity.Module_Keyboard
  7. {
  8. public abstract class SCKeyboardBaseKey : MonoBehaviour, IPointerDownHandler
  9. {
  10. [SerializeField]
  11. private string m_Value;
  12. public virtual string Value {
  13. get {
  14. return m_Value;
  15. }
  16. set {
  17. m_Value = value;
  18. }
  19. }
  20. private SCKeyboardMono m_SCKeyboardMono;
  21. protected SCKeyboardMono SCKeyboardMono {
  22. get
  23. {
  24. if (m_SCKeyboardMono == null)
  25. {
  26. m_SCKeyboardMono = GetComponentInParent<SCKeyboardMono>();
  27. }
  28. return m_SCKeyboardMono;
  29. }
  30. }
  31. public Action OnKeyClickEvent;
  32. Button button;
  33. UnityAction call;
  34. protected virtual void Awake()
  35. {
  36. RegistKey();
  37. }
  38. protected virtual void OnEnable()
  39. {
  40. Init();
  41. }
  42. protected virtual void OnDisable()
  43. {
  44. }
  45. protected virtual void Start()
  46. {
  47. }
  48. protected virtual void OnDestroy()
  49. {
  50. UnRegistKey();
  51. }
  52. public virtual void Init()
  53. {
  54. }
  55. protected virtual void RegistKey()
  56. {
  57. if (button == null) button = this.GetComponent<Button>();
  58. if (call == null) call = OnKeyClick;
  59. if (button)
  60. {
  61. button.onClick.AddListener(call);
  62. }
  63. }
  64. protected virtual void UnRegistKey()
  65. {
  66. if (button)
  67. {
  68. button.onClick.RemoveListener(call);
  69. button = null;
  70. call = null;
  71. }
  72. }
  73. protected virtual void OnKeyClick()
  74. {
  75. OnKeyClickEvent?.Invoke();
  76. }
  77. protected virtual void OnKeyClick(BaseEventData eventData) { OnKeyClickEvent?.Invoke(); }
  78. public virtual void OnPointerDown(PointerEventData eventData)
  79. {
  80. SCKeyboardMono.OnPointerDown?.Invoke(eventData);
  81. }
  82. }
  83. }