-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticlePathEditor.cs
More file actions
165 lines (151 loc) · 6.24 KB
/
ParticlePathEditor.cs
File metadata and controls
165 lines (151 loc) · 6.24 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
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System.Linq;
using System;
using System.Collections.Generic;
///
///放在editor目录下
///可视化控制粒子移动方向,可以在sence界面直接选择粒子移动的点。
[CustomEditor(typeof(ParticleSystem))]
public class ParticlePathEditor : Editor
{
private ParticleSystem _particleSystem;
private Assembly _assembly;
private Type _particleSystemInspector;
private MethodInfo _onInspectorGUI;
private Editor _particleSystemEditor;
private ParticlePath _particlePath;
private int _currentCheckedPoint;
private void OnEnable()
{
_particleSystem = target as ParticleSystem;
//载入程序集
_assembly = Assembly.GetAssembly(typeof(Editor));
//获取ParticleSystemInspector类
_particleSystemInspector = _assembly.GetTypes().Where(t => t.Name == "ParticleSystemInspector").FirstOrDefault();
//获取OnInspectorGUI方法
_onInspectorGUI = _particleSystemInspector.GetMethod("OnInspectorGUI", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
//创建ParticleSystemInspector的实例
_particleSystemEditor = CreateEditor(target, _particleSystemInspector);
_particlePath = _particleSystem.gameObject.GetComponent<ParticlePath>();
_currentCheckedPoint = -1;
if (!_particlePath)
{
_particlePath = _particleSystem.gameObject.AddComponent<ParticlePath>();
_particlePath.IsApprove = true;
_particlePath.hideFlags = HideFlags.HideInInspector;
_particlePath.Waypoints = new List<Vector3>();
_particlePath.IsHideInInspector = false;
_particlePath.PS = _particleSystem;
}
if (!_particlePath.IsApprove)
{
DestroyImmediate(_particlePath);
_particlePath = _particleSystem.gameObject.AddComponent<ParticlePath>();
_particlePath.IsApprove = true;
_particlePath.hideFlags = HideFlags.HideInInspector;
_particlePath.Waypoints = new List<Vector3>();
_particlePath.IsHideInInspector = false;
_particlePath.PS = _particleSystem;
}
}
public override void OnInspectorGUI()
{
EditorGUILayout.BeginVertical("HelpBox");
EditorGUILayout.BeginHorizontal();
GUI.color = _particlePath.IsPath ? Color.white : Color.gray;
_particlePath.IsPath = GUILayout.Toggle(_particlePath.IsPath, "", GUILayout.Width(25));
if (GUILayout.Button("路径模式", "label"))
{
_particlePath.IsHideInInspector = !_particlePath.IsHideInInspector;
}
GUI.enabled = _particlePath.IsPath;
EditorGUILayout.EndHorizontal();
if (!_particlePath.IsHideInInspector)
{
for (int i = 0; i < _particlePath.Waypoints.Count; i++)
{
EditorGUILayout.BeginHorizontal();
GUI.backgroundColor = _currentCheckedPoint == i ? Color.cyan : Color.white;
if (GUILayout.Button("路径点" + (i + 1), "toolbarbutton"))
{
_currentCheckedPoint = i;
}
if (i != 0)
{
if (GUILayout.Button("↑", "toolbarbutton", GUILayout.Width(20)))
{
Vector3 vec = _particlePath.Waypoints[i];
_particlePath.Waypoints[i] = _particlePath.Waypoints[i - 1];
_particlePath.Waypoints[i - 1] = vec;
}
}
if (i != _particlePath.Waypoints.Count - 1)
{
if (GUILayout.Button("↓", "toolbarbutton", GUILayout.Width(20)))
{
Vector3 vec = _particlePath.Waypoints[i];
_particlePath.Waypoints[i] = _particlePath.Waypoints[i + 1];
_particlePath.Waypoints[i + 1] = vec;
}
}
if (GUILayout.Button("-", "toolbarbutton", GUILayout.Width(20)))
{
_particlePath.Waypoints.RemoveAt(i);
_currentCheckedPoint = -1;
break;
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.BeginHorizontal();
GUI.backgroundColor = Color.white;
GUILayout.FlexibleSpace();
if (GUILayout.Button("", "OL Plus"))
{
if (_currentCheckedPoint != -1)
{
_particlePath.Waypoints.Add(_particlePath.Waypoints[_currentCheckedPoint]);
}
else
{
_particlePath.Waypoints.Add(_particlePath.transform.position);
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Speed", "MiniLabel");
_particlePath.Speed = EditorGUILayout.Slider(_particlePath.Speed, 10f, 0.5f);
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
GUI.color = Color.white;
GUI.enabled = true;
_onInspectorGUI.Invoke(_particleSystemEditor, null);
}
private void OnSceneGUI()
{
if (_particlePath.IsPath)
{
Handles.color = Color.cyan;
for (int i = 0; i < _particlePath.Waypoints.Count; i++)
{
if (i < _particlePath.Waypoints.Count - 1)
{
Handles.DrawLine(_particlePath.Waypoints[i], _particlePath.Waypoints[i + 1]);
}
}
if (_currentCheckedPoint != -1 && _currentCheckedPoint < _particlePath.Waypoints.Count)
{
Tools.current = Tool.None;
Vector3 oldVec = _particlePath.Waypoints[_currentCheckedPoint];
Vector3 newVec = Handles.PositionHandle(oldVec, Quaternion.identity);
if (oldVec != newVec)
{
_particlePath.Waypoints[_currentCheckedPoint] = newVec;
}
Handles.Label(newVec, "路径点" + (_currentCheckedPoint + 1));
}
}
}
}