PackagesWindow.cs 6.2 KB

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