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
;
}
}
|
- 本文固定链接: https://www.xuanyusong.com/archives/3900
- 转载请注明: 雨松MOMO 2016年02月05日 于 雨松MOMO程序研究院 发表