using System; using UnityEngine; public class VRGrabbable : MonoBehaviour { public bool AllowOffhandGrab { get { return this.m_allowOffhandGrab; } } public bool IsGrabbed { get { return this.m_grabbedBy != null; } } public bool SnapPosition { get { return this.m_snapPosition; } } public bool SnapOrientation { get { return this.m_snapOrientation; } } public Transform SnapOffset { get { return this.m_snapOffset; } } public VRGrabber GrabbedBy { get { return this.m_grabbedBy; } } public Transform GrabbedTransform { get { return this.m_grabbedCollider.transform; } } public Rigidbody GrabbedRigidbody { get { return this.m_grabbedCollider.attachedRigidbody; } } public Collider[] GrabPoints { get { return this.m_grabPoints; } } public virtual void GrabBegin(VRGrabber hand, Collider grabPoint) { this.m_grabbedBy = hand; this.m_grabbedCollider = grabPoint; base.gameObject.GetComponent().isKinematic = true; this.SetColor(VRGrabbable.COLOR_GRAB); } public virtual void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity) { Rigidbody component = base.gameObject.GetComponent(); component.isKinematic = this.m_grabbedKinematic; component.velocity = linearVelocity; component.angularVelocity = angularVelocity; this.m_grabbedBy = null; this.m_grabbedCollider = null; this.SetColor(this.m_color); } protected virtual void Awake() { if (this.m_grabPoints.Length == 0) { Collider component = base.GetComponent(); if (component == null) { throw new ArgumentException("Grabbable: Grabbables cannot have zero grab points and no collider -- please add a grab point or collider."); } this.m_grabPoints = new Collider[] { component }; } 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); this.m_meshRenderers = base.GetComponentsInChildren(); this.SetColor(this.m_color); } private void Start() { this.m_grabbedKinematic = base.GetComponent().isKinematic; } private void OnDestroy() { if (this.m_grabbedBy != null) { this.m_grabbedBy.ForceRelease(this); } } private void SendMsg(string msgName, object msg) { base.transform.SendMessage(msgName, msg, SendMessageOptions.DontRequireReceiver); } private void SetColor(Color color) { if (!this.m_changeColor) { return; } for (int i = 0; i < this.m_meshRenderers.Length; i++) { MeshRenderer meshRenderer = this.m_meshRenderers[i]; for (int j = 0; j < meshRenderer.materials.Length; j++) { Material material = meshRenderer.materials[j]; material.color = color; } } } [SerializeField] private bool m_allowOffhandGrab = true; [SerializeField] private bool m_snapPosition; [SerializeField] private bool m_snapOrientation; [SerializeField] private Transform m_snapOffset; [SerializeField] private Collider[] m_grabPoints; [SerializeField] public bool m_changeColor; private bool m_grabbedKinematic; private Collider m_grabbedCollider; private VRGrabber m_grabbedBy; public static readonly Color COLOR_GRAB = new Color(1f, 0.5f, 0f, 1f); private Color m_color = Color.black; private MeshRenderer[] m_meshRenderers; }