ParticlePathEditor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Reflection;
  4. using System.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using UnityEditor.SceneManagement;
  8. [CustomEditor(typeof(ParticleSystem))]
  9. public class ParticlePathEditor : Editor
  10. {
  11. private ParticleSystem _particleSystem;
  12. private Assembly _assembly;
  13. private Type _particleSystemInspector;
  14. private MethodInfo _onInspectorGUI;
  15. private Editor _particleSystemEditor;
  16. public ParticlePath _particlePath;
  17. private int _currentCheckedPoint;
  18. private GUIStyle disableStyle;
  19. private void OnEnable()
  20. {
  21. _particleSystem = target as ParticleSystem;
  22. //载入程序集
  23. _assembly = Assembly.GetAssembly(typeof(Editor));
  24. //获取ParticleSystemInspector类
  25. _particleSystemInspector = _assembly.GetTypes().Where(t => t.Name == "ParticleSystemInspector").FirstOrDefault();
  26. //获取OnInspectorGUI方法
  27. _onInspectorGUI = _particleSystemInspector.GetMethod("OnInspectorGUI", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  28. //创建ParticleSystemInspector的实例
  29. _particleSystemEditor = CreateEditor(target, _particleSystemInspector);
  30. _particlePath = _particleSystem.gameObject.GetComponent<ParticlePath>();
  31. _currentCheckedPoint = -1;
  32. if (!_particlePath)
  33. {
  34. _particlePath = _particleSystem.gameObject.AddComponent<ParticlePath>();
  35. _particlePath.IsApprove = true;
  36. _particlePath.IsBezier = false;
  37. _particlePath.hideFlags = HideFlags.HideInInspector;
  38. _particlePath.Waypoints = new List<Vector3>();
  39. _particlePath.IsHideInInspector = false;
  40. _particlePath.PS = _particleSystem;
  41. }
  42. if (!_particlePath.IsApprove)
  43. {
  44. DestroyImmediate(_particlePath);
  45. _particlePath = _particleSystem.gameObject.AddComponent<ParticlePath>();
  46. _particlePath.IsApprove = true;
  47. _particlePath.IsBezier = false;
  48. _particlePath.hideFlags = HideFlags.HideInInspector;
  49. _particlePath.Waypoints = new List<Vector3>();
  50. _particlePath.IsHideInInspector = false;
  51. _particlePath.PS = _particleSystem;
  52. }
  53. }
  54. public override void OnInspectorGUI()
  55. {
  56. bool isChange = false;
  57. if (disableStyle == null)
  58. {
  59. foreach (GUIStyle style in GUI.skin.customStyles)
  60. {
  61. if (style.name == "toolbarbutton")
  62. {
  63. disableStyle = new GUIStyle(style);
  64. disableStyle.normal.textColor = Color.gray;
  65. break;
  66. }
  67. }
  68. }
  69. EditorGUILayout.BeginVertical("HelpBox");
  70. EditorGUILayout.BeginHorizontal();
  71. GUI.color = _particlePath.IsPath ? Color.white : Color.gray;
  72. _particlePath.IsPath = GUILayout.Toggle(_particlePath.IsPath, "", GUILayout.Width(25));
  73. if (GUILayout.Button("Path Mode", "label"))
  74. {
  75. _particlePath.IsHideInInspector = !_particlePath.IsHideInInspector;
  76. }
  77. GUI.enabled = _particlePath.IsPath;
  78. EditorGUILayout.EndHorizontal();
  79. if (!_particlePath.IsHideInInspector)
  80. {
  81. for (int i = 0; i < _particlePath.Waypoints.Count; i++)
  82. {
  83. EditorGUILayout.BeginHorizontal();
  84. GUI.backgroundColor = _currentCheckedPoint == i ? Color.cyan : Color.white;
  85. if (GUILayout.Button("Path " + (i + 1), "toolbarbutton"))
  86. {
  87. _currentCheckedPoint = i;
  88. }
  89. Vector3 pos = _particlePath.Waypoints[i];
  90. pos.x = Convert.ToSingle(GUILayout.TextField(pos.x.ToString(), GUILayout.Width(50)));
  91. pos.y = Convert.ToSingle(GUILayout.TextField(pos.y.ToString(), GUILayout.Width(50)));
  92. pos.z = Convert.ToSingle(GUILayout.TextField(pos.z.ToString(), GUILayout.Width(50)));
  93. if (pos.x != _particlePath.Waypoints[i].x ||
  94. pos.y != _particlePath.Waypoints[i].y ||
  95. pos.z != _particlePath.Waypoints[i].z)
  96. {
  97. _particlePath.Waypoints[i] = pos;
  98. }
  99. if (i != 0)
  100. {
  101. if (GUILayout.Button("↑", "toolbarbutton", GUILayout.Width(20)))
  102. {
  103. Vector3 vec = _particlePath.Waypoints[i];
  104. _particlePath.Waypoints[i] = _particlePath.Waypoints[i - 1];
  105. _particlePath.Waypoints[i - 1] = vec;
  106. }
  107. }
  108. else
  109. {
  110. GUILayout.Box("↑", disableStyle, GUILayout.Width(20) );
  111. }
  112. if (i != _particlePath.Waypoints.Count - 1)
  113. {
  114. if (GUILayout.Button("↓", "toolbarbutton", GUILayout.Width(20)))
  115. {
  116. Vector3 vec = _particlePath.Waypoints[i];
  117. _particlePath.Waypoints[i] = _particlePath.Waypoints[i + 1];
  118. _particlePath.Waypoints[i + 1] = vec;
  119. }
  120. }
  121. else
  122. {
  123. GUILayout.Box("↓", disableStyle, GUILayout.Width(20));
  124. }
  125. if (GUILayout.Button("-", "toolbarbutton", GUILayout.Width(20)))
  126. {
  127. _particlePath.Waypoints.RemoveAt(i);
  128. _currentCheckedPoint = -1;
  129. break;
  130. }
  131. EditorGUILayout.EndHorizontal();
  132. }
  133. EditorGUILayout.BeginHorizontal();
  134. GUI.backgroundColor = Color.white;
  135. GUILayout.FlexibleSpace();
  136. if (GUILayout.Button("", "OL Plus"))
  137. {
  138. if (_currentCheckedPoint != -1)
  139. {
  140. _particlePath.Waypoints.Add(_particlePath.Waypoints[_currentCheckedPoint]);
  141. }
  142. else
  143. {
  144. _particlePath.Waypoints.Add(_particlePath.transform.position);
  145. }
  146. }
  147. EditorGUILayout.EndHorizontal();
  148. EditorGUILayout.BeginHorizontal();
  149. GUILayout.Label("Line segment:");
  150. _particlePath.IsBezier = !GUILayout.Toggle(!_particlePath.IsBezier, "Line");
  151. _particlePath.IsBezier = GUILayout.Toggle(_particlePath.IsBezier, "Bezier");
  152. EditorGUILayout.EndHorizontal();
  153. EditorGUILayout.BeginHorizontal();
  154. GUILayout.Label("Move speed:");
  155. _particlePath.Speed = EditorGUILayout.Slider(_particlePath.Speed, 10f, 0.05f);
  156. EditorGUILayout.EndHorizontal();
  157. EditorGUILayout.BeginHorizontal();
  158. GUILayout.Label("Camera follow:");
  159. _particlePath.IsCameraFollow = GUILayout.Toggle(_particlePath.IsCameraFollow, "Start camera follow");
  160. EditorGUILayout.EndHorizontal();
  161. EditorGUILayout.BeginHorizontal();
  162. GUILayout.Label("Camera speed:");
  163. _particlePath.CameraSpeed= EditorGUILayout.Slider(_particlePath.CameraSpeed, 100f, 0.05f);
  164. EditorGUILayout.EndHorizontal();
  165. EditorGUILayout.BeginHorizontal();
  166. GUILayout.Label("Camera wait time(second):");
  167. _particlePath.CameraWaitTime = EditorGUILayout.Slider(_particlePath.CameraWaitTime, 60f, 0f);
  168. EditorGUILayout.EndHorizontal();
  169. }
  170. EditorGUILayout.EndVertical();
  171. GUI.color = Color.white;
  172. GUI.enabled = true;
  173. _onInspectorGUI.Invoke(_particleSystemEditor, null);
  174. #if UNITY_EDITOR
  175. if (GUI.changed)
  176. {
  177. //EditorUtility.SetDirty(_particlePath);
  178. EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  179. }
  180. #endif
  181. }
  182. private void OnSceneGUI()
  183. {
  184. if (_particlePath.IsPath)
  185. {
  186. Handles.color = Color.cyan;
  187. //直线模式
  188. if (_particlePath.IsBezier == false)
  189. {
  190. for (int i = 0; i < _particlePath.Waypoints.Count; i++)
  191. {
  192. if (i < _particlePath.Waypoints.Count - 1)
  193. {
  194. Handles.DrawLine(_particlePath.Waypoints[i], _particlePath.Waypoints[i + 1]);
  195. }
  196. }
  197. }
  198. //贝塞尔模式
  199. if (_particlePath.IsBezier)
  200. {
  201. Vector3[] path = _particlePath.Waypoints.ToArray();
  202. Vector3[] vector3s = PathControlPointGenerator(path);
  203. Vector3 prevPt = Interp(vector3s, 0);
  204. int SmoothAmount = _particlePath.Waypoints.Count * _particlePath.BezierSmoothSens;
  205. for (int i = 1; i <= SmoothAmount; i++)
  206. {
  207. float pm = (float)i / SmoothAmount;
  208. //Debug.Log(pm);
  209. Vector3 currPt = Interp(vector3s, pm);
  210. Handles.DrawLine(currPt, prevPt);
  211. prevPt = currPt;
  212. }
  213. }
  214. if (_currentCheckedPoint != -1 && _currentCheckedPoint < _particlePath.Waypoints.Count)
  215. {
  216. Tools.current = Tool.None;
  217. Vector3 oldVec = _particlePath.Waypoints[_currentCheckedPoint];
  218. Vector3 newVec = Handles.PositionHandle(oldVec, Quaternion.identity);
  219. if (oldVec != newVec)
  220. {
  221. _particlePath.Waypoints[_currentCheckedPoint] = newVec;
  222. }
  223. Handles.Label(newVec, "Path " + (_currentCheckedPoint + 1));
  224. EditorUtility.SetDirty(_particlePath);
  225. }
  226. }
  227. }
  228. //贝塞尔曲线函数
  229. public Vector3[] PathControlPointGenerator(Vector3[] path)
  230. {
  231. Vector3[] suppliedPath;
  232. Vector3[] vector3s;
  233. //create and store path points:
  234. suppliedPath = path;
  235. //populate calculate path;
  236. int offset = 2;
  237. vector3s = new Vector3[suppliedPath.Length + offset];
  238. Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length);
  239. //populate start and end control points:
  240. //vector3s[0] = vector3s[1] - vector3s[2];
  241. vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
  242. vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]);
  243. //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
  244. if (vector3s[1] == vector3s[vector3s.Length - 2])
  245. {
  246. Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
  247. Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
  248. tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];
  249. tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];
  250. vector3s = new Vector3[tmpLoopSpline.Length];
  251. Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
  252. }
  253. return (vector3s);
  254. }
  255. public Vector3 Interp(Vector3[] pts, float t)
  256. {
  257. int numSections = pts.Length - 3;
  258. int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);
  259. float u = t * (float)numSections - (float)currPt;
  260. Vector3 a = pts[currPt];
  261. Vector3 b = pts[currPt + 1];
  262. Vector3 c = pts[currPt + 2];
  263. Vector3 d = pts[currPt + 3];
  264. return .5f * (
  265. (-a + 3f * b - 3f * c + d) * (u * u * u)
  266. + (2f * a - 5f * b + 4f * c - d) * (u * u)
  267. + (-a + c) * u
  268. + 2f * b
  269. );
  270. }
  271. }