AssetsMonitor.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
  2. using XGame.Framework.Define;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace XGame.Framework.Asset.Monitor
  9. {
  10. public sealed partial class AssetsMonitor
  11. {
  12. private string MonitorPath => Path.Combine(PathDefine.SandboxPath, "AssetsMonitor.log");
  13. private Dictionary<string, AssetLoadingRecord> _assetInfoMap = new Dictionary<string, AssetLoadingRecord>();
  14. private void RecordAssetInternal(string addressableName, object obj)
  15. {
  16. if (!_assetInfoMap.ContainsKey(addressableName))
  17. {
  18. var info = new AssetLoadingRecord()
  19. {
  20. addressableName = addressableName,
  21. loadedTime = UnityEngine.Time.realtimeSinceStartup,
  22. contents = obj.GetType().ToString(),
  23. };
  24. _assetInfoMap.Add(addressableName, info);
  25. }
  26. }
  27. private AssetLoadingRecord[] GetRecordsInternal()
  28. {
  29. return _assetInfoMap.Values.ToArray();
  30. }
  31. private void WriteInternal()
  32. {
  33. try
  34. {
  35. var path = MonitorPath;
  36. if (System.IO.File.Exists(path))
  37. {
  38. System.IO.File.Delete(path);
  39. }
  40. var sb = new StringBuilder();
  41. if (_assetInfoMap != null)
  42. {
  43. sb.AppendLine("AssetLoadingRecord Begin.");
  44. foreach (var item in _assetInfoMap)
  45. {
  46. sb.AppendLine(item.Value.ToString());
  47. }
  48. sb.AppendLine("AssetLoadingRecord End.");
  49. }
  50. System.IO.File.WriteAllText(path, sb.ToString());
  51. UnityEngine.Debug.Log("AssetsMonitor Write End.");
  52. }
  53. catch (Exception ex)
  54. {
  55. UnityEngine.Debug.LogException(ex);
  56. }
  57. }
  58. }
  59. }
  60. #endif