-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathParticleSystemGameObjectEditor.cs
More file actions
201 lines (175 loc) · 5.7 KB
/
ParticleSystemGameObjectEditor.cs
File metadata and controls
201 lines (175 loc) · 5.7 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditor;
namespace ToolKits
{
[CustomEditor(typeof(GameObject)), CanEditMultipleObjects]
public class ParticleSystemGameObjectEditor : OverrideEditor
{
private class Styles
{
public GUIContent ps = new GUIContent("PS", "Show particle system preview");
public GUIStyle preButton = "preButton";
}
private bool m_ShowParticlePreview;
private int m_DefaultHasPreview;
private ParticleSystemPreview m_Preview;
private static Styles s_Styles;
private ParticleSystemPreview preview
{
get
{
if (m_Preview == null)
{
m_Preview = new ParticleSystemPreview();
m_Preview.SetEditor(this);
m_Preview.Initialize(targets);
}
return m_Preview;
}
}
protected override Editor GetBaseEditor()
{
Editor editor = null;
var baseType = typeof(Editor).Assembly.GetType("UnityEditor.GameObjectInspector");
CreateCachedEditor(targets, baseType, ref editor);
return editor;
}
void OnEnable()
{
m_ShowParticlePreview = true;
}
void OnDisable()
{
preview.OnDestroy();
if (null != baseEditor&& IsPreviewCacheNotNull)
{
DestroyImmediate(baseEditor);
baseEditor = null;
}
}
private bool HasParticleSystemPreview()
{
return preview.HasPreviewGUI();
}
private bool HasBasePreview()
{
if (m_DefaultHasPreview == 0)
{
m_DefaultHasPreview = baseEditor.HasPreviewGUI() ? 1 : -1;
}
return m_DefaultHasPreview == 1;
}
private bool IsShowParticleSystemPreview()
{
return HasParticleSystemPreview() && m_ShowParticlePreview;
}
public override bool HasPreviewGUI()
{
return HasParticleSystemPreview() || HasBasePreview();
}
public override GUIContent GetPreviewTitle()
{
return IsShowParticleSystemPreview() ? preview.GetPreviewTitle() : baseEditor.GetPreviewTitle();
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
if (IsShowParticleSystemPreview())
{
preview.OnPreviewGUI(r, background);
}
else
{
baseEditor.OnPreviewGUI(r, background);
}
}
public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
{
if (IsShowParticleSystemPreview())
{
preview.OnInteractivePreviewGUI(r, background);
}
else
{
baseEditor.OnInteractivePreviewGUI(r, background);
}
}
public override void OnPreviewSettings()
{
if (s_Styles == null)
{
s_Styles = new Styles();
}
if (HasBasePreview() && HasParticleSystemPreview())
{
m_ShowParticlePreview = GUILayout.Toggle(m_ShowParticlePreview, s_Styles.ps, s_Styles.preButton);
}
if (IsShowParticleSystemPreview())
{
preview.OnPreviewSettings();
}
else
{
baseEditor.OnPreviewSettings();
}
}
public override string GetInfoString()
{
return IsShowParticleSystemPreview() ? preview.GetInfoString() : baseEditor.GetInfoString();
}
public override void ReloadPreviewInstances()
{
if (IsShowParticleSystemPreview())
{
preview.ReloadPreviewInstances();
}
else
{
baseEditor.ReloadPreviewInstances();
}
}
#if UNITY_2020_2_OR_NEWER
public void OnSceneDrag(SceneView sceneView, int index)
{
System.Type t = baseEditor.GetType();
MethodInfo onSceneDragMi = t.GetMethod("OnSceneDrag",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (onSceneDragMi != null)
{
object[] param = new object[] {sceneView, index};
onSceneDragMi.Invoke(baseEditor, param);
}
}
#else
// /// <summary>
// /// 需要调用 GameObjectInspector 的场景拖曳,否则无法拖动物体到 Scene 视图
// /// </summary>
// /// <param name="sceneView"></param>
public void OnSceneDrag(SceneView sceneView)
{
System.Type t = baseEditor.GetType();
MethodInfo onSceneDragMi = t.GetMethod("OnSceneDrag",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (onSceneDragMi != null)
{
onSceneDragMi.Invoke(baseEditor, new object[1] {sceneView});
}
}
#endif
private bool IsPreviewCacheNotNull
{
get
{
System.Type t = baseEditor.GetType();
var fieldInfo = t.GetField("m_PreviewCache", BindingFlags.NonPublic | BindingFlags.Instance);
if (null != fieldInfo)
{
var value = fieldInfo.GetValue(baseEditor);
return null != value;
}
return false;
}
}
}
}