XRBuildProcessorHelper.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.Build;
  4. using UnityEditor.Build.Reporting;
  5. using UnityEngine;
  6. namespace UnityEditor.XR.Management
  7. {
  8. /// <summary>
  9. /// Base abstract class that provides some common functionality for plugins wishing to integrate with management assisted build.
  10. /// </summary>
  11. /// <typeparam name="T">The type parameter that will be used as the base type of the settings.</typeparam>
  12. public abstract class XRBuildHelper<T> : IPreprocessBuildWithReport, IPostprocessBuildWithReport where T : UnityEngine.Object
  13. {
  14. /// <summary>Override of base IXxxprocessBuildWithReport</summary>
  15. /// <returns>The callback order.</returns>
  16. public virtual int callbackOrder { get { return 0; } }
  17. /// <summary>Override of base IXxxprocessBuildWithReport</summary>
  18. /// <returns>A string specifying the key to be used to set/get settigns in EditorBuildSettings.</returns>
  19. public abstract string BuildSettingsKey { get; }
  20. /// <summary>Helper functin to return current settings for a specific build target.</summary>
  21. ///
  22. /// <param name="buildTargetGroup">An enum specifying which platform group this build is for.</param>
  23. /// <returns>A unity object representing the settings instance data for that build target, or null if not found.</returns>
  24. public virtual UnityEngine.Object SettingsForBuildTargetGroup(BuildTargetGroup buildTargetGroup)
  25. {
  26. UnityEngine.Object settingsObj = null;
  27. EditorBuildSettings.TryGetConfigObject(BuildSettingsKey, out settingsObj);
  28. if (settingsObj == null || !(settingsObj is T))
  29. return null;
  30. return settingsObj;
  31. }
  32. void CleanOldSettings()
  33. {
  34. BuildHelpers.CleanOldSettings<T>();
  35. }
  36. void SetSettingsForRuntime(UnityEngine.Object settingsObj)
  37. {
  38. // Always remember to cleanup preloaded assets after build to make sure we don't
  39. // dirty later builds with assets that may not be needed or are out of date.
  40. CleanOldSettings();
  41. if (settingsObj == null)
  42. return;
  43. if (!(settingsObj is T))
  44. {
  45. Type typeOfT = typeof(T);
  46. Debug.LogErrorFormat("Settings object is not of type {0}. No settings will be copied to runtime.", typeOfT.Name);
  47. return;
  48. }
  49. UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
  50. if (!preloadedAssets.Contains(settingsObj))
  51. {
  52. var assets = preloadedAssets.ToList();
  53. assets.Add(settingsObj);
  54. PlayerSettings.SetPreloadedAssets(assets.ToArray());
  55. }
  56. }
  57. /// <summary>Override of base IPreprocessBuildWithReport</summary>
  58. ///
  59. /// <param name="report">BuildReport instance passed in from build pipeline.</param>
  60. public virtual void OnPreprocessBuild(BuildReport report)
  61. {
  62. SetSettingsForRuntime(SettingsForBuildTargetGroup(report.summary.platformGroup));
  63. }
  64. /// <summary>Override of base IPostprocessBuildWithReport</summary>
  65. ///
  66. /// <param name="report">BuildReport instance passed in from build pipeline.</param>
  67. public virtual void OnPostprocessBuild(BuildReport report)
  68. {
  69. // Always remember to cleanup preloaded assets after build to make sure we don't
  70. // dirty later builds with assets that may not be needed or are out of date.
  71. CleanOldSettings();
  72. }
  73. }
  74. }