|
Inspector视图中的所有组件,如果想展开必须用鼠标点击一下左上角的小箭头。昨天有朋友问我能不能通过脚本来动态展开或者关闭。

可以用反射来做直接上代码~
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
27
|
using
UnityEngine;
using
System.Collections;
using
UnityEditor;
using
System.Reflection;
using
System;
public
class
MyEditor
:
Editor
{
[MenuItem("Test/Test")]
static
void
Start
()
{
var
type
=
typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow");
var
window
=
EditorWindow.GetWindow(type);
FieldInfo
info
=
type.GetField("m_Tracker",
BindingFlags.NonPublic
|
BindingFlags.Instance);
ActiveEditorTracker
tracker
=
info.GetValue(window)as
ActiveEditorTracker;
for(int
i
=
0;
i<
tracker.activeEditors.Length;
i++)
{
//可以通过名子单独判断组件展开或不展开
if(tracker.activeEditors[i].target.GetType().Name
!=
"NewBehaviourScript")
{
//这里1就是展开,0就是合起来
tracker.SetVisible(i,1);
}
}
}
}
|
如下图所示,我的组件都展开了。 OK!
我又发现了一种不用反射的写法。。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[MenuItem(
"CONTEXT/Component/Fold All"
)]
public
static
void
FoldAll(
MenuCommand
command
)
{
ActiveEditorTracker
editorTracker
=
ActiveEditorTracker.sharedTracker;
Editor[]
editors
=
editorTracker.activeEditors;
bool
areAllFolded
=
true;
for(
int
i
=
1;
i
<
editors.Length;
i++
)
{
if(
editorTracker.GetVisible(
i
)
!=
0
)
{
areAllFolded
=
false;
break;
}
}
for(
int
i
=
1;
i
<
editors.Length;
i++
)
{
editorTracker.SetVisible(
i,
areAllFolded
?
1
:
0
);
}
}
|
|