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 ...
随机推荐
- UNIX 网络编程第三版
第五章p102: ps -t pts/6 -o pid,ppid,tty,stat,args,wchan 在我的系统上运行时出现:TTY not found linux发行版为mint17.1 改用 ...
- LeetCode 笔记系列 19 Scramble String [合理使用递归]
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- php大力力 [022节]php编程要有一种态度:渴望遇见麻烦
2015-08-27 php大力力022.php编程要有一种态度:渴望遇见麻烦 不能一遇到问题和麻烦,就烦躁焦躁. 写程序,写代码,调试实验就是天天遇见不可预期的错误bug,这是常态.老生常谈,要适应 ...
- phpstorm-file watcher
在项目中使用了sass,将scss编译成css的时候,每次都需要compass watch netbeans产品带有file watcher功能 三大类 1,less,scss,sass into c ...
- iOS开发:icon和启动图尺寸
歪果仁的总结: Asset iPhone 6s Plus and iPhone 6 Plus (@3x) iPhone 6s, iPhone 6, and iPhone 5 (@2x) iPhone ...
- LCA(倍增)
type arr=record v,nt:longint; end; ; lx=; ..maxn] of longint; eg:..maxn*] of arr; d:..maxn] of longi ...
- HDU 4396
http://acm.hdu.edu.cn/showproblem.php?pid=4396 题意:在至少走k条边的前提下求最短路 思路:在原有最短路模板的基础上多加一维,dis[i][j]表示走到i ...
- 【转】Entity Systems
“Favour composition over inheritance” If you haven’t already read my previous post on the problems o ...
- 中文圣经 for Android
中文圣经(For Android) 目前,中文圣经App包含了如下圣经版本: 和合本 现代中文译本 吕振中译本 中文新译本 英文标准本(ESV) King James Version(KJV) New ...
- Selenium - IWebDriver 控制scroll bar到底部
有时候我们需要控制页面滚动条上的滚动条,但滚动条并非页面上的元素,这个时候就需要借助js是来进行操作.一般用到操作滚动条的会两个场景: 注册时的法律条文需要阅读,判断用户是否阅读的标准是:滚动条是否拉 ...