using System.Collections; using NUnit.Framework; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.TestTools; using UnityEngine.UI; using UnityEngine.XR.Interaction.Toolkit; using UnityEngine.XR.Interaction.Toolkit.Inputs.Readers; using UnityEngine.XR.Interaction.Toolkit.Interactors; using UnityEngine.XR.Interaction.Toolkit.UI; namespace jeanf.universalplayer.tests { /// /// Verifies VR UI interaction without a headset: an XRRayInteractor pointing at a /// world-space canvas (TrackedDeviceGraphicRaycaster) must hit UI, and a manually /// queued press (XRI's ManualValue input mode — same trick the Hands Test Bench /// uses) must produce a real Button click through XRUIInputModule. /// public class UiInteractionTests { private GameObject _eventSystemGo; private GameObject _managerGo; private GameObject _interactorGo; private GameObject _canvasGo; private GameObject _cameraGo; private XRRayInteractor _rayInteractor; private Button _button; private int _clickCount; [UnitySetUp] public IEnumerator SetUp() { _clickCount = 0; // UI event pipeline: EventSystem + XRI's input module (created before the // interactor so the interactor can register with it on enable) // fully qualified: inside the jeanf.* namespace, "EventSystem" resolves to the // jeanf.EventSystem namespace instead of the UGUI component _eventSystemGo = new GameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(XRUIInputModule)); _managerGo = new GameObject("XRInteractionManager", typeof(XRInteractionManager)); _cameraGo = new GameObject("TestCamera", typeof(Camera)); // world-space canvas with the same raycaster the package's UI prefabs use _canvasGo = new GameObject("Canvas", typeof(Canvas), typeof(TrackedDeviceGraphicRaycaster)); var canvas = _canvasGo.GetComponent(); canvas.renderMode = RenderMode.WorldSpace; canvas.worldCamera = _cameraGo.GetComponent(); var canvasRect = _canvasGo.GetComponent(); canvasRect.position = new Vector3(0f, 0f, 2f); canvasRect.sizeDelta = new Vector2(400f, 400f); canvasRect.localScale = Vector3.one * 0.005f; // 2m x 2m — impossible to miss var buttonGo = new GameObject("Button", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Button)); buttonGo.transform.SetParent(_canvasGo.transform, false); var buttonRect = buttonGo.GetComponent(); buttonRect.anchorMin = Vector2.zero; buttonRect.anchorMax = Vector2.one; buttonRect.offsetMin = Vector2.zero; buttonRect.offsetMax = Vector2.zero; _button = buttonGo.GetComponent