[cb]SceneView 获取鼠标位置

简介:



扩展需求

在Scene视图中获取鼠标的位置

Demo

在Scene视图中,当鼠标点击时实例化一个Cube

image

重点部分

image image

image

image

实现代码

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MyGrid))]
public class MyGridInspector : Editor
{
    MyGrid grid;
    public void OnEnable()
    {
        grid = (MyGrid)target;//初始化时获取引用
        SceneView.onSceneGUIDelegate += GridUpdate;//获取SceneView的输入
    }

    public void OnDisable()
    {
        // SceneView.onSceneGUIDelegate -= GridUpdate;
    }

    public override void OnInspectorGUI()
    {


        GUILayout.BeginHorizontal();
        GUILayout.Label("网格宽度");
        grid.width = EditorGUILayout.FloatField(grid.width, GUILayout.Width(50));
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("网格高度");
        grid.height = EditorGUILayout.FloatField(grid.height, GUILayout.Width(50));
        GUILayout.EndHorizontal();

        if (GUILayout.Button("打开Grid Window", GUILayout.Width(255)))
        {
            MyGridWindow window = (MyGridWindow)EditorWindow.GetWindow(typeof(MyGridWindow));
            window.Init();
        }

        SceneView.RepaintAll();//SceneView重绘
    }

    void GridUpdate(SceneView sceneview)
    {
        Event e = Event.current;//获取事件

        if (e.isKey && e.character == 'a')
        {
            //GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
            GameObject obj;

            //如果选中Object
            //if (Selection.activeObject)
            //{
            //    obj = (GameObject)Instantiate(Selection.activeObject);
            //    obj.transform.position = Vector3.zero;
            //}

            //在Editor模式实例化一个Prefab
            //if (Selection.activeObject)
            //{
            //    //找到Prefab
            //    Object prefab =  PrefabUtility.GetPrefabParent(Selection.activeObject);
            //    if (prefab)
            //    {
            //        obj = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
            //        obj.transform.position = Vector3.zero;
            //    }
            //}


            //根据鼠标点击的位置实例化Prefab
            if (Selection.activeObject)
            {
                //屏幕的鼠标坐标转换成世界坐标
                /**
                 * 1、从屏幕发出射线
                 * 2、我们需要转化事件的屏幕空间的空间是可以接受的screenpointtoray()
                 * 3、e.mousePosition 左上角坐标(0,0),右下角坐标(Camera.current.pixelWidth, -Camera.current.pixelHeight),
                      把它转换成世界坐标变为左下角(0,9),右上角(Camera.current.pixelWidth, Camera.current.pixelHeight)
                 */
                /* 
                 Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
                 Vector3 mousePos = r.origin;//mousepos向量保存射线的来源
                 //找到Prefab
                 Object prefab = PrefabUtility.GetPrefabParent(Selection.activeObject);
                 if (prefab)
                 {
                     obj = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                     obj.transform.position = new Vector3(mousePos.x,mousePos.y,0.0f);
                 }*/
            }

            //将Cube对齐到网格中心位置?
            if (Selection.activeObject)
            {
                Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
                Vector3 mousePos = r.origin;//mousepos向量保存射线的来源
                //找到Prefab
                Object prefab = PrefabUtility.GetPrefabParent(Selection.activeObject);
                if (prefab)
                {
                    obj = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                    Vector3 aligned = new Vector3(Mathf.Floor(mousePos.x / grid.width) * grid.width + grid.width / 2.0f,
                        Mathf.Floor(mousePos.y / grid.height) * grid.height + grid.height / 2.0f,
                        0.0f);

                    obj.transform.position = aligned;
                }
            }
        }
        else if (e.isKey && e.character == 'n' && e.clickCount==0) //在当前鼠标位置创建一个Cube
        {
            Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
            Vector3 mousePos = r.origin;//mousepos向量保存射线的来源

            GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
            Vector3 aligned = new Vector3(Mathf.Floor(mousePos.x / grid.width) * grid.width + grid.width / 2.0f,
                Mathf.Floor(mousePos.y / grid.height) * grid.height + grid.height / 2.0f,
                0.0f);

            obj.transform.position = aligned;
            //注册撤消操作
            Undo.RegisterCreatedObjectUndo(obj, "Create" + obj.name);
            Debug.Log("create");

        }
        else if (e.isKey && e.character == 'd')//删除选中的GameObject
        {
            foreach (GameObject obj in Selection.gameObjects)
            {
                Debug.Log(obj.name);
                DestroyImmediate(obj);
            }
        }
        else if (e.isKey && e.character == '1')
        {
            GameObject obj;
            if (Selection.activeObject)
            {
                Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
                Vector3 mousePos = r.origin;//mousepos向量保存射线的来源
                //找到Prefab
                Object prefab = PrefabUtility.GetPrefabParent(Selection.activeObject);
                if (prefab)
                {
                    obj = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                    Vector3 aligned = new Vector3(Mathf.Floor(mousePos.x / grid.width) * grid.width + grid.width / 2.0f,
                        Mathf.Floor(mousePos.y / grid.height) * grid.height + grid.height / 2.0f,
                        0.0f);

                    obj.transform.position = aligned;
                    //注册撤消操作
                    Undo.RegisterCreatedObjectUndo(obj, "Create" + obj.name);
                    Debug.Log("create");
                }
            }
        }
        //撤消单个对象实例
        else if (e.isKey && e.character == '4')
        {
            GameObject obj;
            if (Selection.activeObject)
            {
                Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
                Vector3 mousePos = r.origin;//mousepos向量保存射线的来源
                //找到Prefab
                Object prefab = PrefabUtility.GetPrefabParent(Selection.activeObject);
                if (prefab)
                {
                    Undo.IncrementCurrentGroup();
                    obj = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                    Vector3 aligned = new Vector3(Mathf.Floor(mousePos.x / grid.width) * grid.width + grid.width / 2.0f,
                        Mathf.Floor(mousePos.y / grid.height) * grid.height + grid.height / 2.0f,
                        0.0f);

                    obj.transform.position = aligned;
                    //注册撤消操作
                    Undo.RegisterCreatedObjectUndo(obj, "Create" + obj.name);
                    Debug.Log("create");
                }
            }
        }
    }
}

 

参考资料

http://code.tutsplus.com/tutorials/how-to-add-your-own-tools-to-unitys-editor--active-10047


本文转自赵青青博客园博客,原文链接:http://www.cnblogs.com/zhaoqingqing/p/3801342.html,如需转载请自行联系原作者

相关文章
UG01---NX10头部是标题栏 ,界面认识,进入全屏点击右下角,继续点击就退出了
UG01---NX10头部是标题栏 ,界面认识,进入全屏点击右下角,继续点击就退出了
窗口置前的几种方法
函数功能:该函数改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序。子窗口,弹出式窗口,及顶层窗口根据它们在屏幕上出现的顺序排序、顶层窗口设置的级别最高,并且被设置为Z序的第一个窗口。
AS中,使用CTRL+SHIFT+BS返回上一个编辑位置
AS中,使用CTRL+SHIFT+BS返回上一个编辑位置
161 0
VC下利用Picture控件实现png图片按钮(鼠标初始、按下、松开、获得焦点四种状态)
VC下利用Picture控件实现png图片按钮(鼠标初始、按下、松开、获得焦点四种状态)
132 0