PackagesWindow.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. #if UNITY_5_3_OR_NEWER
  5. using UnityEditor.SceneManagement;
  6. #endif
  7. #if UNITY_2018_3_OR_NEWER
  8. using UnityEditor.Experimental.SceneManagement;
  9. #endif
  10. using FairyGUI;
  11. namespace FairyGUIEditor
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public class PackagesWindow : EditorWindow
  17. {
  18. Vector2 scrollPos1;
  19. Vector2 scrollPos2;
  20. GUIStyle itemStyle;
  21. int selectedPackage;
  22. string selectedPackageName;
  23. string selectedComponentName;
  24. public PackagesWindow()
  25. {
  26. this.maxSize = new Vector2(550, 400);
  27. this.minSize = new Vector2(550, 400);
  28. }
  29. public void SetSelection(string packageName, string componentName)
  30. {
  31. selectedPackageName = packageName;
  32. selectedComponentName = componentName;
  33. }
  34. void OnGUI()
  35. {
  36. if (itemStyle == null)
  37. itemStyle = new GUIStyle(GUI.skin.GetStyle("Tag MenuItem"));
  38. EditorGUILayout.BeginHorizontal();
  39. //package list start------
  40. EditorGUILayout.BeginHorizontal();
  41. GUILayout.Space(5);
  42. EditorGUILayout.BeginVertical();
  43. GUILayout.Space(10);
  44. EditorGUILayout.LabelField("Packages", (GUIStyle)"OL Title", GUILayout.Width(300));
  45. GUILayout.Space(5);
  46. EditorGUILayout.BeginHorizontal();
  47. GUILayout.Space(4);
  48. scrollPos1 = EditorGUILayout.BeginScrollView(scrollPos1, (GUIStyle)"CN Box", GUILayout.Height(300), GUILayout.Width(300));
  49. EditorToolSet.LoadPackages();
  50. List<UIPackage> pkgs = UIPackage.GetPackages();
  51. int cnt = pkgs.Count;
  52. if (cnt == 0)
  53. {
  54. selectedPackage = -1;
  55. selectedPackageName = null;
  56. }
  57. else
  58. {
  59. for (int i = 0; i < cnt; i++)
  60. {
  61. EditorGUILayout.BeginHorizontal();
  62. if (GUILayout.Toggle(selectedPackageName == pkgs[i].name, pkgs[i].name, itemStyle, GUILayout.ExpandWidth(true)))
  63. {
  64. selectedPackage = i;
  65. selectedPackageName = pkgs[i].name;
  66. }
  67. EditorGUILayout.EndHorizontal();
  68. }
  69. }
  70. EditorGUILayout.EndScrollView();
  71. EditorGUILayout.EndHorizontal();
  72. EditorGUILayout.EndVertical();
  73. EditorGUILayout.EndHorizontal();
  74. //package list end------
  75. //component list start------
  76. EditorGUILayout.BeginHorizontal();
  77. GUILayout.Space(5);
  78. EditorGUILayout.BeginVertical();
  79. GUILayout.Space(10);
  80. EditorGUILayout.LabelField("Components", (GUIStyle)"OL Title", GUILayout.Width(220));
  81. GUILayout.Space(5);
  82. EditorGUILayout.BeginHorizontal();
  83. GUILayout.Space(4);
  84. scrollPos2 = EditorGUILayout.BeginScrollView(scrollPos2, (GUIStyle)"CN Box", GUILayout.Height(300), GUILayout.Width(220));
  85. if (selectedPackage >= 0)
  86. {
  87. List<PackageItem> items = pkgs[selectedPackage].GetItems();
  88. int i = 0;
  89. foreach (PackageItem pi in items)
  90. {
  91. if (pi.type == PackageItemType.Component && pi.exported)
  92. {
  93. EditorGUILayout.BeginHorizontal();
  94. if (GUILayout.Toggle(selectedComponentName == pi.name, pi.name, itemStyle, GUILayout.ExpandWidth(true)))
  95. selectedComponentName = pi.name;
  96. i++;
  97. EditorGUILayout.EndHorizontal();
  98. }
  99. }
  100. }
  101. EditorGUILayout.EndScrollView();
  102. EditorGUILayout.EndHorizontal();
  103. EditorGUILayout.EndVertical();
  104. EditorGUILayout.EndHorizontal();
  105. //component list end------
  106. GUILayout.Space(10);
  107. EditorGUILayout.EndHorizontal();
  108. GUILayout.Space(20);
  109. //buttons start---
  110. EditorGUILayout.BeginHorizontal();
  111. GUILayout.Space(180);
  112. if (GUILayout.Button("Refresh", GUILayout.Width(100)))
  113. EditorToolSet.ReloadPackages();
  114. GUILayout.Space(20);
  115. if (GUILayout.Button("OK", GUILayout.Width(100)) && selectedPackage >= 0)
  116. {
  117. UIPackage selectedPkg = pkgs[selectedPackage];
  118. string tmp = selectedPkg.assetPath.ToLower();
  119. string packagePath;
  120. int pos = tmp.LastIndexOf("/resources/");
  121. if (pos != -1)
  122. packagePath = selectedPkg.assetPath.Substring(pos + 11);
  123. else
  124. {
  125. pos = tmp.IndexOf("resources/");
  126. if (pos == 0)
  127. packagePath = selectedPkg.assetPath.Substring(pos + 10);
  128. else
  129. packagePath = selectedPkg.assetPath;
  130. }
  131. if (Selection.activeGameObject != null)
  132. {
  133. #if UNITY_2018_3_OR_NEWER
  134. bool isPrefab = PrefabUtility.GetPrefabAssetType(Selection.activeGameObject) != PrefabAssetType.NotAPrefab;
  135. #else
  136. bool isPrefab = PrefabUtility.GetPrefabType(Selection.activeGameObject) == PrefabType.Prefab;
  137. #endif
  138. Selection.activeGameObject.SendMessage("OnUpdateSource",
  139. new object[] { selectedPkg.name, packagePath, selectedComponentName, !isPrefab },
  140. SendMessageOptions.DontRequireReceiver);
  141. }
  142. #if UNITY_2018_3_OR_NEWER
  143. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  144. if (prefabStage != null)
  145. EditorSceneManager.MarkSceneDirty(prefabStage.scene);
  146. else
  147. ApplyChange();
  148. #else
  149. ApplyChange();
  150. #endif
  151. this.Close();
  152. }
  153. EditorGUILayout.EndHorizontal();
  154. }
  155. void ApplyChange()
  156. {
  157. #if UNITY_5_3_OR_NEWER
  158. EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  159. #elif UNITY_5
  160. EditorApplication.MarkSceneDirty();
  161. #else
  162. EditorUtility.SetDirty(Selection.activeGameObject);
  163. #endif
  164. }
  165. }
  166. }