简体   繁体   English

Unity EditorWindow-在场景视图中查找鼠标位置

[英]Unity EditorWindow - find mouse position in sceen view

I've been googling and trying to find an answer to this. 我一直在谷歌搜索,并试图找到答案。 But I've come up with absolutely nothing. 但是我什么也没想出来。

private static void OnSceneGUI(SceneView sceneView)
{
    // var mousePos = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    // Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    Ray ray = Camera.current.ScreenPointToRay(Event.current.mousePosition);
    Debug.Log("Screen: " + ray);
}

This is what I've come up with now, to find the mouse position. 这就是我现在想出的方法,以找到鼠标位置。

It seems that the X is always right, but the Y and Z is following the zoom of the camera, not the mousePos on screen. 似乎X总是正确的,但是Y和Z跟随摄像机的缩放,而不是屏幕上的mousePos。

My goal is to find mousePos and then reset playerPos to where my mouse is. 我的目标是找到mousePos,然后将playerPos重置为鼠标所在的位置。

[MenuItem("MyMenu/DevTools/ResetPlayer #r")]
private static void ResetPlayer()
{
    var player = GameObject.Find("Player");
    Transform playerPos = player.GetComponent<Transform>();
    Vector3 reset = new Vector3(-7, 0, 0);

    playerPos.position = reset;
}

For now I've only figured out how to reset to a fixed position. 现在,我只想出了如何重设到固定位置。

I am very new in this editor coding, so I appreciate all help I can get! 我在此编辑器编码中非常陌生,因此,我感谢能获得的所有帮助! :) :)

Alright, so finally figured out how to do this! 好了,所以终于想出了该怎么做! I'll just share my results here in case someone else is stuck on this in the future. 我只是在这里分享我的结果,以防将来有人被卡住。 :) :)

 static Vector3 resets; 
private static void OnSceneGUI(SceneView sceneView)
{
    Vector3 distanceFromCam = new Vector3(Camera.main.transform.position.x, 
                                                Camera.main.transform.position.y, 
                                                    0);
    Plane plane = new Plane(Vector3.forward, distanceFromCam);

    Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    float enter = 0.0f;

    if (plane.Raycast(ray, out enter))
    {
        //Get the point that is clicked
        resets = ray.GetPoint(enter);
        //Debug.Log("Mouse Pos" + resets);
    }
}

And here is the shortcut for the button and keybinding. 这是按钮和按键绑定的快捷方式。

[MenuItem("MyMenu/DevTools/ResetPlayer #r")]
private static void ResetPlayer()
{
    var player = GameObject.Find("Player");                                     // Find Player GameObject.
    Transform playerPos = player.GetComponent<Transform>();                     // Get the Transform from PlayerGO and make it to a Transform playerPos.
    //Vector3 resets = new Vector3(-7, 0, 0);                                    // Define the hardcoded position you want to reset the player to.                              

    playerPos.position = resets;                                                // Set playerPos to a hardcoded position.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM