ProFlareAtlasInspector.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. 
  2. /// ProFlares - v1.08 - Copyright 2014-2015 All rights reserved - ProFlares.com
  3. /// <summary>
  4. /// ProFlareAtlasInspector.cs
  5. /// Custom inspector for the ProFlareAtlas
  6. /// </summary>
  7. using UnityEngine;
  8. using UnityEditor;
  9. using System.Collections;
  10. [CustomEditor(typeof(ProFlareAtlas))]
  11. public class ProFlareAtlasInspector : Editor {
  12. ProFlareAtlas _ProFlareAtlas;
  13. public string renameString;
  14. bool listeningForGuiChanges;
  15. bool guiChanged = false;
  16. private void CheckUndo()
  17. {
  18. Event e = Event.current;
  19. if ( e.type == EventType.MouseDown && e.button == 0 || e.type == EventType.KeyUp && ( e.keyCode == KeyCode.Tab ) ) {
  20. //Debug.Log("record2");
  21. Undo.RegisterCompleteObjectUndo(target, "Undo ProFlare Change");
  22. Undo.FlushUndoRecordObjects();
  23. listeningForGuiChanges = true;
  24. guiChanged = false;
  25. }
  26. else if ( listeningForGuiChanges && guiChanged ) {
  27. //Debug.Log("record2");
  28. listeningForGuiChanges = false;
  29. Undo.RegisterCompleteObjectUndo(target, "Undo ProFlare Change");
  30. Undo.FlushUndoRecordObjects();
  31. }
  32. }
  33. public override void OnInspectorGUI () {
  34. _ProFlareAtlas = target as ProFlareAtlas;
  35. CheckUndo();
  36. // base.OnInspectorGUI();
  37. FlareEditorHelper.DrawGuiDivider();
  38. GUIStyle title = FlareEditorHelper.TitleStyle();
  39. GUIStyle thinButton = FlareEditorHelper.ThinButtonStyle();
  40. GUIStyle enumDropDown = FlareEditorHelper.EnumStyleButton();
  41. GUIStyle redButton = FlareEditorHelper.ThinButtonRedStyle();
  42. EditorGUILayout.LabelField("ProFlare Atlas Editor",title);
  43. GUILayout.Space(10f);
  44. _ProFlareAtlas.texture = EditorGUILayout.ObjectField("Flare Atlas Texture", _ProFlareAtlas.texture, typeof(Texture2D), false) as Texture2D;
  45. if(_ProFlareAtlas.texture == null){
  46. EditorGUILayout.HelpBox("Assign a texture to the atlas.", MessageType.Warning,true);
  47. return;
  48. }
  49. TextAsset ta = EditorGUILayout.ObjectField("Atlas JSON Import", null, typeof(TextAsset), false) as TextAsset;
  50. if (ta != null)
  51. {
  52. FlareJson.LoadSpriteData(_ProFlareAtlas, ta);
  53. Updated = true;
  54. }
  55. FlareEditorHelper.DrawGuiDivider();
  56. EditorGUILayout.LabelField("Atlas Elements",title);
  57. GUILayout.Space(6f);
  58. EditorGUILayout.BeginHorizontal();
  59. if(_ProFlareAtlas.elementsList.Count < 1)
  60. EditorGUILayout.HelpBox("No Elements in flare atlas", MessageType.Warning,true);
  61. else{
  62. _ProFlareAtlas.elementNameList = new string[_ProFlareAtlas.elementsList.Count];
  63. for(int i = 0; i < _ProFlareAtlas.elementNameList.Length; i++)
  64. _ProFlareAtlas.elementNameList[i] = _ProFlareAtlas.elementsList[i].name;
  65. int _ProFlareAtlasElementNumber = EditorGUILayout.Popup(_ProFlareAtlas.elementNumber, _ProFlareAtlas.elementNameList,enumDropDown);
  66. if(_ProFlareAtlasElementNumber != _ProFlareAtlas.elementNumber){
  67. _ProFlareAtlas.elementNumber = _ProFlareAtlasElementNumber;
  68. renameString = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].name;
  69. }
  70. if(GUILayout.Button("EDIT",thinButton)){Updated = true;
  71. if(_ProFlareAtlas.editElements)
  72. _ProFlareAtlas.editElements = false;
  73. else
  74. _ProFlareAtlas.editElements = true;
  75. }
  76. }
  77. if(GUILayout.Button("ADD NEW",thinButton)){
  78. _ProFlareAtlas.editElements = true;
  79. ProFlareAtlas.Element element = new ProFlareAtlas.Element();
  80. element.name = "New Element " + _ProFlareAtlas.elementsList.Count;
  81. renameString = element.name;
  82. element.Imported = false;
  83. _ProFlareAtlas.elementsList.Add(element);
  84. _ProFlareAtlas.elementNumber = _ProFlareAtlas.elementsList.Count-1;
  85. Updated = true;
  86. }
  87. EditorGUILayout.EndHorizontal();
  88. if(_ProFlareAtlas.elementsList.Count < 1)
  89. return;
  90. EditorGUILayout.BeginVertical("box");
  91. GUILayout.Space(20f);
  92. Rect lastRect = GUILayoutUtility.GetLastRect();
  93. Rect outerRect2 = new Rect(lastRect.center.x,0+lastRect.yMin,200,200);
  94. if(_ProFlareAtlas.elementsList.Count > 0){
  95. GUI.DrawTextureWithTexCoords(outerRect2, _ProFlareAtlas.texture, _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV, false);
  96. GUILayout.Space(200f);
  97. }
  98. GUI.enabled = _ProFlareAtlas.editElements;
  99. if(_ProFlareAtlas.editElements){
  100. int extra = 0;
  101. #if UNITY_4_3
  102. extra = 10;
  103. #endif
  104. Rect outerRect3 = new Rect(107+extra,lastRect.yMin,0.5f,200);
  105. Rect rect = new Rect(0,0,1,1);
  106. GUI.DrawTextureWithTexCoords(outerRect3, EditorGUIUtility.whiteTexture, rect, false);
  107. Rect outerRect4 = new Rect(7+extra,100+lastRect.yMin,200,0.5f);
  108. GUI.DrawTextureWithTexCoords(outerRect4, EditorGUIUtility.whiteTexture, rect, true);
  109. }
  110. GUILayout.BeginHorizontal();
  111. if(!_ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].Imported){
  112. renameString = EditorGUILayout.TextField("Name",renameString);
  113. if(GUILayout.Button("RENAME")){
  114. Updated = true;
  115. _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].name = renameString;
  116. }
  117. }else
  118. EditorGUILayout.LabelField("Name - "+renameString);
  119. GUILayout.EndHorizontal();
  120. EditorGUILayout.Toggle("Imported :", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].Imported);
  121. float width = EditorGUILayout.Slider("Width", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.width,0f,1f);
  122. float height = EditorGUILayout.Slider("Height", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.height,0f,1f);
  123. float CenterX = EditorGUILayout.Slider("Center X", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.center.x,0f,1f);
  124. float CenterY = EditorGUILayout.Slider("Center Y", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.center.y,0f,1f);
  125. float xMin = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.xMin;
  126. float yMin = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.yMin;
  127. Rect newRect = new Rect(xMin,yMin,width,height);
  128. newRect.center = new Vector2(CenterX,CenterY);
  129. GUILayout.Space(40f);
  130. _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV = newRect;
  131. if(GUILayout.Button("DELETE ELEMENT",redButton)){
  132. Updated = true;
  133. _ProFlareAtlas.elementsList.Remove(_ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber]);
  134. _ProFlareAtlas.elementNumber = 0;
  135. }
  136. EditorGUILayout.EndVertical();
  137. GUI.enabled = true;
  138. if (GUI.changed || Updated)
  139. {
  140. Updated = false;
  141. guiChanged = true;
  142. EditorUtility.SetDirty (target);
  143. }
  144. FlareEditorHelper.DrawGuiDivider();
  145. }
  146. bool Updated = false;
  147. }