AssetsMonitor.Static.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. namespace XGame.Framework.Asset.Monitor
  4. {
  5. public sealed partial class AssetsMonitor
  6. {
  7. private static AssetsMonitor _instance;
  8. private static AssetsMonitor Instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. {
  14. _instance = new AssetsMonitor();
  15. }
  16. return _instance;
  17. }
  18. }
  19. /// <summary>
  20. /// 记录加载数据
  21. /// </summary>
  22. /// <param name="addressableName"></param>
  23. /// <param name="obj"></param>
  24. [Conditional(MacroDefine.UNITY_EDITOR)]
  25. [Conditional(MacroDefine.UNITY_STANDALONE)]
  26. [Conditional(MacroDefine.UNITY_STANDALONE_WIN)]
  27. public static void RecordAsset(string addressableName, object obj)
  28. {
  29. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
  30. Instance.RecordAssetInternal(addressableName, obj);
  31. #endif
  32. }
  33. /// <summary>
  34. /// 获取所有已加载过的资源数据
  35. /// </summary>
  36. /// <returns></returns>
  37. public static AssetLoadingRecord[] GetRecords()
  38. {
  39. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
  40. return Instance.GetRecordsInternal();
  41. #else
  42. return null;
  43. #endif
  44. }
  45. [Conditional(MacroDefine.UNITY_EDITOR)]
  46. public static void Write()
  47. {
  48. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
  49. Instance.WriteInternal();
  50. #endif
  51. }
  52. [Conditional(MacroDefine.UNITY_EDITOR)]
  53. public static void Dispose()
  54. {
  55. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
  56. if (_instance != null)
  57. {
  58. //Write();
  59. _instance._assetInfoMap.Clear();
  60. _instance._assetInfoMap = null;
  61. }
  62. _instance = null;
  63. #endif
  64. }
  65. #region AssetModule
  66. /// <summary>
  67. /// 返回指定AssetModule的实例数据
  68. /// </summary>
  69. /// <param name="assetModule"></param>
  70. /// <returns></returns>
  71. public static AssetInstanceCopy[] GetAssetInstances(IAssetModule assetModule)
  72. {
  73. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
  74. if (assetModule is AssetModule impl)
  75. {
  76. var field = impl.GetType().GetField("assetMap", System.Reflection.BindingFlags.NonPublic |
  77. System.Reflection.BindingFlags.Instance);
  78. if (field != null)
  79. {
  80. var assetMap = field.GetValue(impl) as Dictionary<long, AssetInstance>;
  81. var instances = new AssetInstanceCopy[assetMap.Count];
  82. var index = 0;
  83. foreach(var item in assetMap)
  84. {
  85. var instance = item.Value;
  86. instances[index++] = new AssetInstanceCopy()
  87. {
  88. asset = instance.Asset,
  89. reference = instance.Reference,
  90. hashcode = instance.Hashcode,
  91. name = instance.Name
  92. };
  93. }
  94. return instances;
  95. }
  96. }
  97. #endif
  98. return null;
  99. }
  100. #endregion
  101. }
  102. }