BuildPipelineProcesor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditor.Callbacks;
  6. using System;
  7. using System.IO;
  8. using UnityEditor.Build;
  9. using UnityEditor.Build.Reporting;
  10. namespace Ximmerse.XR
  11. {
  12. public class BuildPlayerProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
  13. {
  14. /// <summary>
  15. /// The subpath of tracking profile files to streaming assets.
  16. /// </summary>
  17. const string kTrackingProfiles = "/TrackingProfiles";
  18. public int callbackOrder => 1;
  19. /// <summary>
  20. /// Copy tracking profiles from plugin to streaming assets.
  21. /// </summary>
  22. static void CopyTrackingProfiles()
  23. {
  24. string streamingAssets = Application.streamingAssetsPath;
  25. if (!Directory.Exists(streamingAssets))
  26. {
  27. Directory.CreateDirectory(streamingAssets);
  28. }
  29. string destPath = streamingAssets + kTrackingProfiles;
  30. if (!Directory.Exists(destPath))
  31. {
  32. Directory.CreateDirectory(destPath);
  33. }
  34. if (Directory.Exists(SDKEditorUtility.kPluginTrackingProfilePath))
  35. {
  36. var jsons = Directory.GetFiles(SDKEditorUtility.kPluginTrackingProfilePath, "*.json", SearchOption.TopDirectoryOnly);
  37. var datas = Directory.GetFiles(SDKEditorUtility.kPluginTrackingProfilePath, "*.dat", SearchOption.TopDirectoryOnly);
  38. foreach (var json in jsons)
  39. {
  40. string newFileName = Path.Combine(destPath, Path.GetFileName(json));
  41. File.Copy(json, newFileName, true);
  42. Debug.LogFormat("Copy: {0}=>{1}", json, newFileName);
  43. }
  44. foreach (var data in datas)
  45. {
  46. string newFileName = Path.Combine(destPath, Path.GetFileName(data));
  47. File.Copy(data, newFileName, true);
  48. Debug.LogFormat("Copy: {0}=>{1}", data, newFileName);
  49. }
  50. Debug.LogFormat("All tracking profiles has been copied from {0} => {1}", SDKEditorUtility.kPluginTrackingProfilePath, destPath);
  51. }
  52. }
  53. /// <summary>
  54. /// Cleanup tracking profiles after building player.
  55. /// </summary>
  56. static void CleanupTrackingProfiles()
  57. {
  58. string streamingAssets = Application.streamingAssetsPath;
  59. if (!Directory.Exists(streamingAssets))
  60. {
  61. return;
  62. }
  63. string destPath = streamingAssets + kTrackingProfiles;
  64. if (!Directory.Exists(destPath))
  65. {
  66. return;
  67. }
  68. Directory.Delete(destPath, true); //delete the tracking profiles directory
  69. string metaDestPath = destPath + ".meta";
  70. if (File.Exists(metaDestPath))
  71. {
  72. File.Delete(metaDestPath); //delete dest path meta
  73. }
  74. var f = Directory.GetFiles(streamingAssets);
  75. if (f.Length == 0)
  76. {
  77. Directory.Delete(streamingAssets, true); //delete the sa dir if empty
  78. string metaSA = streamingAssets + ".meta";
  79. if (File.Exists(metaSA))
  80. {
  81. File.Delete(metaSA); //delete SA meta
  82. }
  83. }
  84. }
  85. public void OnPostprocessBuild(BuildReport report)
  86. {
  87. if (report.summary.platform == BuildTarget.Android)
  88. {
  89. CleanupTrackingProfiles();
  90. }
  91. }
  92. public void OnPreprocessBuild(BuildReport report)
  93. {
  94. if (report.summary.platform == BuildTarget.Android)
  95. {
  96. CopyTrackingProfiles();
  97. }
  98. }
  99. }
  100. }