Unity UI - interacting with the worldspace user interface when locking the cursor

I am trying to interact with a world space interface using a first-person controller when Cursor.lockState set to CursorLockMode.Locked .

world class user interface user interface and space of the world

But when the cursor is locked, the cursor position is set to (- 1, -1) , which is reported from the Inspector .

cursor position (-1, -1) cursor position (-1, -1)

I performed graphical raycast with EventSystem.RaycastAll , Sreen.width/2 and PointerEventData . EventSystem.current.RaycastAll collects all user interface objects in the middle of the screen, but no events are sent to them.

I also tried ExecuteEvents.Execute<IEventSystemHandler> to send the event to custom objects. This works for the button when I send it a message. Obviously, this is not an elegant solution. I don’t even know how to send a message to the slider.

 // manully send a 'submit' event to UI elements List<RaycastResult> results = new List<RaycastResult>(); void Update() { if (Input.GetButtonUp("Fire1")) { PointerEventData data = new PointerEventData(EventSystem.current); data.position = new Vector2(Screen.width / 2, Screen.height / 2); EventSystem.current.RaycastAll(data, results); foreach (var result in results) { ExecuteEvents.ExecuteHierarchy<ISubmitHandler>( result.gameObject, data, ExecuteEvents.submitHandler ); } } } 

This crazy attempt works when playing in full screen on Windows. 2333

 [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern int SetCursorPos ( int x , int y ); void SetCursorPositionToCenter() {  SetCursorPos(Screen.width/2, Screen.height/2); } 

Relavant Resources

+5
source share

Source: https://habr.com/ru/post/1264553/


All Articles