EditorToolSet.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using FairyGUI;
  5. namespace FairyGUIEditor
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public class EditorToolSet
  11. {
  12. public static GUIContent[] packagesPopupContents;
  13. static bool _loaded;
  14. [InitializeOnLoadMethod]
  15. static void Startup()
  16. {
  17. EditorApplication.update += EditorApplication_Update;
  18. }
  19. [RuntimeInitializeOnLoadMethod]
  20. static void InitializeOnLoad()
  21. {
  22. _loaded = false;
  23. }
  24. [MenuItem("GameObject/FairyGUI/UI Panel", false, 0)]
  25. static void CreatePanel()
  26. {
  27. EditorApplication.update -= EditorApplication_Update;
  28. EditorApplication.update += EditorApplication_Update;
  29. StageCamera.CheckMainCamera();
  30. GameObject panelObject = new GameObject("UIPanel");
  31. if (Selection.activeGameObject != null)
  32. {
  33. panelObject.transform.parent = Selection.activeGameObject.transform;
  34. panelObject.layer = Selection.activeGameObject.layer;
  35. }
  36. else
  37. {
  38. int layer = LayerMask.NameToLayer(StageCamera.LayerName);
  39. panelObject.layer = layer;
  40. }
  41. panelObject.AddComponent<FairyGUI.UIPanel>();
  42. Selection.objects = new Object[] { panelObject };
  43. }
  44. [MenuItem("GameObject/FairyGUI/UI Camera", false, 0)]
  45. static void CreateCamera()
  46. {
  47. StageCamera.CheckMainCamera();
  48. Selection.objects = new Object[] { StageCamera.main.gameObject };
  49. }
  50. [MenuItem("Window/FairyGUI - Refresh Packages And Panels")]
  51. static void RefreshPanels()
  52. {
  53. ReloadPackages();
  54. }
  55. static void EditorApplication_Update()
  56. {
  57. if (Application.isPlaying)
  58. return;
  59. if (_loaded || !EMRenderSupport.hasTarget)
  60. return;
  61. LoadPackages();
  62. }
  63. public static void ReloadPackages()
  64. {
  65. if (!Application.isPlaying)
  66. {
  67. _loaded = false;
  68. LoadPackages();
  69. Debug.Log("FairyGUI - Refresh Packages And Panels complete.");
  70. }
  71. else
  72. EditorUtility.DisplayDialog("FairyGUI", "Cannot run in play mode.", "OK");
  73. }
  74. public static void LoadPackages()
  75. {
  76. if (Application.isPlaying || _loaded)
  77. return;
  78. EditorApplication.update -= EditorApplication_Update;
  79. EditorApplication.update += EditorApplication_Update;
  80. _loaded = true;
  81. UIPackage.RemoveAllPackages();
  82. UIPackage.branch = null;
  83. FontManager.Clear();
  84. NTexture.DisposeEmpty();
  85. UIObjectFactory.Clear();
  86. string[] ids = AssetDatabase.FindAssets("_fui t:textAsset");
  87. int cnt = ids.Length;
  88. for (int i = 0; i < cnt; i++)
  89. {
  90. string assetPath = AssetDatabase.GUIDToAssetPath(ids[i]);
  91. int pos = assetPath.LastIndexOf("_fui");
  92. if (pos == -1)
  93. continue;
  94. assetPath = assetPath.Substring(0, pos);
  95. if (AssetDatabase.AssetPathToGUID(assetPath) != null)
  96. UIPackage.AddPackage(assetPath,
  97. (string name, string extension, System.Type type, out DestroyMethod destroyMethod) =>
  98. {
  99. destroyMethod = DestroyMethod.Unload;
  100. return AssetDatabase.LoadAssetAtPath(name + extension, type);
  101. }
  102. );
  103. }
  104. List<UIPackage> pkgs = UIPackage.GetPackages();
  105. pkgs.Sort(CompareUIPackage);
  106. cnt = pkgs.Count;
  107. packagesPopupContents = new GUIContent[cnt + 1];
  108. for (int i = 0; i < cnt; i++)
  109. packagesPopupContents[i] = new GUIContent(pkgs[i].name);
  110. packagesPopupContents[cnt] = new GUIContent("Please Select");
  111. EMRenderSupport.Reload();
  112. }
  113. static int CompareUIPackage(UIPackage u1, UIPackage u2)
  114. {
  115. return u1.name.CompareTo(u2.name);
  116. }
  117. }
  118. }