Tan*_* Li 7 c# unity-game-engine unity3d-editor unity3d-gui unity5
当设置为时,我正在尝试使用第一人称控制器与世界空间UI进行交互.Cursor.lockStateCursorLockMode.Locked
但是当光标被锁定时,光标位置设置为(-1,-1),这是从Inspector告知的.
我进行了图形光线投射EventSystem.RaycastAll,Sreen.width/2和PointerEventData.EventSystem.current.RaycastAll收集屏幕中间的所有UI对象,但不会向它们发送任何事件.
我还试图ExecuteEvents.Execute<IEventSystemHandler>将事件发送到UI目标.当我向其发送'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
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在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);
}
Run Code Online (Sandbox Code Playgroud)
小智 1
我在使用 Graphics Raycaster 和 Global UI 元素时也遇到了很多问题。我也尝试了很多东西,但事件系统似乎不起作用。但我正在模拟带有触摸板的鼠标,也许我没有完全模拟鼠标如何与 UI 元素配合使用。
我最终放弃了图形光线投射器,而是向所有 UI 元素添加了一个盒子碰撞器。这样,常规物理光线投射器就能够检测到它并运行一些代码。对于滑块,它只是随着指针的 x 或 y 增加而增加滑块的值。
希望这有所帮助。