HotReloadBuildHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEditor.Build;
  5. using UnityEngine;
  6. namespace SingularityGroup.HotReload.Editor {
  7. internal static class HotReloadBuildHelper {
  8. /// <summary>
  9. /// Should HotReload runtime be included in the current build?
  10. /// </summary>
  11. public static bool IncludeInThisBuild() {
  12. return IsAllBuildSettingsSupported();
  13. }
  14. /// <summary>
  15. /// Get scripting backend for the current platform.
  16. /// </summary>
  17. /// <returns>Scripting backend</returns>
  18. public static ScriptingImplementation GetCurrentScriptingBackend() {
  19. #pragma warning disable CS0618
  20. return PlayerSettings.GetScriptingBackend(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget));
  21. #pragma warning restore CS0618
  22. }
  23. public static ManagedStrippingLevel GetCurrentStrippingLevel() {
  24. #pragma warning disable CS0618
  25. return PlayerSettings.GetManagedStrippingLevel(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget));
  26. #pragma warning restore CS0618
  27. }
  28. public static void SetCurrentScriptingBackend(ScriptingImplementation to) {
  29. #pragma warning disable CS0618
  30. // only set it if default is not correct (avoid changing ProjectSettings when not needed)
  31. if (GetCurrentScriptingBackend() != to) {
  32. PlayerSettings.SetScriptingBackend(EditorUserBuildSettings.selectedBuildTargetGroup, to);
  33. }
  34. #pragma warning restore CS0618
  35. }
  36. public static void SetCurrentStrippingLevel(ManagedStrippingLevel to) {
  37. #pragma warning disable CS0618
  38. // only set it if default is not correct (avoid changing ProjectSettings when not needed)
  39. if (GetCurrentStrippingLevel() != to) {
  40. PlayerSettings.SetManagedStrippingLevel(EditorUserBuildSettings.selectedBuildTargetGroup, to);
  41. }
  42. #pragma warning restore CS0618
  43. }
  44. /// Is the current build target supported?
  45. /// main thread only
  46. public static bool IsBuildTargetSupported() {
  47. var buildTarget = EditorUserBuildSettings.selectedBuildTargetGroup;
  48. return Array.IndexOf(unsupportedBuildTargets, buildTarget) == -1;
  49. }
  50. /// Are all the settings supported?
  51. /// main thread only
  52. static bool IsAllBuildSettingsSupported() {
  53. if (!IsBuildTargetSupported()) {
  54. return false;
  55. }
  56. // need way to give it settings object, dont want to give serializedobject
  57. var options = HotReloadSettingsEditor.LoadSettingsOrDefault();
  58. var so = new SerializedObject(options);
  59. // check all projeect options
  60. foreach (var option in HotReloadSettingsTab.allOptions) {
  61. var projectOption = option as ProjectOptionBase;
  62. if (projectOption != null) {
  63. // if option is required, build can't use hot reload
  64. if (projectOption.IsRequiredForBuild() && !projectOption.GetValue(so)) {
  65. return false;
  66. }
  67. }
  68. }
  69. return GetCurrentScriptingBackend() == ScriptingImplementation.Mono2x
  70. && GetCurrentStrippingLevel() == ManagedStrippingLevel.Disabled
  71. && EditorUserBuildSettings.development;
  72. }
  73. /// <summary>
  74. /// Some platforms are not supported because they don't have Mono scripting backend.
  75. /// </summary>
  76. /// <remarks>
  77. /// Only list the platforms that definately don't have Mono scripting.
  78. /// </remarks>
  79. private static readonly BuildTargetGroup[] unsupportedBuildTargets = new [] {
  80. BuildTargetGroup.iOS, // mono support was removed many years ago
  81. BuildTargetGroup.WebGL, // has never had mono
  82. };
  83. #pragma warning disable CS0618
  84. public static bool IsMonoSupported(BuildTargetGroup buildTarget) {
  85. var backend = PlayerSettings.GetScriptingBackend(buildTarget);
  86. try {
  87. // GetDefaultScriptingBackend returns IL2CPP for Unity 6 which goes against Unity documentation.
  88. // Have to use a workaround approach instead
  89. PlayerSettings.SetScriptingBackend(buildTarget, ScriptingImplementation.Mono2x);
  90. return PlayerSettings.GetScriptingBackend(buildTarget) == ScriptingImplementation.Mono2x;
  91. } catch {
  92. return false;
  93. } finally {
  94. PlayerSettings.SetScriptingBackend(buildTarget, backend);
  95. }
  96. }
  97. #pragma warning restore CS0618
  98. }
  99. }