ResourceComponentInspector.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Text;
  12. using UnityEditor;
  13. using UnityEngine;
  14. using UnityGameFramework.Runtime;
  15. namespace UnityGameFramework.Editor
  16. {
  17. [CustomEditor(typeof(ResourceComponent))]
  18. internal sealed class ResourceComponentInspector : GameFrameworkInspector
  19. {
  20. private static readonly string[] ResourceModeNames = new string[] { "Package", "Updatable", "Updatable While Playing" };
  21. private SerializedProperty m_ResourceMode = null;
  22. private SerializedProperty m_ReadWritePathType = null;
  23. private SerializedProperty m_MinUnloadUnusedAssetsInterval = null;
  24. private SerializedProperty m_MaxUnloadUnusedAssetsInterval = null;
  25. private SerializedProperty m_AssetAutoReleaseInterval = null;
  26. private SerializedProperty m_AssetCapacity = null;
  27. private SerializedProperty m_AssetExpireTime = null;
  28. private SerializedProperty m_AssetPriority = null;
  29. private SerializedProperty m_ResourceAutoReleaseInterval = null;
  30. private SerializedProperty m_ResourceCapacity = null;
  31. private SerializedProperty m_ResourceExpireTime = null;
  32. private SerializedProperty m_ResourcePriority = null;
  33. private SerializedProperty m_UpdatePrefixUri = null;
  34. private SerializedProperty m_GenerateReadWriteVersionListLength = null;
  35. private SerializedProperty m_UpdateRetryCount = null;
  36. private SerializedProperty m_InstanceRoot = null;
  37. private SerializedProperty m_LoadResourceAgentHelperCount = null;
  38. private FieldInfo m_EditorResourceModeFieldInfo = null;
  39. private int m_ResourceModeIndex = 0;
  40. private HelperInfo<ResourceHelperBase> m_ResourceHelperInfo = new HelperInfo<ResourceHelperBase>("Resource");
  41. private HelperInfo<LoadResourceAgentHelperBase> m_LoadResourceAgentHelperInfo = new HelperInfo<LoadResourceAgentHelperBase>("LoadResourceAgent");
  42. public override void OnInspectorGUI()
  43. {
  44. base.OnInspectorGUI();
  45. serializedObject.Update();
  46. ResourceComponent t = (ResourceComponent)target;
  47. bool isEditorResourceMode = (bool)m_EditorResourceModeFieldInfo.GetValue(target);
  48. if (isEditorResourceMode)
  49. {
  50. EditorGUILayout.HelpBox("Editor resource mode is enabled. Some options are disabled.", MessageType.Warning);
  51. }
  52. EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
  53. {
  54. if (EditorApplication.isPlaying && IsPrefabInHierarchy(t.gameObject))
  55. {
  56. EditorGUILayout.EnumPopup("Resource Mode", t.ResourceMode);
  57. }
  58. else
  59. {
  60. int selectedIndex = EditorGUILayout.Popup("Resource Mode", m_ResourceModeIndex, ResourceModeNames);
  61. if (selectedIndex != m_ResourceModeIndex)
  62. {
  63. m_ResourceModeIndex = selectedIndex;
  64. m_ResourceMode.enumValueIndex = selectedIndex + 1;
  65. }
  66. }
  67. m_ReadWritePathType.enumValueIndex = (int)(ReadWritePathType)EditorGUILayout.EnumPopup("Read-Write Path Type", t.ReadWritePathType);
  68. }
  69. EditorGUI.EndDisabledGroup();
  70. float minUnloadUnusedAssetsInterval = EditorGUILayout.Slider("Min Unload Unused Assets Interval", m_MinUnloadUnusedAssetsInterval.floatValue, 0f, 3600f);
  71. if (minUnloadUnusedAssetsInterval != m_MinUnloadUnusedAssetsInterval.floatValue)
  72. {
  73. if (EditorApplication.isPlaying)
  74. {
  75. t.MinUnloadUnusedAssetsInterval = minUnloadUnusedAssetsInterval;
  76. }
  77. else
  78. {
  79. m_MinUnloadUnusedAssetsInterval.floatValue = minUnloadUnusedAssetsInterval;
  80. }
  81. }
  82. float maxUnloadUnusedAssetsInterval = EditorGUILayout.Slider("Max Unload Unused Assets Interval", m_MaxUnloadUnusedAssetsInterval.floatValue, 0f, 3600f);
  83. if (maxUnloadUnusedAssetsInterval != m_MaxUnloadUnusedAssetsInterval.floatValue)
  84. {
  85. if (EditorApplication.isPlaying)
  86. {
  87. t.MaxUnloadUnusedAssetsInterval = maxUnloadUnusedAssetsInterval;
  88. }
  89. else
  90. {
  91. m_MaxUnloadUnusedAssetsInterval.floatValue = maxUnloadUnusedAssetsInterval;
  92. }
  93. }
  94. EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying && isEditorResourceMode);
  95. {
  96. float assetAutoReleaseInterval = EditorGUILayout.DelayedFloatField("Asset Auto Release Interval", m_AssetAutoReleaseInterval.floatValue);
  97. if (assetAutoReleaseInterval != m_AssetAutoReleaseInterval.floatValue)
  98. {
  99. if (EditorApplication.isPlaying)
  100. {
  101. t.AssetAutoReleaseInterval = assetAutoReleaseInterval;
  102. }
  103. else
  104. {
  105. m_AssetAutoReleaseInterval.floatValue = assetAutoReleaseInterval;
  106. }
  107. }
  108. int assetCapacity = EditorGUILayout.DelayedIntField("Asset Capacity", m_AssetCapacity.intValue);
  109. if (assetCapacity != m_AssetCapacity.intValue)
  110. {
  111. if (EditorApplication.isPlaying)
  112. {
  113. t.AssetCapacity = assetCapacity;
  114. }
  115. else
  116. {
  117. m_AssetCapacity.intValue = assetCapacity;
  118. }
  119. }
  120. float assetExpireTime = EditorGUILayout.DelayedFloatField("Asset Expire Time", m_AssetExpireTime.floatValue);
  121. if (assetExpireTime != m_AssetExpireTime.floatValue)
  122. {
  123. if (EditorApplication.isPlaying)
  124. {
  125. t.AssetExpireTime = assetExpireTime;
  126. }
  127. else
  128. {
  129. m_AssetExpireTime.floatValue = assetExpireTime;
  130. }
  131. }
  132. int assetPriority = EditorGUILayout.DelayedIntField("Asset Priority", m_AssetPriority.intValue);
  133. if (assetPriority != m_AssetPriority.intValue)
  134. {
  135. if (EditorApplication.isPlaying)
  136. {
  137. t.AssetPriority = assetPriority;
  138. }
  139. else
  140. {
  141. m_AssetPriority.intValue = assetPriority;
  142. }
  143. }
  144. float resourceAutoReleaseInterval = EditorGUILayout.DelayedFloatField("Resource Auto Release Interval", m_ResourceAutoReleaseInterval.floatValue);
  145. if (resourceAutoReleaseInterval != m_ResourceAutoReleaseInterval.floatValue)
  146. {
  147. if (EditorApplication.isPlaying)
  148. {
  149. t.ResourceAutoReleaseInterval = resourceAutoReleaseInterval;
  150. }
  151. else
  152. {
  153. m_ResourceAutoReleaseInterval.floatValue = resourceAutoReleaseInterval;
  154. }
  155. }
  156. int resourceCapacity = EditorGUILayout.DelayedIntField("Resource Capacity", m_ResourceCapacity.intValue);
  157. if (resourceCapacity != m_ResourceCapacity.intValue)
  158. {
  159. if (EditorApplication.isPlaying)
  160. {
  161. t.ResourceCapacity = resourceCapacity;
  162. }
  163. else
  164. {
  165. m_ResourceCapacity.intValue = resourceCapacity;
  166. }
  167. }
  168. float resourceExpireTime = EditorGUILayout.DelayedFloatField("Resource Expire Time", m_ResourceExpireTime.floatValue);
  169. if (resourceExpireTime != m_ResourceExpireTime.floatValue)
  170. {
  171. if (EditorApplication.isPlaying)
  172. {
  173. t.ResourceExpireTime = resourceExpireTime;
  174. }
  175. else
  176. {
  177. m_ResourceExpireTime.floatValue = resourceExpireTime;
  178. }
  179. }
  180. int resourcePriority = EditorGUILayout.DelayedIntField("Resource Priority", m_ResourcePriority.intValue);
  181. if (resourcePriority != m_ResourcePriority.intValue)
  182. {
  183. if (EditorApplication.isPlaying)
  184. {
  185. t.ResourcePriority = resourcePriority;
  186. }
  187. else
  188. {
  189. m_ResourcePriority.intValue = resourcePriority;
  190. }
  191. }
  192. if (m_ResourceModeIndex > 0)
  193. {
  194. string updatePrefixUri = EditorGUILayout.DelayedTextField("Update Prefix Uri", m_UpdatePrefixUri.stringValue);
  195. if (updatePrefixUri != m_UpdatePrefixUri.stringValue)
  196. {
  197. if (EditorApplication.isPlaying)
  198. {
  199. t.UpdatePrefixUri = updatePrefixUri;
  200. }
  201. else
  202. {
  203. m_UpdatePrefixUri.stringValue = updatePrefixUri;
  204. }
  205. }
  206. int generateReadWriteVersionListLength = EditorGUILayout.DelayedIntField("Generate Read-Write Version List Length", m_GenerateReadWriteVersionListLength.intValue);
  207. if (generateReadWriteVersionListLength != m_GenerateReadWriteVersionListLength.intValue)
  208. {
  209. if (EditorApplication.isPlaying)
  210. {
  211. t.GenerateReadWriteVersionListLength = generateReadWriteVersionListLength;
  212. }
  213. else
  214. {
  215. m_GenerateReadWriteVersionListLength.intValue = generateReadWriteVersionListLength;
  216. }
  217. }
  218. int updateRetryCount = EditorGUILayout.DelayedIntField("Update Retry Count", m_UpdateRetryCount.intValue);
  219. if (updateRetryCount != m_UpdateRetryCount.intValue)
  220. {
  221. if (EditorApplication.isPlaying)
  222. {
  223. t.UpdateRetryCount = updateRetryCount;
  224. }
  225. else
  226. {
  227. m_UpdateRetryCount.intValue = updateRetryCount;
  228. }
  229. }
  230. }
  231. }
  232. EditorGUI.EndDisabledGroup();
  233. EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
  234. {
  235. EditorGUILayout.PropertyField(m_InstanceRoot);
  236. m_ResourceHelperInfo.Draw();
  237. m_LoadResourceAgentHelperInfo.Draw();
  238. m_LoadResourceAgentHelperCount.intValue = EditorGUILayout.IntSlider("Load Resource Agent Helper Count", m_LoadResourceAgentHelperCount.intValue, 1, 128);
  239. }
  240. EditorGUI.EndDisabledGroup();
  241. if (EditorApplication.isPlaying && IsPrefabInHierarchy(t.gameObject))
  242. {
  243. EditorGUILayout.LabelField("Unload Unused Assets", Utility.Text.Format("{0} / {1}", t.LastUnloadUnusedAssetsOperationElapseSeconds.ToString("F2"), t.MaxUnloadUnusedAssetsInterval.ToString("F2")));
  244. EditorGUILayout.LabelField("Read-Only Path", t.ReadOnlyPath.ToString());
  245. EditorGUILayout.LabelField("Read-Write Path", t.ReadWritePath.ToString());
  246. EditorGUILayout.LabelField("Current Variant", t.CurrentVariant ?? "<Unknwon>");
  247. EditorGUILayout.LabelField("Applicable Game Version", isEditorResourceMode ? "N/A" : t.ApplicableGameVersion ?? "<Unknwon>");
  248. EditorGUILayout.LabelField("Internal Resource Version", isEditorResourceMode ? "N/A" : t.InternalResourceVersion.ToString());
  249. EditorGUILayout.LabelField("Asset Count", isEditorResourceMode ? "N/A" : t.AssetCount.ToString());
  250. EditorGUILayout.LabelField("Resource Count", isEditorResourceMode ? "N/A" : t.ResourceCount.ToString());
  251. EditorGUILayout.LabelField("Resource Group Count", isEditorResourceMode ? "N/A" : t.ResourceGroupCount.ToString());
  252. if (m_ResourceModeIndex > 0)
  253. {
  254. EditorGUILayout.LabelField("Applying Resource Pack Path", isEditorResourceMode ? "N/A" : t.ApplyingResourcePackPath ?? "<Unknwon>");
  255. EditorGUILayout.LabelField("Apply Waiting Count", isEditorResourceMode ? "N/A" : t.ApplyWaitingCount.ToString());
  256. EditorGUILayout.LabelField("Updating Resource Group", isEditorResourceMode ? "N/A" : t.UpdatingResourceGroup != null ? t.UpdatingResourceGroup.Name : "<Unknwon>");
  257. EditorGUILayout.LabelField("Update Waiting Count", isEditorResourceMode ? "N/A" : t.UpdateWaitingCount.ToString());
  258. EditorGUILayout.LabelField("Update Waiting While Playing Count", isEditorResourceMode ? "N/A" : t.UpdateWaitingWhilePlayingCount.ToString());
  259. EditorGUILayout.LabelField("Update Candidate Count", isEditorResourceMode ? "N/A" : t.UpdateCandidateCount.ToString());
  260. }
  261. EditorGUILayout.LabelField("Load Total Agent Count", isEditorResourceMode ? "N/A" : t.LoadTotalAgentCount.ToString());
  262. EditorGUILayout.LabelField("Load Free Agent Count", isEditorResourceMode ? "N/A" : t.LoadFreeAgentCount.ToString());
  263. EditorGUILayout.LabelField("Load Working Agent Count", isEditorResourceMode ? "N/A" : t.LoadWorkingAgentCount.ToString());
  264. EditorGUILayout.LabelField("Load Waiting Task Count", isEditorResourceMode ? "N/A" : t.LoadWaitingTaskCount.ToString());
  265. if (!isEditorResourceMode)
  266. {
  267. EditorGUILayout.BeginVertical("box");
  268. {
  269. GameFramework.TaskInfo[] loadAssetInfos = t.GetAllLoadAssetInfos();
  270. if (loadAssetInfos.Length > 0)
  271. {
  272. foreach (GameFramework.TaskInfo loadAssetInfo in loadAssetInfos)
  273. {
  274. DrawLoadAssetInfo(loadAssetInfo);
  275. }
  276. if (GUILayout.Button("Export CSV Data"))
  277. {
  278. string exportFileName = EditorUtility.SaveFilePanel("Export CSV Data", string.Empty, "Load Asset Task Data.csv", string.Empty);
  279. if (!string.IsNullOrEmpty(exportFileName))
  280. {
  281. try
  282. {
  283. int index = 0;
  284. string[] data = new string[loadAssetInfos.Length + 1];
  285. data[index++] = "Load Asset Name,Serial Id,Priority,Status";
  286. foreach (GameFramework.TaskInfo loadAssetInfo in loadAssetInfos)
  287. {
  288. data[index++] = Utility.Text.Format("{0},{1},{2},{3}", loadAssetInfo.Description, loadAssetInfo.SerialId.ToString(), loadAssetInfo.Priority.ToString(), loadAssetInfo.Status.ToString());
  289. }
  290. File.WriteAllLines(exportFileName, data, Encoding.UTF8);
  291. Debug.Log(Utility.Text.Format("Export load asset task CSV data to '{0}' success.", exportFileName));
  292. }
  293. catch (Exception exception)
  294. {
  295. Debug.LogError(Utility.Text.Format("Export load asset task CSV data to '{0}' failure, exception is '{1}'.", exportFileName, exception.ToString()));
  296. }
  297. }
  298. }
  299. }
  300. else
  301. {
  302. GUILayout.Label("Load Asset Task is Empty ...");
  303. }
  304. }
  305. EditorGUILayout.EndVertical();
  306. }
  307. }
  308. serializedObject.ApplyModifiedProperties();
  309. Repaint();
  310. }
  311. protected override void OnCompileComplete()
  312. {
  313. base.OnCompileComplete();
  314. RefreshTypeNames();
  315. }
  316. private void OnEnable()
  317. {
  318. m_ResourceMode = serializedObject.FindProperty("m_ResourceMode");
  319. m_ReadWritePathType = serializedObject.FindProperty("m_ReadWritePathType");
  320. m_MinUnloadUnusedAssetsInterval = serializedObject.FindProperty("m_MinUnloadUnusedAssetsInterval");
  321. m_MaxUnloadUnusedAssetsInterval = serializedObject.FindProperty("m_MaxUnloadUnusedAssetsInterval");
  322. m_AssetAutoReleaseInterval = serializedObject.FindProperty("m_AssetAutoReleaseInterval");
  323. m_AssetCapacity = serializedObject.FindProperty("m_AssetCapacity");
  324. m_AssetExpireTime = serializedObject.FindProperty("m_AssetExpireTime");
  325. m_AssetPriority = serializedObject.FindProperty("m_AssetPriority");
  326. m_ResourceAutoReleaseInterval = serializedObject.FindProperty("m_ResourceAutoReleaseInterval");
  327. m_ResourceCapacity = serializedObject.FindProperty("m_ResourceCapacity");
  328. m_ResourceExpireTime = serializedObject.FindProperty("m_ResourceExpireTime");
  329. m_ResourcePriority = serializedObject.FindProperty("m_ResourcePriority");
  330. m_UpdatePrefixUri = serializedObject.FindProperty("m_UpdatePrefixUri");
  331. m_GenerateReadWriteVersionListLength = serializedObject.FindProperty("m_GenerateReadWriteVersionListLength");
  332. m_UpdateRetryCount = serializedObject.FindProperty("m_UpdateRetryCount");
  333. m_InstanceRoot = serializedObject.FindProperty("m_InstanceRoot");
  334. m_LoadResourceAgentHelperCount = serializedObject.FindProperty("m_LoadResourceAgentHelperCount");
  335. m_EditorResourceModeFieldInfo = target.GetType().GetField("m_EditorResourceMode", BindingFlags.NonPublic | BindingFlags.Instance);
  336. m_ResourceHelperInfo.Init(serializedObject);
  337. m_LoadResourceAgentHelperInfo.Init(serializedObject);
  338. RefreshModes();
  339. RefreshTypeNames();
  340. }
  341. private void DrawLoadAssetInfo(GameFramework.TaskInfo loadAssetInfo)
  342. {
  343. EditorGUILayout.LabelField(loadAssetInfo.Description, Utility.Text.Format("[SerialId]{0} [Priority]{1} [Status]{2}", loadAssetInfo.SerialId.ToString(), loadAssetInfo.Priority.ToString(), loadAssetInfo.Status.ToString()));
  344. }
  345. private void RefreshModes()
  346. {
  347. m_ResourceModeIndex = m_ResourceMode.enumValueIndex > 0 ? m_ResourceMode.enumValueIndex - 1 : 0;
  348. }
  349. private void RefreshTypeNames()
  350. {
  351. m_ResourceHelperInfo.Refresh();
  352. m_LoadResourceAgentHelperInfo.Refresh();
  353. serializedObject.ApplyModifiedProperties();
  354. }
  355. }
  356. }