Unity3D研究院编辑器之脚本设置ToolBar
Unity版本5.3.2
如下图所示,ToolBar就是Unity顶部的那一横条。这里的所有按钮一般情况下都得我们手动的用鼠标去点击。这篇文章我们说说如果自动操作它们
1.自动点击左边四个按钮 (拖动、坐标、旋转、缩放、矩形)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[MenuItem("ToolBar/Tools")]
static void ToolsMethod()
{
if(Tools.current == Tool.View)
Tools.current = Tool.Move;
else if(Tools.current == Tool.Move)
Tools.current = Tool.Rotate;
else if(Tools.current == Tool.Rotate)
Tools.current = Tool.Scale;
else if(Tools.current == Tool.Scale)
Tools.current = Tool.Rect;
else if(Tools.current == Tool.Rect)
Tools.current = Tool.View;
}
|
那么如果我不像自动设置,只是想监听手动点击的事件如何?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[InitializeOnLoadMethod]
static void StartInitializeOnLoadMethod()
{
SceneView.onSceneGUIDelegate = delegate(SceneView sceneView) {
if (Event.current != null && Event.current.button == 1 && Event.current.type <= EventType.mouseUp)
{
Vector2 mousePosition = Event.current.mousePosition;
EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x, mousePosition.y, 0, 0), "ToolBar/",null);
Event.current.Use();
}
};
Type t = typeof(Tools);
FieldInfo info =t.GetField ("onToolChanged", BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
//这段代码所属的类名, 请把这个换成你的类名
MethodInfo method = typeof(MyEditor).GetMethod ("OnToolChangedFunc", BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
Delegate d = Delegate.CreateDelegate(info.FieldType,method);
info.SetValue(null,d);
}
static void OnToolChangedFunc(Tool from, Tool to)
{
//这里还可以得到from 和 to 也就是从哪里到哪里
Debug.Log(Tools.current +" ");
}
|
OK.如下图所示,当我切换标签的时候,就会输出切换的事件。

2.自动点击 Center Local 这两个辅助按钮
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[MenuItem("ToolBar/pivotMode")]
static void pivotMode()
{
Tools.pivotMode = (Tools.pivotMode == PivotMode.Center)? PivotMode.Pivot : PivotMode.Center;
Refresh();
}
[MenuItem("ToolBar/pivotRotation")]
static void pivotRotation()
{
Tools.pivotRotation = (Tools.pivotRotation == PivotRotation.Global)? PivotRotation.Local : PivotRotation.Global;
Refresh();
}
static void Refresh()
{
MethodInfo info = typeof(Tools).GetMethod("RepaintAllToolViews",BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
info.Invoke(null,null);
}
|
3.自动点击 Play Pause Step 这三个播放按钮
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[MenuItem("ToolBar/Play")]
static void Play()
{
EditorApplication.ExecuteMenuItem("Edit/Play");
}
[MenuItem("ToolBar/Pause")]
static void Pause()
{
EditorApplication.ExecuteMenuItem("Edit/Pause");
}
[MenuItem("ToolBar/Step")]
static void Step()
{
EditorApplication.ExecuteMenuItem("Edit/Step");
}
|
还是,那么如果我不像自动设置,只是想监听手动点击的事件如何?这样就可以监听了。
|
1
2
3
4
5
6
7
8
9
10
|
[InitializeOnLoadMethod]
static void StartInitializeOnLoadMethod()
{
EditorApplication.playmodeStateChanged = delegate() {
Debug.Log("isPlaying :" + EditorApplication.isPlaying);
Debug.Log("isPaused :" +EditorApplication.isPaused);
Debug.Log("isPlayingOrWillChangePlaymode :" +EditorApplication.isPlayingOrWillChangePlaymode);
};
}
|
4.云 Account 按钮
这是云端账号系统,应该没有自动的需求吧?
5.Layers 按钮
这是设置游戏中所有层级的菜单。“小眼睛”表示是否在Scene视图中看见。“小锁头”表示是否可以在Scene视图中被鼠标选择到。 具体 内容大家可以看我之前的文章 #你好Unity3D#限制SceneView视图中不可选择游戏对象

自动设置的代码
|
1
2
3
4
5
6
|
[MenuItem("ToolBar/SetLayer")]
static void SetLayer()
{
Tools.visibleLayers = (1<< LayerMask.NameToLayer("UI")) | (1<< LayerMask.NameToLayer("Default"));
Tools.lockedLayers = (1<< LayerMask.NameToLayer("UI")) | (1<< LayerMask.NameToLayer("Default"));
}
|
6.Layout布局按钮 自动设置的代码如下
|
1
2
3
4
5
6
|
[MenuItem("ToolBar/SetLayout")]
static void SetLayout()
{
//把对应layout的名子填正确就行
EditorApplication.ExecuteMenuItem("Window/Layouts/2 by 3");
}
|
7.今天有朋友问我如何在SceneView通过脚本设置成顶视图,其实 上、下、左、右、前、后都可以通过脚本设置的。代码如下。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[MenuItem("GameObject/Top View")]
static void Start()
{
SceneView view = SceneView.lastActiveSceneView;
if(view)
{
view.rotation = Quaternion.Euler(90f, 0f, 0f);
view.pivot = Vector3.zero;
view.size = 5f;
view.orthographic = true;
}
}
|
- 本文固定链接: http://www.xuanyusong.com/archives/3900
- 转载请注明: 雨松MOMO 2016年02月05日 于 雨松MOMO程序研究院 发表
Unity3D研究院编辑器之脚本设置ToolBar的更多相关文章
- Unity3D研究院编辑器之脚本设置ToolBar及脚本设置顶视图
Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...
- Unity3D研究院编辑器之脚本获取资源内存和硬盘大小
内存 使用Profiler可以查看某个资源的内存占用情况,但是必须启动游戏,并且待查看的资源已经载入游戏中.我希望的是不启动游戏,也能看到它的内存好做统计. 硬盘 由于unity中的资源压缩格式记录在 ...
- 《转》Unity3D研究院编辑器之创建Lua脚本模板
Unity里能创建 c#脚本模板,但是如果我想创建Lua脚本模板怎么办呢?拓展一下编辑器吧. 设置一下Lua脚本的模板地址 : Assets/Editor/Lua/Template/lua.lua ...
- [Unity] Unity3D研究院编辑器之自定义默认资源的Inspector面板
比如编辑模式下对场景或者特定文件夹有一些操作可以在这个面板里来完成.. 代码如下. using UnityEngine; using System.Collections; using UnityEd ...
- Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五)
http://www.xuanyusong.com/archives/3727 感谢楼下的牛逼回复更正一下,我表示我也是才知道.. 其实不需要实例化也能查找,你依然直接用GetComponentsIn ...
- Unity3D研究院编辑器之Editor的GUI的事件拦截
OnGUI是Unity上一个时代的UI系统,而现在运行时的UI系统已经被UGUI取代,但是Editor的UI还是在用老的这一套GUI系统.比如unity编辑器里的所有窗口,布局,按钮,拖动条.滚动等等 ...
- Unity3D研究院编辑器之不影响原有布局拓展Inspector
今天无意间发现了一篇好文章,也让我解决了一个很久都没解决的难题.问题是这样的,假如我想去拓展Unity自带的inspector但是并不想影响原有布局. 比如下面这段代码: 1 2 3 4 5 ...
- [Unity] Unity3D研究院编辑器之独立Inspector属性
本文转自: http://www.xuanyusong.com/archives/3680雨松MOMO Unity提供了强大的Editor功能, 我们可以很轻易的在EditorGUI中绘制任意的属性. ...
- Unity3D研究院编辑器之重写Hierarchy的右键菜单
Hierarchy视图中选择一个游戏对象以后通过右键可以打开一个unity默认菜单,一般情况下都可以满足我们,但是我想真对某些特殊的游戏对象而展开特殊的菜单.如下图所示,比如这样: 代码: using ...
随机推荐
- Listview之优化BaseAdapter中的getView中的contentView
BaseAdapter中getView中改动的地方: @Override public View getView(int position, View contentView, ViewGroup a ...
- Oracle普通索引,唯一索引,主键的区别
索引是我们经常使用的一种数据库优化手段,适当的业务操作场景使用适当的索引方案,可以显著的提升系统整体查询性能,当然用户体验也随之提高. 在Oracle中,唯一性索引(Unique Index)是我们经 ...
- droidbox官网
droidbox官网,droidbox已经移植到github上了 https://github.com/pjlantz/droidbox
- (转) mysql的连接,创建账号,修改密码
原文:http://blog.chinaunix.net/uid-20749043-id-1878306.html mysql的连接,创建账号,修改密码 2008-10-13 15:31:29 分类 ...
- 2016 - 1- 22 img tag and the lists (intro to HMTL&CSS)
1 :The img tag img tag allows put some img file into page. just like : <a href = "me.png&quo ...
- linux下解决端口被占用的问题
以端口9877为例,先查看是否有进程占用该端口 netstat -tln | grep 9877 然后查看占用该端口的进程的进程id lsof -i :9877 最后kill 命令结束该进程: kil ...
- JS监听关闭浏览器事件
Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...
- VS2013 JS 跟踪
VS2013JS 跟踪 1.在页面对应的cs文件中设置断点,F10单步执行. 2.单步执行调试会自然而然执行到JS里面.
- yii2 ArrayHelper map 使用
<不喜勿喷> 引用类 use yii\helpers\ArrayHelper; 源码中修改(尽量不要修改,可以研究下不修改的方式) 源码路径 查看数据 视图层 实现效果
- PHP中的数组(二)常用数组处理函数
数组的相关处理函数 一.数组键/值操作有关的函数 1.array_values() 无论是关联的还是索引的返回的都是索引数组 <?php $lamp=array(&quo ...