PrebuildIncludeResources.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using UnityEditor;
  3. using UnityEditor.Build;
  4. using UnityEngine;
  5. namespace SingularityGroup.HotReload.Editor {
  6. /// <summary>Includes HotReload Resources only in development builds</summary>
  7. /// <remarks>
  8. /// This build script ensures that HotReload Resources are not included in release builds.
  9. /// <para>
  10. /// When HotReload is enabled:<br/>
  11. /// - include HotReloadSettingsObject in development Android builds.<br/>
  12. /// - exclude HotReloadSettingsObject from the build.<br/>
  13. /// When HotReload is disabled:<br/>
  14. /// - excludes HotReloadSettingsObject from the build.<br/>
  15. /// </para>
  16. /// </remarks>
  17. #pragma warning disable CS0618
  18. internal class PrebuildIncludeResources : IPreprocessBuild, IPostprocessBuild {
  19. #pragma warning restore CS0618
  20. public int callbackOrder => 10;
  21. // Preprocess warnings don't show up in console
  22. bool warnSettingsNotSupported;
  23. public void OnPreprocessBuild(BuildTarget target, string path) {
  24. try {
  25. if (HotReloadBuildHelper.IncludeInThisBuild()) {
  26. // move scriptable object into Resources/ folder
  27. HotReloadSettingsEditor.AddOrRemoveFromBuild(true);
  28. } else {
  29. // make sure HotReload resources are not in the build
  30. HotReloadSettingsEditor.AddOrRemoveFromBuild(false);
  31. var options = HotReloadSettingsEditor.LoadSettingsOrDefault();
  32. var so = new SerializedObject(options);
  33. if (IncludeInBuildOption.I.GetValue(so)) {
  34. warnSettingsNotSupported = true;
  35. }
  36. }
  37. } catch (BuildFailedException) {
  38. throw;
  39. } catch (Exception ex) {
  40. throw new BuildFailedException(ex);
  41. }
  42. }
  43. public void OnPostprocessBuild(BuildTarget target, string path) {
  44. if (warnSettingsNotSupported) {
  45. Debug.LogWarning("Hot Reload was not included in the build because one or more build settings were not supported.");
  46. }
  47. }
  48. // Do nothing in post build. settings asset will be dirty if build fails, so not worth fixing just for successful builds.
  49. // [PostProcessBuild]
  50. // private static void PostBuild(BuildTarget target, string pathToBuiltProject) {
  51. // }
  52. }
  53. }