UIInteraction.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using Valve.VR;
  6. using Valve.VR.Extras;
  7. using Valve.VR.InteractionSystem;
  8. public class UIInteraction : MonoBehaviour
  9. {
  10. public static GameObject curentObject;
  11. public Hand TargetSource;
  12. public SteamVR_Action_Boolean ClickAction;
  13. public EventSystem system;
  14. int currID;
  15. private SteamVR_LaserPointer pointer;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. curentObject = null;
  20. currID = 0;
  21. pointer = GetComponent<SteamVR_LaserPointer>();
  22. }
  23. // Update is called once per frame
  24. void Update()
  25. {
  26. if (TargetSource.currentAttachedObject != null)
  27. {
  28. pointer.holder.SetActive(false);
  29. }
  30. else
  31. {
  32. pointer.holder.SetActive(true);
  33. }
  34. if (ClickAction.GetStateDown(TargetSource.handType) && pointer.active)
  35. {
  36. RaycastHit[] hits;
  37. hits = Physics.RaycastAll(transform.position, transform.forward, 0.25f);
  38. for (int i = 0; i < hits.Length; i++)
  39. {
  40. RaycastHit hit = hits[i];
  41. int id = hit.collider.gameObject.GetInstanceID();
  42. if (currID != id)
  43. {
  44. //currID = id;
  45. curentObject = hit.collider.gameObject;
  46. ExecuteEvents.Execute(curentObject, new BaseEventData(system), ExecuteEvents.submitHandler);
  47. Debug.Log("pressed stuff");
  48. }
  49. }
  50. }
  51. }
  52. }