VRGrabbable.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using UnityEngine;
  3. public class VRGrabbable : MonoBehaviour
  4. {
  5. public bool AllowOffhandGrab
  6. {
  7. get
  8. {
  9. return this.m_allowOffhandGrab;
  10. }
  11. }
  12. public bool IsGrabbed
  13. {
  14. get
  15. {
  16. return this.m_grabbedBy != null;
  17. }
  18. }
  19. public bool SnapPosition
  20. {
  21. get
  22. {
  23. return this.m_snapPosition;
  24. }
  25. }
  26. public bool SnapOrientation
  27. {
  28. get
  29. {
  30. return this.m_snapOrientation;
  31. }
  32. }
  33. public Transform SnapOffset
  34. {
  35. get
  36. {
  37. return this.m_snapOffset;
  38. }
  39. }
  40. public VRGrabber GrabbedBy
  41. {
  42. get
  43. {
  44. return this.m_grabbedBy;
  45. }
  46. }
  47. public Transform GrabbedTransform
  48. {
  49. get
  50. {
  51. return this.m_grabbedCollider.transform;
  52. }
  53. }
  54. public Rigidbody GrabbedRigidbody
  55. {
  56. get
  57. {
  58. return this.m_grabbedCollider.attachedRigidbody;
  59. }
  60. }
  61. public Collider[] GrabPoints
  62. {
  63. get
  64. {
  65. return this.m_grabPoints;
  66. }
  67. }
  68. public virtual void GrabBegin(VRGrabber hand, Collider grabPoint)
  69. {
  70. this.m_grabbedBy = hand;
  71. this.m_grabbedCollider = grabPoint;
  72. base.gameObject.GetComponent<Rigidbody>().isKinematic = true;
  73. this.SetColor(VRGrabbable.COLOR_GRAB);
  74. }
  75. public virtual void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity)
  76. {
  77. Rigidbody component = base.gameObject.GetComponent<Rigidbody>();
  78. component.isKinematic = this.m_grabbedKinematic;
  79. component.velocity = linearVelocity;
  80. component.angularVelocity = angularVelocity;
  81. this.m_grabbedBy = null;
  82. this.m_grabbedCollider = null;
  83. this.SetColor(this.m_color);
  84. }
  85. protected virtual void Awake()
  86. {
  87. if (this.m_grabPoints.Length == 0)
  88. {
  89. Collider component = base.GetComponent<Collider>();
  90. if (component == null)
  91. {
  92. throw new ArgumentException("Grabbable: Grabbables cannot have zero grab points and no collider -- please add a grab point or collider.");
  93. }
  94. this.m_grabPoints = new Collider[]
  95. {
  96. component
  97. };
  98. }
  99. this.m_color = new Color(UnityEngine.Random.Range(0.1f, 0.95f), UnityEngine.Random.Range(0.1f, 0.95f), UnityEngine.Random.Range(0.1f, 0.95f), 1f);
  100. this.m_meshRenderers = base.GetComponentsInChildren<MeshRenderer>();
  101. this.SetColor(this.m_color);
  102. }
  103. private void Start()
  104. {
  105. this.m_grabbedKinematic = base.GetComponent<Rigidbody>().isKinematic;
  106. }
  107. private void OnDestroy()
  108. {
  109. if (this.m_grabbedBy != null)
  110. {
  111. this.m_grabbedBy.ForceRelease(this);
  112. }
  113. }
  114. private void SendMsg(string msgName, object msg)
  115. {
  116. base.transform.SendMessage(msgName, msg, SendMessageOptions.DontRequireReceiver);
  117. }
  118. private void SetColor(Color color)
  119. {
  120. if (!this.m_changeColor)
  121. {
  122. return;
  123. }
  124. for (int i = 0; i < this.m_meshRenderers.Length; i++)
  125. {
  126. MeshRenderer meshRenderer = this.m_meshRenderers[i];
  127. for (int j = 0; j < meshRenderer.materials.Length; j++)
  128. {
  129. Material material = meshRenderer.materials[j];
  130. material.color = color;
  131. }
  132. }
  133. }
  134. [SerializeField]
  135. private bool m_allowOffhandGrab = true;
  136. [SerializeField]
  137. private bool m_snapPosition;
  138. [SerializeField]
  139. private bool m_snapOrientation;
  140. [SerializeField]
  141. private Transform m_snapOffset;
  142. [SerializeField]
  143. private Collider[] m_grabPoints;
  144. [SerializeField]
  145. public bool m_changeColor;
  146. private bool m_grabbedKinematic;
  147. private Collider m_grabbedCollider;
  148. private VRGrabber m_grabbedBy;
  149. public static readonly Color COLOR_GRAB = new Color(1f, 0.5f, 0f, 1f);
  150. private Color m_color = Color.black;
  151. private MeshRenderer[] m_meshRenderers;
  152. }