当Cursor.lockState
设置为CursorLockMode.Locked
时,我试图使用第一人称控制器
与世界空间UI
交互。
但是当鼠标指针被锁定时,光标位置被设置为(-1,-1),这从检查器中得知。
我使用了EventSystem.RaycastAll
,Sreen.width/2
和PointerEventData
进行图形射线投射。 EventSystem.current.RaycastAll
会收集屏幕中央所有UI对象,但是没有事件发送到它们。
我还尝试过对ExecuteEvents.Execute<IEventSystemHandler>
进行手动事件发送,当我向按钮发送“ submit”事件时,它有效。 很明显这不是一个优雅的解决方案。 我不知道如何向滑块发送消息。
// 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
);
}
}
}
这个疯狂的尝试只有在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);
}