VRHandAttachment.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class VRHandAttachment : MonoBehaviour
  6. {
  7. public VRHandAttachment.OnHandAttachment onAttachment
  8. {
  9. get
  10. {
  11. return this.m_OnAttachment;
  12. }
  13. }
  14. public AVRController controller
  15. {
  16. get
  17. {
  18. return this.m_Controller;
  19. }
  20. private set
  21. {
  22. this.m_Controller = value;
  23. }
  24. }
  25. public bool isLeftHand
  26. {
  27. get
  28. {
  29. return this.m_IsLeftHand;
  30. }
  31. }
  32. private void Start()
  33. {
  34. GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID;
  35. if (vrdeviceTypeID == GameMain.VRDeviceType.NON || vrdeviceTypeID == GameMain.VRDeviceType.RIFT || vrdeviceTypeID == GameMain.VRDeviceType.FOVE)
  36. {
  37. base.gameObject.SetActive(false);
  38. return;
  39. }
  40. this.coFindParent = this.CoroutineFindParent();
  41. base.StartCoroutine(this.coFindParent);
  42. }
  43. private IEnumerator CoroutineFindParent()
  44. {
  45. for (int i = 0; i < 4; i++)
  46. {
  47. yield return null;
  48. }
  49. while (this.controller == null)
  50. {
  51. this.controller = ((!this.m_IsLeftHand) ? GameMain.Instance.OvrMgr.ovr_obj.right_controller.controller : GameMain.Instance.OvrMgr.ovr_obj.left_controller.controller);
  52. yield return null;
  53. }
  54. this.HandAttachmentParent();
  55. yield break;
  56. }
  57. private void HandAttachmentParent()
  58. {
  59. GameObject gameObject = this.controller.gameObject;
  60. this.m_AttachmentGameObject.transform.SetParent(gameObject.transform, false);
  61. if (this.onAttachment != null)
  62. {
  63. this.onAttachment.Invoke(this);
  64. }
  65. }
  66. private void OnDisable()
  67. {
  68. if (this.m_IsQuittingApplication)
  69. {
  70. return;
  71. }
  72. if (this.coFindParent != null && this.coFindParent.MoveNext())
  73. {
  74. base.StopCoroutine(this.coFindParent);
  75. }
  76. }
  77. private void OnEnable()
  78. {
  79. if (this.coFindParent != null && this.coFindParent.MoveNext())
  80. {
  81. base.StartCoroutine(this.coFindParent);
  82. }
  83. }
  84. private void OnApplicationQuit()
  85. {
  86. this.m_IsQuittingApplication = true;
  87. }
  88. [SerializeField]
  89. private GameObject m_AttachmentGameObject;
  90. [SerializeField]
  91. private VRHandAttachment.OnHandAttachment m_OnAttachment = new VRHandAttachment.OnHandAttachment();
  92. [SerializeField]
  93. private bool m_IsLeftHand;
  94. private AVRController m_Controller;
  95. private IEnumerator coFindParent;
  96. private bool m_IsQuittingApplication;
  97. [Serializable]
  98. public class OnHandAttachment : UnityEvent<VRHandAttachment>
  99. {
  100. }
  101. }