KunaiEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace Kunai
  7. {
  8. public class KunaiEditor : EditorWindow
  9. {
  10. Object obj;
  11. Object obj2;
  12. float flt2;
  13. string path;
  14. float scaleFloat = 1.00f;
  15. string notify;
  16. string tagString;
  17. float originalSpeed;
  18. float originalSize;
  19. Vector3 originalScale;
  20. float xSpeed = 0;
  21. float ySpeed = 0;
  22. float zSpeed = 0;
  23. KunaiRotation kunaiRotation;
  24. GameObject gObj;
  25. Color particleColor;
  26. List<GameObject> particlesList = new List<GameObject>();
  27. List<GameObject> tempList = new List<GameObject>();
  28. List<float> particleSizes = new List<float>();
  29. List<float> particleSpeeds = new List<float>();
  30. List<float> particleGravity = new List<float>();
  31. List<Vector3> particleScales = new List<Vector3>();
  32. List<Color> particleColors = new List<Color>();
  33. [MenuItem("Window/Kunai Editor")]
  34. public static void ShowWindow()
  35. {
  36. EditorWindow.GetWindow (typeof(KunaiEditor), false, "Kunai Editor");
  37. }
  38. void Update()
  39. {
  40. //Each time the object is changed;
  41. if (obj2 != obj && obj != null)
  42. {
  43. //If the Object has been changed, reset everything before getting new data
  44. AssetDatabase.Refresh();
  45. ResetScales ();
  46. ResetColors ();
  47. Clear ();
  48. AssetDatabase.Refresh();
  49. if(obj != null)
  50. {
  51. //If that something is a GameObject
  52. if (obj is GameObject)
  53. {
  54. //Cast it as a GameObject
  55. gObj = obj as GameObject;
  56. //If that GameObject has a ParticleSystem
  57. if (gObj.GetComponent<ParticleSystem>() != null)
  58. {
  59. notify = "Particle";
  60. particleColor = gObj.GetComponent<ParticleSystem>().startColor;
  61. particlesList.Add (gObj.gameObject);
  62. particleColors.Add (gObj.GetComponent<ParticleSystem>().startColor);
  63. particleSizes.Add (gObj.GetComponent<ParticleSystem>().startSize);
  64. particleSpeeds.Add (gObj.GetComponent<ParticleSystem>().startSpeed);
  65. particleGravity.Add (gObj.GetComponent<ParticleSystem>().gravityModifier);
  66. particleScales.Add (gObj.transform.localScale);
  67. }
  68. //If that gameObject has children
  69. if (gObj.transform.childCount > 0)
  70. {
  71. GetChildren(gObj);
  72. }
  73. if (gObj.transform.childCount == 0 && gObj.GetComponent<ParticleSystem>() == null)
  74. {
  75. notify = "Invalid Selection";
  76. }
  77. }
  78. //If the drag n dropped item is a folder
  79. else if (Directory.Exists(AssetDatabase.GetAssetPath(obj.GetInstanceID())))
  80. {
  81. string sAssetFolderPath = AssetDatabase.GetAssetPath(obj);
  82. string sDataPath = Application.dataPath;
  83. string sFolderPath = sDataPath.Substring(0 ,sDataPath.Length-6)+sAssetFolderPath;
  84. string[] aFilePaths = Directory.GetFiles(sFolderPath);
  85. foreach (string sFilePath in aFilePaths)
  86. {
  87. string sAssetPath = sFilePath.Substring(sDataPath.Length-6);
  88. Object temp = AssetDatabase.LoadAssetAtPath(sAssetPath,typeof(GameObject));
  89. tempList.Add (temp as GameObject);
  90. }
  91. //Find all GameObjects in folder and put them into an array
  92. foreach (GameObject temp in tempList)
  93. {
  94. if(temp != null)
  95. {
  96. //If the GameObject is a particle system put it in an array. Make arrays for size and speeds too
  97. if (temp.GetComponent<ParticleSystem>() != null)
  98. {
  99. particlesList.Add (temp);
  100. particleColors.Add (temp.GetComponent<ParticleSystem>().startColor);
  101. particleSizes.Add (temp.GetComponent<ParticleSystem>().startSize);
  102. particleSpeeds.Add (temp.GetComponent<ParticleSystem>().startSpeed);
  103. particleGravity.Add (temp.GetComponent<ParticleSystem>().gravityModifier);
  104. particleScales.Add (temp.transform.localScale);
  105. }
  106. if (temp.transform.childCount > 0)
  107. {
  108. GetChildren(temp);
  109. }
  110. if (temp.transform.childCount == 0 && temp.GetComponent<ParticleSystem>() == null)
  111. {
  112. notify = "";
  113. }
  114. }
  115. }
  116. }
  117. }
  118. //If nothing is currently in the drag n drop field
  119. else
  120. {
  121. notify = null;
  122. }
  123. obj2 = obj;
  124. }
  125. if (flt2 != scaleFloat)
  126. {
  127. SetScales ();
  128. flt2 = scaleFloat;
  129. }
  130. }
  131. void OnGUI ()
  132. {
  133. GUILayout.Label ("Folder or Prefab", EditorStyles.boldLabel);
  134. GUILayout.Label ("Drag and drop the prefab or folder you wish to scale.", EditorStyles.wordWrappedMiniLabel);
  135. obj = (Object)EditorGUILayout.ObjectField(obj, typeof(Object), true,GUILayout.ExpandWidth(false));
  136. GUILayout.Label (notify);
  137. scaleFloat = EditorGUILayout.FloatField ("Scale Multiplier", (Mathf.Clamp(scaleFloat, 0.01f, 1000f)),GUILayout.ExpandWidth(false));
  138. GUILayout.BeginHorizontal ();
  139. if(GUILayout.Button("Set Scale"))
  140. {
  141. SetScales ();
  142. }
  143. if(GUILayout.Button("Reset Scale"))
  144. {
  145. ResetScales ();
  146. }
  147. GUILayout.EndHorizontal ();
  148. GUILayout.Label ("Rotations", EditorStyles.boldLabel);
  149. xSpeed = EditorGUILayout.FloatField ("X", (Mathf.Clamp(xSpeed, -10000f, 10000f)),GUILayout.ExpandWidth(false));
  150. ySpeed = EditorGUILayout.FloatField ("Y", (Mathf.Clamp(ySpeed, -10000f, 10000f)),GUILayout.ExpandWidth(false));
  151. zSpeed = EditorGUILayout.FloatField ("Z", (Mathf.Clamp(zSpeed, -10000f, 10000f)),GUILayout.ExpandWidth(false));
  152. GUILayout.BeginHorizontal ();
  153. if(GUILayout.Button("Set Rotation"))
  154. {
  155. SetRotation ();
  156. }
  157. if(GUILayout.Button("Reset Rotation"))
  158. {
  159. xSpeed = 0;
  160. ySpeed = 0;
  161. zSpeed = 0;
  162. ResetRotation ();
  163. }
  164. GUILayout.EndHorizontal ();
  165. GUILayout.Label ("Color", EditorStyles.boldLabel);
  166. GUILayout.Label ("Note: This will not effect Gradients!", EditorStyles.wordWrappedMiniLabel);
  167. particleColor = EditorGUILayout.ColorField("Start Color", particleColor,GUILayout.ExpandWidth(false));
  168. GUILayout.BeginHorizontal ();
  169. if(GUILayout.Button("Set Color"))
  170. {
  171. SetColors ();
  172. }
  173. if(GUILayout.Button("Reset Color"))
  174. {
  175. ResetColors ();
  176. }
  177. GUILayout.EndHorizontal ();
  178. GUILayout.BeginHorizontal ();
  179. tagString = EditorGUILayout.TextField ("Copy Suffix", tagString,GUILayout.ExpandWidth(false));
  180. if(GUILayout.Button("Save Copy"))
  181. {
  182. if (particlesList[0].activeInHierarchy)
  183. {
  184. Debug.Log ("Can only save copies of a prefab, not active gameObjects.");
  185. }
  186. else
  187. Save ();
  188. }
  189. GUILayout.EndHorizontal ();
  190. GUILayout.Label ("Please use 'Exit' to close window rather than the X button.", EditorStyles.wordWrappedMiniLabel);
  191. if(GUILayout.Button("Exit"))
  192. {
  193. ResetScales ();
  194. ResetColors ();
  195. this.Close();
  196. }
  197. }
  198. void GetChildren(GameObject parentObj)
  199. {
  200. //Check if those children have particleSystems
  201. foreach (Transform child in parentObj.transform)
  202. {
  203. //If they do, put them in a list to scale
  204. if (child.GetComponent<ParticleSystem>() != null)
  205. {
  206. if(!particlesList.Contains (parentObj))
  207. {
  208. particlesList.Add (parentObj);
  209. particleScales.Add (parentObj.transform.localScale);
  210. }
  211. particlesList.Add (child.gameObject);
  212. particleColors.Add (child.GetComponent<ParticleSystem>().startColor);
  213. particleSizes.Add (child.GetComponent<ParticleSystem>().startSize);
  214. particleSpeeds.Add (child.GetComponent<ParticleSystem>().startSpeed);
  215. particleGravity.Add (child.GetComponent<ParticleSystem>().gravityModifier);
  216. particleScales.Add (child.transform.localScale);
  217. }
  218. //If it has children but none of them are particles
  219. else {notify = "";}
  220. }
  221. }
  222. void Clear ()
  223. {
  224. tempList.Clear ();
  225. particlesList.Clear ();
  226. particleSizes.Clear ();
  227. particleSpeeds.Clear ();
  228. particleScales.Clear ();
  229. particleGravity.Clear ();
  230. }
  231. void ResetScales ()
  232. {
  233. scaleFloat = 1.00f;
  234. int j = 0;
  235. for(int i = 0; i < particlesList.Count; i++)
  236. {
  237. if(particlesList[i].GetComponent<ParticleSystem> () != null)
  238. {
  239. particlesList[i].GetComponent<ParticleSystem>().startSize = particleSizes[j] * scaleFloat;
  240. particlesList[i].GetComponent<ParticleSystem>().startSpeed = particleSpeeds[j] * scaleFloat;
  241. particlesList[i].GetComponent<ParticleSystem>().gravityModifier = particleGravity[j] * scaleFloat;
  242. particlesList[i].transform.localScale = (particleScales[j] * scaleFloat);
  243. j++;
  244. }
  245. else {particlesList[i].transform.localScale = (particleScales[i] * scaleFloat);};
  246. }
  247. }
  248. void SetColors ()
  249. {
  250. int j = 0;
  251. for(int i = 0; i < particlesList.Count; i++)
  252. {
  253. if(particlesList[i].GetComponent<ParticleSystem> () != null)
  254. {
  255. particlesList[i].GetComponent<ParticleSystem>().startColor = particleColor;
  256. j++;
  257. }
  258. }
  259. }
  260. void ResetColors ()
  261. {
  262. int j = 0;
  263. for(int i = 0; i < particlesList.Count; i++)
  264. {
  265. if(particlesList[i].GetComponent<ParticleSystem> () != null)
  266. {
  267. particlesList[i].GetComponent<ParticleSystem>().startColor = particleColors[j];
  268. j++;
  269. }
  270. }
  271. }
  272. void SetScales ()
  273. {
  274. int j = 0;
  275. for(int i = 0; i < particlesList.Count; i++)
  276. {
  277. if(particlesList[i].GetComponent<ParticleSystem> () != null)
  278. {
  279. particlesList[i].GetComponent<ParticleSystem>().startSize = particleSizes[j] * scaleFloat;
  280. particlesList[i].GetComponent<ParticleSystem>().startSpeed = particleSpeeds[j] * scaleFloat;
  281. particlesList[i].GetComponent<ParticleSystem>().gravityModifier = particleGravity[j] * scaleFloat;
  282. particlesList[i].transform.localScale = (particleScales[j] * scaleFloat);
  283. j++;
  284. }
  285. else {particlesList[i].transform.localScale = (particleScales[i] * scaleFloat);};
  286. }
  287. }
  288. void SetRotation ()
  289. {
  290. foreach(Object temp in particlesList)
  291. {
  292. GameObject tempGO = temp as GameObject;
  293. if(tempGO.GetComponent<KunaiRotation>() == null)
  294. {
  295. kunaiRotation = tempGO.AddComponent<KunaiRotation>() as KunaiRotation;
  296. }
  297. else{kunaiRotation = tempGO.GetComponent<KunaiRotation>();}
  298. kunaiRotation.xSpeed = xSpeed;
  299. kunaiRotation.ySpeed = ySpeed;
  300. kunaiRotation.zSpeed = zSpeed;
  301. }
  302. }
  303. void ResetRotation ()
  304. {
  305. foreach(Object temp in particlesList)
  306. {
  307. GameObject tempGO = temp as GameObject;
  308. if(tempGO.GetComponent<KunaiRotation>() != null)
  309. {
  310. kunaiRotation = tempGO.GetComponent<KunaiRotation>();
  311. }
  312. DestroyImmediate(kunaiRotation, true);
  313. }
  314. }
  315. void Save ()
  316. {
  317. if(tagString == null || tagString == "")
  318. {
  319. Debug.LogWarning("You must enter a tag to save a copy of your prefab!");
  320. return;
  321. }
  322. foreach(Object temp in particlesList)
  323. {
  324. GameObject tempGO = temp as GameObject;
  325. //If this object is a parent
  326. if(tempGO.transform.root == tempGO.transform && tempGO.transform.childCount > 0)
  327. {
  328. string myPath = AssetDatabase.GetAssetPath (temp).Substring(0, AssetDatabase.GetAssetPath (temp).Length - 7);
  329. Object prefab = PrefabUtility.CreateEmptyPrefab(myPath + tagString + ".prefab");
  330. PrefabUtility.ReplacePrefab(temp as GameObject, prefab);
  331. }
  332. //If this object is just a particle
  333. if(tempGO.transform.root == tempGO.transform && tempGO.transform.childCount == 0)
  334. {
  335. string myPath = AssetDatabase.GetAssetPath (temp).Substring(0, AssetDatabase.GetAssetPath (temp).Length - 7);
  336. Object prefab = PrefabUtility.CreateEmptyPrefab(myPath + tagString + ".prefab");
  337. PrefabUtility.ReplacePrefab(temp as GameObject, prefab);
  338. }
  339. }
  340. ResetScales ();
  341. ResetColors ();
  342. }
  343. void OnApplicationQuit()
  344. {
  345. ResetScales ();
  346. ResetColors ();
  347. }
  348. }
  349. }