ModuleInteractor.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine;
  2. namespace Rokid.UXR.Interaction
  3. {
  4. /// <summary>
  5. /// Module Interactor, users bind the corresponding interactors to the corresponding modules, and handle updates, destruction, and other operations of the corresponding module interactors.
  6. /// </summary>
  7. public class ModuleInteractor : MonoBehaviour
  8. {
  9. public InputModuleType moduleType;
  10. private void Start()
  11. {
  12. }
  13. private void Awake()
  14. {
  15. switch (moduleType)
  16. {
  17. case InputModuleType.ThreeDof:
  18. UpdateInteractor(ThreeDofEventInput.Instance, transform);
  19. ThreeDofEventInput.OnReleaseThreeDofModule += DestroySelf;
  20. break;
  21. case InputModuleType.Gesture:
  22. UpdateInteractor(GesEventInput.Instance, transform);
  23. GesEventInput.OnReleaseGesModule += DestroySelf;
  24. break;
  25. case InputModuleType.Mouse:
  26. UpdateInteractor(MouseEventInput.Instance, transform);
  27. MouseEventInput.OnReleaseMouseModule += DestroySelf;
  28. break;
  29. case InputModuleType.ButtonMouse:
  30. UpdateInteractor(ButtonMouseEventInput.Instance, transform);
  31. ButtonMouseEventInput.OnReleaseButtonMouseModule += DestroySelf;
  32. break;
  33. case InputModuleType.TouchPad:
  34. UpdateInteractor(TouchPadEventInput.Instance, transform);
  35. TouchPadEventInput.OnReleaseTouchPadModule += DestroySelf;
  36. break;
  37. }
  38. }
  39. private void OnDestroy()
  40. {
  41. switch (moduleType)
  42. {
  43. case InputModuleType.ThreeDof:
  44. ThreeDofEventInput.OnReleaseThreeDofModule -= DestroySelf;
  45. break;
  46. case InputModuleType.Gesture:
  47. GesEventInput.OnReleaseGesModule -= DestroySelf;
  48. break;
  49. case InputModuleType.Mouse:
  50. MouseEventInput.OnReleaseMouseModule -= DestroySelf;
  51. break;
  52. case InputModuleType.ButtonMouse:
  53. MouseEventInput.OnReleaseMouseModule -= DestroySelf;
  54. break;
  55. case InputModuleType.TouchPad:
  56. TouchPadEventInput.OnReleaseTouchPadModule -= DestroySelf;
  57. break;
  58. }
  59. }
  60. private void UpdateInteractor(IEventInput eventInput, Transform curInteractor)
  61. {
  62. if (eventInput.Interactor != null)
  63. {
  64. DestroyImmediate(eventInput.Interactor.gameObject);
  65. }
  66. eventInput.Interactor = this.transform;
  67. }
  68. private void DestroySelf()
  69. {
  70. Destroy(this.gameObject);
  71. }
  72. }
  73. }