扩展需求

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

Demo

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

重点部分

实现代码

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

[cb]SceneView 获取鼠标位置的更多相关文章

  1. jq获取鼠标位置

    jq获取鼠标位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. C++获取鼠标位置及全局检测鼠标行为

    1.获取鼠标位置(在屏幕的位置)  CPoint m_mouse; GetCursorPos(&m_mouse); 2. 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置 ...

  3. jquery 获取鼠标位置

    //获取鼠标位置 $(function(){ $('body').mousemove(function(e) { e = e || window.event; __xx = e.pageX || e. ...

  4. Adobe Edge Animate –获取鼠标位置及跟随鼠标功能实现

    Adobe Edge Animate –获取鼠标位置及跟随鼠标功能实现 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 在网络上浏览有关Edge相关问题的时 ...

  5. JavaScript获取鼠标位置的三种方法

    在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的游览器下会有不同的结果甚至是有的游览器下没结果,这篇文章就鼠标点击位置坐标获取做一些简单的总结. 获取鼠 ...

  6. C# 图像处理:获取鼠标位置信息(全局)

    Point ms = Control.MousePosition; //获取鼠标位置 this.label2.Text = string.Format("{0}:{1}", ms. ...

  7. 兼容IE FF 获取鼠标位置

    由于Firefox和IE等浏览器之间对js解释的方式不一样,firefox下面获取鼠标位置不能够直接使用clientX来获取.网上说的一般都是触发mousemove事件才行.我这里有两段代码,思路都一 ...

  8. canvas获取鼠标位置

    canvas获取鼠标位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  9. JS获取鼠标位置,兼容IE FF

    由于Firefox和IE等浏览器之间对js解释的方式不一样,firefox下面获取鼠标位置不能够直接使用clientX来获取.网上说的一般都是触发mousemove事件才行.我这里有两段代码,思路都一 ...

随机推荐

  1. 一些java多线程的经验

    多线程的时候,可以try--catch后再catch中加continue让程序继续运行(当然,前提是这个异常的数据不影响后续的操作)

  2. 21天打造分布式爬虫-Crawl类爬取小程序社区(八)

    8.1.Crawl的用法实战 新建项目 scrapy startproject wxapp scrapy genspider -t crawl wxapp_spider "wxapp-uni ...

  3. linux arm 交叉编译ACE(ubuntu16.04)

    解压ace包 tar zxvf ACE_6.1.0.tar.gz 在终端设置环境变量 sudo gedit /etc/profile 在打开的内容添加 export ACE_ROOT=/home/xx ...

  4. 快速排序——Quick Sort

    基本思想:(分治) 先从数列中取出一个数作为key值: 将比这个数小的数全部放在它的左边,大于或等于它的数全部放在它的右边: 对左右两个小数列重复第二步,直至各区间只有1个数. 辅助理解:挖坑填数 初 ...

  5. PostgreSQL Json字段作为查询条件案例

    业务扩展字段在数据库中经常会使用json格式的数据来存储,这就涉及到一个头疼的问题,假设要使用扩展字段里的某个值作为查询条件怎么办,原来PostgreSQL本身就支持这种查询方式. 例子:假设业务扩展 ...

  6. ckeditor 在dwz里面使用

    在ckeditor的配置的过程中,所有的配置的地方都配置了,但是就是不显示编辑器(编辑器代码如下),很郁闷哦 1 <textarea id="editor1" name=&q ...

  7. centos适用的国内yum源:网易、搜狐

    默认的yum源是centos官网的,速度慢是不用说了.所以使用yum安装东西之前需要把yum源改为国内的.参考 http://mirrors.163.com/.help/centos.html 和 h ...

  8. redhat 下搭建网站

    1.修改yum源 把iso重新挂载到/media路径下,media是个只读的文件 vi  /etc/yum.repos.d/rhel-source.repo            //编辑yum源文件 ...

  9. 常用的NoSQL数据库类型简述

    一.文档存储类型(Document Stores) 文档存储,也称为面向文档的数据库系统,其主要特点在于它们的无模式的数据组织. 特点: 1.记录数据不需要具有统一的结构,即不同的记录可以具有不同的列 ...

  10. SpringMVC4返回json

    前言 目前前后端分离后,接口大多数返回给前端的都是json数据,那么我尝试用SpringMVC4的Controller返回json.demo过程中遇到了几个问题写出来和java初学者分享一下. 开发环 ...