DisplayObjectEditor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using UnityEngine;
  2. using UnityEditor;
  3. using FairyGUI;
  4. namespace FairyGUIEditor
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. [CustomEditor(typeof(DisplayObjectInfo))]
  10. public class DisplayObjectEditor : Editor
  11. {
  12. #if UNITY_2019_1_OR_NEWER
  13. bool _guiControllersFoldout = true;
  14. bool _guiTransitionsFoldout = true;
  15. bool _guiTextFormatFoldout = true;
  16. #endif
  17. void OnEnable()
  18. {
  19. EditorApplication.update += _onEditorAppUpdate;
  20. }
  21. private void OnDisable()
  22. {
  23. EditorApplication.update -= _onEditorAppUpdate;
  24. }
  25. void _onEditorAppUpdate()
  26. {
  27. Repaint();
  28. }
  29. public override void OnInspectorGUI()
  30. {
  31. DisplayObject obj = (target as DisplayObjectInfo).displayObject;
  32. if (obj == null)
  33. return;
  34. EditorGUILayout.LabelField(obj.GetType().Name + ": " + obj.id, (GUIStyle)"OL Title");
  35. EditorGUILayout.Separator();
  36. EditorGUI.BeginChangeCheck();
  37. string name = EditorGUILayout.TextField("Name", obj.name);
  38. if (EditorGUI.EndChangeCheck())
  39. obj.name = name;
  40. if (obj is Container)
  41. {
  42. EditorGUI.BeginChangeCheck();
  43. bool fairyBatching = EditorGUILayout.Toggle("FairyBatching", ((Container)obj).fairyBatching);
  44. if (EditorGUI.EndChangeCheck())
  45. ((Container)obj).fairyBatching = fairyBatching;
  46. }
  47. GObject gObj = obj.gOwner;
  48. if (gObj != null)
  49. {
  50. EditorGUILayout.Separator();
  51. EditorGUILayout.LabelField(gObj.GetType().Name + ": " + gObj.id, (GUIStyle)"OL Title");
  52. EditorGUILayout.Separator();
  53. if (!string.IsNullOrEmpty(gObj.resourceURL))
  54. {
  55. PackageItem pi = UIPackage.GetItemByURL(gObj.resourceURL);
  56. EditorGUILayout.BeginHorizontal();
  57. EditorGUILayout.PrefixLabel("Resource");
  58. EditorGUILayout.LabelField(pi.name + "@" + pi.owner.name);
  59. EditorGUILayout.EndHorizontal();
  60. }
  61. EditorGUI.BeginChangeCheck();
  62. name = EditorGUILayout.TextField("Name", gObj.name);
  63. if (EditorGUI.EndChangeCheck())
  64. gObj.name = name;
  65. if (gObj.parent != null)
  66. {
  67. string[] options = new string[gObj.parent.numChildren];
  68. int[] values = new int[options.Length];
  69. for (int i = 0; i < options.Length; i++)
  70. {
  71. options[i] = i.ToString();
  72. values[i] = i;
  73. }
  74. EditorGUI.BeginChangeCheck();
  75. int childIndex = EditorGUILayout.IntPopup("Child Index", gObj.parent.GetChildIndex(gObj), options, values);
  76. if (EditorGUI.EndChangeCheck())
  77. gObj.parent.SetChildIndex(gObj, childIndex);
  78. }
  79. else
  80. {
  81. EditorGUILayout.BeginHorizontal();
  82. EditorGUILayout.PrefixLabel("Child Index");
  83. EditorGUILayout.LabelField("No Parent");
  84. EditorGUILayout.EndHorizontal();
  85. }
  86. EditorGUI.BeginChangeCheck();
  87. float alpha = EditorGUILayout.Slider("Alpha", gObj.alpha, 0, 1);
  88. if (EditorGUI.EndChangeCheck())
  89. gObj.alpha = alpha;
  90. EditorGUI.BeginChangeCheck();
  91. Vector3 position = EditorGUILayout.Vector3Field("Position", gObj.position);
  92. if (EditorGUI.EndChangeCheck())
  93. gObj.position = position;
  94. EditorGUI.BeginChangeCheck();
  95. Vector3 rotation = EditorGUILayout.Vector3Field("Rotation", new Vector3(gObj.rotationX, gObj.rotationY, gObj.rotation));
  96. if (EditorGUI.EndChangeCheck())
  97. {
  98. gObj.rotationX = rotation.x;
  99. gObj.rotationY = rotation.y;
  100. gObj.rotation = rotation.z;
  101. }
  102. EditorGUI.BeginChangeCheck();
  103. Vector2 scale = EditorGUILayout.Vector2Field("Scale", gObj.scale);
  104. if (EditorGUI.EndChangeCheck())
  105. gObj.scale = scale;
  106. EditorGUI.BeginChangeCheck();
  107. Vector2 skew = EditorGUILayout.Vector2Field("Skew", gObj.skew);
  108. if (EditorGUI.EndChangeCheck())
  109. gObj.skew = skew;
  110. EditorGUI.BeginChangeCheck();
  111. Vector2 size = EditorGUILayout.Vector2Field("Size", gObj.size);
  112. if (EditorGUI.EndChangeCheck())
  113. gObj.size = size;
  114. EditorGUI.BeginChangeCheck();
  115. Vector2 pivot = EditorGUILayout.Vector2Field("Pivot", gObj.pivot);
  116. if (EditorGUI.EndChangeCheck())
  117. gObj.pivot = pivot;
  118. EditorGUI.BeginChangeCheck();
  119. string text = EditorGUILayout.TextField("Text", gObj.text);
  120. if (EditorGUI.EndChangeCheck())
  121. gObj.text = text;
  122. EditorGUI.BeginChangeCheck();
  123. string icon = EditorGUILayout.TextField("Icon", gObj.icon);
  124. if (EditorGUI.EndChangeCheck())
  125. gObj.icon = icon;
  126. //Draw Color Field
  127. var objType = gObj.GetType();
  128. var colorProperty = objType.GetProperty("color");
  129. if (colorProperty != null)
  130. {
  131. EditorGUI.BeginChangeCheck();
  132. Color color = (Color)colorProperty.GetValue(gObj);
  133. color = EditorGUILayout.ColorField("Color", color);
  134. if (EditorGUI.EndChangeCheck())
  135. {
  136. colorProperty.SetValue(gObj, color);
  137. }
  138. }
  139. EditorGUI.BeginChangeCheck();
  140. string tooltips = EditorGUILayout.TextField("Tooltips", gObj.tooltips);
  141. if (EditorGUI.EndChangeCheck())
  142. gObj.tooltips = tooltips;
  143. if (!(gObj is GImage))
  144. {
  145. EditorGUI.BeginChangeCheck();
  146. bool touchable = EditorGUILayout.Toggle("Touchable", gObj.touchable);
  147. if (EditorGUI.EndChangeCheck())
  148. gObj.touchable = touchable;
  149. EditorGUI.BeginChangeCheck();
  150. bool draggable = EditorGUILayout.Toggle("Draggable", gObj.draggable);
  151. if (EditorGUI.EndChangeCheck())
  152. gObj.draggable = draggable;
  153. }
  154. #if UNITY_2019_1_OR_NEWER
  155. TextFormat textFormat = null;
  156. if (gObj is GTextField gTxt)
  157. {
  158. textFormat = gTxt.textFormat;
  159. }
  160. if (textFormat != null)
  161. {
  162. _guiTextFormatFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(_guiTextFormatFoldout, "Text Format");
  163. EditorGUI.BeginChangeCheck();
  164. if (_guiTextFormatFoldout)
  165. {
  166. var initLabelWidth = EditorGUIUtility.labelWidth;
  167. var richStyle = new GUIStyle(GUI.skin.label);
  168. richStyle.richText = true;
  169. EditorGUIUtility.labelWidth = 60;
  170. textFormat.font = EditorGUILayout.TextField("Font", textFormat.font);
  171. textFormat.align = (AlignType)EditorGUILayout.EnumPopup("Align", textFormat.align);
  172. EditorGUIUtility.labelWidth = initLabelWidth;
  173. EditorGUILayout.BeginHorizontal();
  174. EditorGUIUtility.labelWidth = 40;
  175. textFormat.size = EditorGUILayout.IntField("Size", textFormat.size);
  176. textFormat.color = EditorGUILayout.ColorField("Color", textFormat.color);
  177. EditorGUIUtility.labelWidth = initLabelWidth;
  178. EditorGUILayout.EndHorizontal();
  179. EditorGUILayout.BeginHorizontal();
  180. EditorGUIUtility.labelWidth = 40;
  181. textFormat.outline = EditorGUILayout.FloatField("Outline", textFormat.outline);
  182. textFormat.outlineColor = EditorGUILayout.ColorField("Color", textFormat.outlineColor);
  183. EditorGUIUtility.labelWidth = initLabelWidth;
  184. EditorGUILayout.EndHorizontal();
  185. EditorGUILayout.BeginHorizontal();
  186. EditorGUIUtility.labelWidth = 50;
  187. textFormat.shadowOffset = EditorGUILayout.Vector2Field("Shadow Offset", textFormat.shadowOffset);
  188. textFormat.shadowColor = EditorGUILayout.ColorField("Color", textFormat.shadowColor);
  189. EditorGUIUtility.labelWidth = initLabelWidth;
  190. EditorGUILayout.EndHorizontal();
  191. EditorGUILayout.BeginHorizontal();
  192. textFormat.italic = EditorGUILayout.ToggleLeft("<i>I</i>", textFormat.italic, richStyle, GUILayout.Width(30));
  193. textFormat.bold = EditorGUILayout.ToggleLeft("<b>B</b>", textFormat.bold, richStyle, GUILayout.Width(30));
  194. textFormat.underline = EditorGUILayout.ToggleLeft("U̲", textFormat.underline, richStyle, GUILayout.Width(30));
  195. textFormat.strikethrough = EditorGUILayout.ToggleLeft(" S̶ ̶ ̶", textFormat.strikethrough, richStyle, GUILayout.Width(36));
  196. EditorGUILayout.EndHorizontal();
  197. EditorGUILayout.BeginHorizontal();
  198. EditorGUIUtility.labelWidth = 90;
  199. textFormat.lineSpacing = EditorGUILayout.IntField("Line Spacing", textFormat.lineSpacing);
  200. textFormat.letterSpacing = EditorGUILayout.IntField("Letter Spacing", textFormat.letterSpacing);
  201. EditorGUIUtility.labelWidth = initLabelWidth;
  202. EditorGUILayout.EndHorizontal();
  203. textFormat.specialStyle = (TextFormat.SpecialStyle)EditorGUILayout.EnumPopup("Special Style", textFormat.specialStyle);
  204. }
  205. if (EditorGUI.EndChangeCheck())
  206. gObj.asTextField.textFormat = textFormat;
  207. EditorGUILayout.EndFoldoutHeaderGroup();
  208. }
  209. if (gObj is GComponent gComp)
  210. {
  211. EditorGUI.BeginChangeCheck();
  212. bool opaque = EditorGUILayout.Toggle("Opaque", gComp.opaque);
  213. if (EditorGUI.EndChangeCheck())
  214. gComp.opaque = opaque;
  215. var headerLabelStyle = new GUIStyle(GUI.skin.label);
  216. headerLabelStyle.fontStyle = FontStyle.Bold;
  217. if (gComp.Controllers.Count > 0)
  218. {
  219. _guiControllersFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(_guiControllersFoldout, "Controllers");
  220. if (_guiControllersFoldout)
  221. {
  222. foreach (var ctl in gComp.Controllers)
  223. {
  224. EditorGUILayout.BeginHorizontal();
  225. EditorGUILayout.LabelField(ctl.name, headerLabelStyle, GUILayout.MaxWidth(ctl.name.Length * 15));
  226. for (var i = 0; i < ctl.pageCount; i++)
  227. {
  228. var btnName = ctl.GetPageId(i) + ": " + ctl.GetPageName(i);
  229. var btnStyle = new GUIStyle("ButtonMid");
  230. if (ctl.selectedIndex == i)
  231. {
  232. btnStyle.normal.textColor = Color.green;
  233. btnStyle.hover.textColor = Color.yellow;
  234. btnStyle.fontStyle = FontStyle.Bold;
  235. }
  236. if (GUILayout.Button(btnName, btnStyle))
  237. {
  238. ctl.selectedIndex = i;
  239. }
  240. }
  241. EditorGUILayout.EndHorizontal();
  242. }
  243. }
  244. EditorGUILayout.EndFoldoutHeaderGroup();
  245. }
  246. if (gComp.Transitions.Count > 0)
  247. {
  248. _guiTransitionsFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(_guiTransitionsFoldout, "Transitions");
  249. if (_guiTransitionsFoldout)
  250. {
  251. foreach (var transition in gComp.Transitions)
  252. {
  253. EditorGUILayout.BeginHorizontal();
  254. var labelStyle = new GUIStyle(headerLabelStyle);
  255. if (transition.playing)
  256. {
  257. labelStyle.normal.textColor = Color.yellow;
  258. }
  259. EditorGUILayout.LabelField($"{transition.name} - {transition.totalDuration}s", labelStyle, GUILayout.MinWidth(150));
  260. EditorGUI.BeginChangeCheck();
  261. var timeScale = transition.timeScale;
  262. if (EditorGUI.EndChangeCheck())
  263. transition.timeScale = timeScale;
  264. if (GUILayout.Button("▶", GUILayout.Width(20)))
  265. {
  266. transition.Play();
  267. }
  268. if (GUILayout.Button("■", GUILayout.Width(20)))
  269. {
  270. transition.Stop();
  271. }
  272. EditorGUILayout.EndHorizontal();
  273. }
  274. }
  275. EditorGUILayout.EndFoldoutHeaderGroup();
  276. }
  277. }
  278. #endif
  279. }
  280. }
  281. }
  282. }