ShaderVariantCollectorWindow.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #if UNITY_2019_4_OR_NEWER
  2. using System;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEditor.UIElements;
  8. using UnityEngine.UIElements;
  9. namespace YooAsset.Editor
  10. {
  11. public class ShaderVariantCollectorWindow : EditorWindow
  12. {
  13. [MenuItem("YooAsset/ShaderVariant Collector", false, 201)]
  14. public static void OpenWindow()
  15. {
  16. ShaderVariantCollectorWindow window = GetWindow<ShaderVariantCollectorWindow>("着色器变种收集工具", true, WindowsDefine.DockedWindowTypes);
  17. window.minSize = new Vector2(800, 600);
  18. }
  19. private List<string> _packageNames;
  20. private Button _saveButton;
  21. private Button _collectButton;
  22. private TextField _collectOutputField;
  23. private Label _currentShaderCountField;
  24. private Label _currentVariantCountField;
  25. private SliderInt _processCapacitySlider;
  26. private PopupField<string> _packageField;
  27. public void CreateGUI()
  28. {
  29. try
  30. {
  31. VisualElement root = this.rootVisualElement;
  32. // 加载布局文件
  33. var visualAsset = UxmlLoader.LoadWindowUXML<ShaderVariantCollectorWindow>();
  34. if (visualAsset == null)
  35. return;
  36. visualAsset.CloneTree(root);
  37. // 配置保存按钮
  38. _saveButton = root.Q<Button>("SaveButton");
  39. _saveButton.clicked += SaveBtn_clicked;
  40. // 包裹名称列表
  41. _packageNames = GetBuildPackageNames();
  42. // 文件输出目录
  43. _collectOutputField = root.Q<TextField>("CollectOutput");
  44. _collectOutputField.SetValueWithoutNotify(ShaderVariantCollectorSettingData.Setting.SavePath);
  45. _collectOutputField.RegisterValueChangedCallback(evt =>
  46. {
  47. ShaderVariantCollectorSettingData.IsDirty = true;
  48. ShaderVariantCollectorSettingData.Setting.SavePath = _collectOutputField.value;
  49. });
  50. // 收集的包裹
  51. var packageContainer = root.Q("PackageContainer");
  52. if (_packageNames.Count > 0)
  53. {
  54. int defaultIndex = GetDefaultPackageIndex(ShaderVariantCollectorSettingData.Setting.CollectPackage);
  55. _packageField = new PopupField<string>(_packageNames, defaultIndex);
  56. _packageField.label = "Package";
  57. _packageField.style.width = 350;
  58. _packageField.RegisterValueChangedCallback(evt =>
  59. {
  60. ShaderVariantCollectorSettingData.IsDirty = true;
  61. ShaderVariantCollectorSettingData.Setting.CollectPackage = _packageField.value;
  62. });
  63. packageContainer.Add(_packageField);
  64. }
  65. else
  66. {
  67. _packageField = new PopupField<string>();
  68. _packageField.label = "Package";
  69. _packageField.style.width = 350;
  70. packageContainer.Add(_packageField);
  71. }
  72. // 容器值
  73. _processCapacitySlider = root.Q<SliderInt>("ProcessCapacity");
  74. _processCapacitySlider.SetValueWithoutNotify(ShaderVariantCollectorSettingData.Setting.ProcessCapacity);
  75. #if !UNITY_2020_3_OR_NEWER
  76. _processCapacitySlider.label = $"Capacity ({_processCapacitySlider.value})";
  77. _processCapacitySlider.RegisterValueChangedCallback(evt =>
  78. {
  79. ShaderVariantCollectorSettingData.IsDirty = true;
  80. ShaderVariantCollectorSettingData.Setting.ProcessCapacity = _processCapacitySlider.value;
  81. _processCapacitySlider.label = $"Capacity ({_processCapacitySlider.value})";
  82. });
  83. #else
  84. _processCapacitySlider.RegisterValueChangedCallback(evt =>
  85. {
  86. ShaderVariantCollectorSettingData.IsDirty = true;
  87. ShaderVariantCollectorSettingData.Setting.ProcessCapacity = _processCapacitySlider.value;
  88. });
  89. #endif
  90. _currentShaderCountField = root.Q<Label>("CurrentShaderCount");
  91. _currentVariantCountField = root.Q<Label>("CurrentVariantCount");
  92. // 变种收集按钮
  93. _collectButton = root.Q<Button>("CollectButton");
  94. _collectButton.clicked += CollectButton_clicked;
  95. }
  96. catch (Exception e)
  97. {
  98. Debug.LogError(e.ToString());
  99. }
  100. }
  101. public void OnDestroy()
  102. {
  103. if (ShaderVariantCollectorSettingData.IsDirty)
  104. ShaderVariantCollectorSettingData.SaveFile();
  105. }
  106. private void Update()
  107. {
  108. if (_saveButton != null)
  109. {
  110. if (ShaderVariantCollectorSettingData.IsDirty)
  111. {
  112. if (_saveButton.enabledSelf == false)
  113. _saveButton.SetEnabled(true);
  114. }
  115. else
  116. {
  117. if (_saveButton.enabledSelf)
  118. _saveButton.SetEnabled(false);
  119. }
  120. }
  121. if (_currentShaderCountField != null)
  122. {
  123. int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount();
  124. _currentShaderCountField.text = $"Current Shader Count : {currentShaderCount}";
  125. }
  126. if (_currentVariantCountField != null)
  127. {
  128. int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount();
  129. _currentVariantCountField.text = $"Current Variant Count : {currentVariantCount}";
  130. }
  131. }
  132. private void SaveBtn_clicked()
  133. {
  134. ShaderVariantCollectorSettingData.SaveFile();
  135. }
  136. private void CollectButton_clicked()
  137. {
  138. string savePath = ShaderVariantCollectorSettingData.Setting.SavePath;
  139. string packageName = ShaderVariantCollectorSettingData.Setting.CollectPackage;
  140. int processCapacity = _processCapacitySlider.value;
  141. ShaderVariantCollector.Run(savePath, packageName, processCapacity, null);
  142. }
  143. // 构建包裹相关
  144. private int GetDefaultPackageIndex(string packageName)
  145. {
  146. for (int index = 0; index < _packageNames.Count; index++)
  147. {
  148. if (_packageNames[index] == packageName)
  149. {
  150. return index;
  151. }
  152. }
  153. ShaderVariantCollectorSettingData.Setting.CollectPackage = _packageNames[0];
  154. return 0;
  155. }
  156. private List<string> GetBuildPackageNames()
  157. {
  158. List<string> result = new List<string>();
  159. foreach (var package in AssetBundleCollectorSettingData.Setting.Packages)
  160. {
  161. result.Add(package.PackageName);
  162. }
  163. return result;
  164. }
  165. }
  166. }
  167. #endif