123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //======= Copyright (c) Valve Corporation, All rights reserved. ===============
- //
- // Purpose: Demonstrates how to create a simple interactable object
- //
- //=============================================================================
- using UnityEngine;
- namespace Valve.VR.InteractionSystem.Sample
- {
- //-------------------------------------------------------------------------
- [RequireComponent( typeof( Interactable ) )]
- public class InteractableExample : MonoBehaviour
- {
- public Transform camera;
- private TextMesh generalText;
- private TextMesh hoveringText;
- private Vector3 oldPosition;
- private Quaternion oldRotation;
- private float attachTime;
- private Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags & ( ~Hand.AttachmentFlags.SnapOnAttach ) & (~Hand.AttachmentFlags.DetachOthers) & (~Hand.AttachmentFlags.VelocityMovement);
- private Interactable interactable;
- //-------------------------------------------------
- void Awake()
- {
- //var textMeshs = GetComponentsInChildren<TextMesh>();
- //generalText = textMeshs[0];
- //hoveringText = textMeshs[1];
- //generalText.text = "No Hand Hovering";
- //hoveringText.text = "Hovering: False";
- interactable = this.GetComponent<Interactable>();
- }
- //-------------------------------------------------
- // Called when a Hand starts hovering over this object
- //-------------------------------------------------
- private void OnHandHoverBegin( Hand hand )
- {
- //generalText.text = "Hovering hand: " + hand.name;
- }
- //-------------------------------------------------
- // Called when a Hand stops hovering over this object
- //-------------------------------------------------
- private void OnHandHoverEnd( Hand hand )
- {
- //generalText.text = "No Hand Hovering";
- }
- //-------------------------------------------------
- // Called every Update() while a Hand is hovering over this object
- //-------------------------------------------------
- private void HandHoverUpdate( Hand hand )
- {
- GrabTypes startingGrabType = hand.GetGrabStarting();
- bool isGrabEnding = hand.IsGrabEnding(this.gameObject);
- if (interactable.attachedToHand == null && startingGrabType != GrabTypes.None)
- {
- transform.SetParent(null);
- // Save our position/rotation so that we can restore it when we detach
- oldPosition = transform.position;
- oldRotation = transform.rotation;
- // Call this to continue receiving HandHoverUpdate messages,
- // and prevent the hand from hovering over anything else
- hand.HoverLock(interactable);
- // Attach this object to the hand
- hand.AttachObject(gameObject, startingGrabType, attachmentFlags);
- }
- else if (isGrabEnding)
- {
- // Detach this object from the hand
- hand.DetachObject(gameObject);
- // Call this to undo HoverLock
- hand.HoverUnlock(interactable);
- // Restore position/rotation
- //transform.position = oldPosition;
- transform.SetParent(GameObject.Find("Automatic Cameras").transform);
- }
- }
- //-------------------------------------------------
- // Called when this GameObject becomes attached to the hand
- //-------------------------------------------------
- private void OnAttachedToHand( Hand hand )
- {
- //generalText.text = string.Format("Attached: {0}", hand.name);
- attachTime = Time.time;
- }
- //-------------------------------------------------
- // Called when this GameObject is detached from the hand
- //-------------------------------------------------
- private void OnDetachedFromHand( Hand hand )
- {
- //generalText.text = string.Format("Detached: {0}", hand.name);
- }
- //-------------------------------------------------
- // Called every Update() while this GameObject is attached to the hand
- //-------------------------------------------------
- private void HandAttachedUpdate( Hand hand )
- {
- //generalText.text = string.Format("Attached: {0} :: Time: {1:F2}", hand.name, (Time.time - attachTime));
- }
- private bool lastHovering = false;
- private void Update()
- {
- /*if (interactable.isHovering != lastHovering) //save on the .tostrings a bit
- {
- //hoveringText.text = string.Format("Hovering: {0}", interactable.isHovering);
- lastHovering = interactable.isHovering;
- }*/
- }
- //-------------------------------------------------
- // Called when this attached GameObject becomes the primary attached object
- //-------------------------------------------------
- private void OnHandFocusAcquired( Hand hand )
- {
- }
- //-------------------------------------------------
- // Called when another attached GameObject becomes the primary attached object
- //-------------------------------------------------
- private void OnHandFocusLost( Hand hand )
- {
- }
- }
- }
|