ToggleVisibility.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR;
  5. using Valve.VR.InteractionSystem;
  6. using Valve.VR.InteractionSystem.Sample;
  7. public class ToggleVisibility : MonoBehaviour
  8. {
  9. public SteamVR_Action_Boolean FixController = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("CMController", "FixController");
  10. Interactable InteractableScript;
  11. InputHandling UnderlyingController;
  12. LockToPoint LockToPoint;
  13. CheckVisible CheckVisible;
  14. List<MeshRenderer> Meshs = new List<MeshRenderer>();
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. InteractableScript = GetComponent<Interactable>();
  19. UnderlyingController = GetComponent<InputHandling>();
  20. InteractableScript.onAttachedToHand += AttachedToHand;
  21. InteractableScript.onDetachedFromHand += DetachedToHand;
  22. LockToPoint = GetComponent<LockToPoint>();
  23. CheckVisible = GetComponentInChildren<CheckVisible>();
  24. // Get Meshes that will be hidden on PickUp
  25. Meshs.AddRange(GetComponents<MeshRenderer>());
  26. Meshs.AddRange(GetComponentsInChildren<MeshRenderer>());
  27. }
  28. // Update is called once per frame
  29. void Update()
  30. {
  31. if (InteractableScript.attachedToHand && FixController.GetStateDown(InteractableScript.attachedToHand.handType))
  32. {
  33. LockToPoint.snapToDynamic.position = InteractableScript.attachedToHand.transform.position;
  34. LockToPoint.snapToDynamic.rotation = InteractableScript.attachedToHand.transform.rotation;
  35. CheckVisible.enabled = false;
  36. LockToPoint.SnapDynamic = true;
  37. }
  38. }
  39. private void AttachedToHand(Hand hand)
  40. {
  41. CheckVisible.enabled = true;
  42. LockToPoint.SnapDynamic = false;
  43. Meshs.ForEach(x => x.enabled = false);
  44. if (UnderlyingController != null)
  45. ControllerButtonHints.ShowButtonHint(hand, UnderlyingController.AvalibleButtons());
  46. }
  47. private void DetachedToHand(Hand hand)
  48. {
  49. Meshs.ForEach(x => x.enabled = true);
  50. if (UnderlyingController != null)
  51. ControllerButtonHints.HideButtonHint(hand, UnderlyingController.AvalibleButtons());
  52. }
  53. }