【问题标题】:Unity EditorWindow - find mouse position in sceen viewUnity EditorWindow - 在屏幕视图中查找鼠标位置
【发布时间】:2019-02-02 12:34:00
【问题描述】:

我一直在谷歌上搜索并试图找到答案。但我什么都想不出来。

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);
}

这就是我现在想出的,找到鼠标位置。

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.

我的目标是找到 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;
}

目前我只知道如何重置到固定位置。

我是这个编辑器编码的新手,所以我很感激我能得到的所有帮助! :)

【问题讨论】:

  • 您是如何设法在 EditorWindow 中获得 OnSceneGUI 功能的?只有“编辑器”类包含对该方法的访问权限。

标签: c# visual-studio unity3d


【解决方案1】:

好的,所以终于想出了如何做到这一点! 我将在这里分享我的结果,以防其他人在未来遇到此问题。 :)

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);
    }
}

这里是按钮和键绑定的快捷方式。

[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.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多