123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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<Rigidbody>().isKinematic = true;
- this.SetColor(VRGrabbable.COLOR_GRAB);
- }
- public virtual void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity)
- {
- Rigidbody component = base.gameObject.GetComponent<Rigidbody>();
- 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<Collider>();
- 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<MeshRenderer>();
- this.SetColor(this.m_color);
- }
- private void Start()
- {
- this.m_grabbedKinematic = base.GetComponent<Rigidbody>().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;
- }
|