1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Valve.VR;
- using Valve.VR.InteractionSystem;
- using Valve.VR.InteractionSystem.Sample;
- public class ToggleVisibility : MonoBehaviour
- {
- public SteamVR_Action_Boolean FixController = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("CMController", "FixController");
- Interactable InteractableScript;
- InputHandling UnderlyingController;
- LockToPoint LockToPoint;
- CheckVisible CheckVisible;
- List<MeshRenderer> Meshs = new List<MeshRenderer>();
- // Start is called before the first frame update
- void Start()
- {
- InteractableScript = GetComponent<Interactable>();
- UnderlyingController = GetComponent<InputHandling>();
- InteractableScript.onAttachedToHand += AttachedToHand;
- InteractableScript.onDetachedFromHand += DetachedToHand;
- LockToPoint = GetComponent<LockToPoint>();
- CheckVisible = GetComponentInChildren<CheckVisible>();
- // Get Meshes that will be hidden on PickUp
- Meshs.AddRange(GetComponents<MeshRenderer>());
- Meshs.AddRange(GetComponentsInChildren<MeshRenderer>());
- }
- // Update is called once per frame
- void Update()
- {
- if (InteractableScript.attachedToHand && FixController.GetStateDown(InteractableScript.attachedToHand.handType))
- {
- LockToPoint.snapToDynamic.position = InteractableScript.attachedToHand.transform.position;
- LockToPoint.snapToDynamic.rotation = InteractableScript.attachedToHand.transform.rotation;
- CheckVisible.enabled = false;
- LockToPoint.SnapDynamic = true;
- }
- }
- private void AttachedToHand(Hand hand)
- {
- CheckVisible.enabled = true;
- LockToPoint.SnapDynamic = false;
- Meshs.ForEach(x => x.enabled = false);
- if (UnderlyingController != null)
- ControllerButtonHints.ShowButtonHint(hand, UnderlyingController.AvalibleButtons());
- }
- private void DetachedToHand(Hand hand)
- {
- Meshs.ForEach(x => x.enabled = true);
- if (UnderlyingController != null)
- ControllerButtonHints.HideButtonHint(hand, UnderlyingController.AvalibleButtons());
- }
- }
|