PackagesWindow.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. {
  38. itemStyle = new GUIStyle(EditorStyles.textField);
  39. itemStyle.normal.background = null;
  40. itemStyle.onNormal.background = GUI.skin.GetStyle("ObjectPickerResultsEven").active.background;
  41. itemStyle.focused.background = null;
  42. itemStyle.onFocused.background = null;
  43. itemStyle.hover.background = null;
  44. itemStyle.onHover.background = null;
  45. itemStyle.active.background = null;
  46. itemStyle.onActive.background = null;
  47. itemStyle.margin.top = 0;
  48. itemStyle.margin.bottom = 0;
  49. }
  50. EditorGUILayout.BeginHorizontal();
  51. //package list start------
  52. EditorGUILayout.BeginHorizontal();
  53. GUILayout.Space(5);
  54. EditorGUILayout.BeginVertical();
  55. GUILayout.Space(10);
  56. EditorGUILayout.LabelField("Packages", (GUIStyle)"OL Title", GUILayout.Width(300));
  57. EditorGUILayout.BeginHorizontal();
  58. GUILayout.Space(4);
  59. scrollPos1 = EditorGUILayout.BeginScrollView(scrollPos1, (GUIStyle)"CN Box", GUILayout.Height(300), GUILayout.Width(300));
  60. EditorToolSet.LoadPackages();
  61. List<UIPackage> pkgs = UIPackage.GetPackages();
  62. int cnt = pkgs.Count;
  63. if (cnt == 0)
  64. {
  65. selectedPackage = -1;
  66. selectedPackageName = null;
  67. }
  68. else
  69. {
  70. for (int i = 0; i < cnt; i++)
  71. {
  72. EditorGUILayout.BeginHorizontal();
  73. GUILayout.Space(4);
  74. if (GUILayout.Toggle(selectedPackageName == pkgs[i].name, pkgs[i].name, itemStyle, GUILayout.ExpandWidth(true)))
  75. {
  76. selectedPackage = i;
  77. selectedPackageName = pkgs[i].name;
  78. }
  79. EditorGUILayout.EndHorizontal();
  80. }
  81. }
  82. EditorGUILayout.EndScrollView();
  83. EditorGUILayout.EndHorizontal();
  84. EditorGUILayout.EndVertical();
  85. EditorGUILayout.EndHorizontal();
  86. //package list end------
  87. //component list start------
  88. EditorGUILayout.BeginHorizontal();
  89. GUILayout.Space(5);
  90. EditorGUILayout.BeginVertical();
  91. GUILayout.Space(10);
  92. EditorGUILayout.LabelField("Components", (GUIStyle)"OL Title", GUILayout.Width(220));
  93. EditorGUILayout.BeginHorizontal();
  94. GUILayout.Space(4);
  95. scrollPos2 = EditorGUILayout.BeginScrollView(scrollPos2, (GUIStyle)"CN Box", GUILayout.Height(300), GUILayout.Width(220));
  96. if (selectedPackage >= 0)
  97. {
  98. List<PackageItem> items = pkgs[selectedPackage].GetItems();
  99. int i = 0;
  100. foreach (PackageItem pi in items)
  101. {
  102. if (pi.type == PackageItemType.Component && pi.exported)
  103. {
  104. EditorGUILayout.BeginHorizontal();
  105. GUILayout.Space(4);
  106. if (GUILayout.Toggle(selectedComponentName == pi.name, pi.name, itemStyle, GUILayout.ExpandWidth(true)))
  107. selectedComponentName = pi.name;
  108. i++;
  109. EditorGUILayout.EndHorizontal();
  110. }
  111. }
  112. }
  113. EditorGUILayout.EndScrollView();
  114. EditorGUILayout.EndHorizontal();
  115. EditorGUILayout.EndVertical();
  116. EditorGUILayout.EndHorizontal();
  117. //component list end------
  118. GUILayout.Space(10);
  119. EditorGUILayout.EndHorizontal();
  120. GUILayout.Space(20);
  121. //buttons start---
  122. EditorGUILayout.BeginHorizontal();
  123. GUILayout.Space(180);
  124. if (GUILayout.Button("Refresh", GUILayout.Width(100)))
  125. EditorToolSet.ReloadPackages();
  126. GUILayout.Space(20);
  127. if (GUILayout.Button("OK", GUILayout.Width(100)) && selectedPackage >= 0)
  128. {
  129. UIPackage selectedPkg = pkgs[selectedPackage];
  130. string tmp = selectedPkg.assetPath.ToLower();
  131. string packagePath;
  132. int pos = tmp.LastIndexOf("resources/");
  133. if (pos != -1)
  134. packagePath = selectedPkg.assetPath.Substring(pos + 10);
  135. else
  136. packagePath = selectedPkg.assetPath;
  137. if (Selection.activeGameObject != null)
  138. {
  139. #if UNITY_2018_3_OR_NEWER
  140. bool isPrefab = PrefabUtility.GetPrefabAssetType(Selection.activeGameObject) != PrefabAssetType.NotAPrefab;
  141. #else
  142. bool isPrefab = PrefabUtility.GetPrefabType(Selection.activeGameObject) == PrefabType.Prefab;
  143. #endif
  144. Selection.activeGameObject.SendMessage("OnUpdateSource",
  145. new object[] { selectedPkg.name, packagePath, selectedComponentName, !isPrefab },
  146. SendMessageOptions.DontRequireReceiver);
  147. }
  148. #if UNITY_2018_3_OR_NEWER
  149. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  150. if (prefabStage != null)
  151. EditorSceneManager.MarkSceneDirty(prefabStage.scene);
  152. else
  153. ApplyChange();
  154. #else
  155. ApplyChange();
  156. #endif
  157. this.Close();
  158. }
  159. EditorGUILayout.EndHorizontal();
  160. }
  161. void ApplyChange()
  162. {
  163. #if UNITY_5_3_OR_NEWER
  164. EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  165. #elif UNITY_5
  166. EditorApplication.MarkSceneDirty();
  167. #else
  168. EditorUtility.SetDirty(Selection.activeGameObject);
  169. #endif
  170. }
  171. }
  172. }