[cb]SceneView 获取鼠标位置

时间:2021-03-25 10:20:04

扩展需求

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

Demo

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

[cb]SceneView 获取鼠标位置

重点部分

[cb]SceneView 获取鼠标位置 [cb]SceneView 获取鼠标位置

[cb]SceneView 获取鼠标位置

[cb]SceneView 获取鼠标位置

实现代码

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