SampleBuildProcessor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEditor.Build;
  4. using UnityEditor.Build.Reporting;
  5. namespace Samples
  6. {
  7. /// <summary>
  8. /// Simple build processor that makes sure that any custom configuration that the user creates is
  9. /// correctly passed on to the provider implementation at runtime.
  10. ///
  11. /// Custom configuration instances that are stored in EditorBuildSettings are not copied to the target build
  12. /// as they are considered unreferenced assets. In order to get them to the runtime side of things, they need
  13. /// to be serialized to the build app and deserialized at runtime. Previously this would be a manual process
  14. /// requiring the implementor to manually serialize to some location that can then be read from to deserialize
  15. /// at runtime. With the new PlayerSettings Preloaded Assets API we can now just add our asset to the preloaded
  16. /// list and have it be instantiated at app launch.
  17. ///
  18. /// Note that the preloaded assets are only notified with Awake, so anything you want or need to do with the
  19. /// asset after launch needs to be handled there.
  20. ///
  21. /// More info on APIs used here:
  22. /// * &lt;a href="https://docs.unity3d.com/ScriptReference/EditorBuildSettings.html"&gt;EditorBuildSettings&lt;/a&gt;
  23. /// * &lt;a href="https://docs.unity3d.com/ScriptReference/PlayerSettings.GetPreloadedAssets.html&gt;PlayerSettings.GetPreloadedAssets&lt;/a&gt;
  24. /// * &lt;a href="https://docs.unity3d.com/ScriptReference/PlayerSettings.SetPreloadedAssets.html"&gt;PlayerSettings.SetPreloadedAssets&lt;/a&gt;
  25. /// </summary>
  26. public class SampleBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
  27. {
  28. /// <summary>Override of <see cref="IPreprocessBuildWithReport"/> and <see cref="IPostprocessBuildWithReport"/></summary>
  29. public int callbackOrder
  30. {
  31. get { return 0; }
  32. }
  33. void CleanOldSettings()
  34. {
  35. UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
  36. if (preloadedAssets == null)
  37. return;
  38. var oldSettings = from s in preloadedAssets
  39. where s != null && s.GetType() == typeof(SampleSettings)
  40. select s;
  41. if (oldSettings != null && oldSettings.Any())
  42. {
  43. var assets = preloadedAssets.ToList();
  44. foreach (var s in oldSettings)
  45. {
  46. assets.Remove(s);
  47. }
  48. PlayerSettings.SetPreloadedAssets(assets.ToArray());
  49. }
  50. }
  51. /// <summary>Override of <see cref="IPreprocessBuildWithReport"/></summary>
  52. /// <param name="report">Build report.</param>
  53. public void OnPreprocessBuild(BuildReport report)
  54. {
  55. // Always remember to cleanup preloaded assets after build to make sure we don't
  56. // dirty later builds with assets that may not be needed or are out of date.
  57. CleanOldSettings();
  58. SampleSettings settings = null;
  59. EditorBuildSettings.TryGetConfigObject(SampleConstants.k_SettingsKey, out settings);
  60. if (settings == null)
  61. return;
  62. UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
  63. if (!preloadedAssets.Contains(settings))
  64. {
  65. var assets = preloadedAssets.ToList();
  66. assets.Add(settings);
  67. PlayerSettings.SetPreloadedAssets(assets.ToArray());
  68. }
  69. }
  70. /// <summary>Override of <see cref="IPostprocessBuildWithReport"/></summary>
  71. /// <param name="report">Build report.</param>
  72. public void OnPostprocessBuild(BuildReport report)
  73. {
  74. // Always remember to cleanup preloaded assets after build to make sure we don't
  75. // dirty later builds with assets that may not be needed or are out of date.
  76. CleanOldSettings();
  77. }
  78. }
  79. }