HotReloadSettingsObject.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #if ENABLE_MONO && (DEVELOPMENT_BUILD || UNITY_EDITOR)
  2. using System;
  3. using System.Linq;
  4. using JetBrains.Annotations;
  5. using System.IO;
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. namespace SingularityGroup.HotReload {
  11. /// <summary>
  12. /// HotReload runtime settings. These can be changed while the app is running.
  13. /// </summary>
  14. /// <remarks>
  15. /// ScriptableObject that may be included in Resources/ folder.
  16. /// See also Editor/PrebuildIncludeResources.cs
  17. /// </remarks>
  18. [Serializable]
  19. class HotReloadSettingsObject : ScriptableObject {
  20. #region singleton
  21. private static HotReloadSettingsObject _I;
  22. public static HotReloadSettingsObject I {
  23. get {
  24. if (_I == null) {
  25. _I = LoadSettingsOrDefault();
  26. }
  27. return _I;
  28. }
  29. }
  30. /// <summary>Create settings inside Assets/ because user cannot edit files that are included inside a Unity package</summary>
  31. /// <remarks>
  32. /// You can change this in a build script if you want it created somewhere else.
  33. /// </remarks>
  34. public static string editorAssetPath = "Assets/HotReload/Resources/HotReloadSettingsObject.asset";
  35. private static string resourceName => Path.GetFileNameWithoutExtension(editorAssetPath);
  36. public static bool TryLoadSettings(out HotReloadSettingsObject settings) {
  37. try {
  38. settings = LoadSettings();
  39. return settings != null;
  40. } catch(FileNotFoundException) {
  41. settings = null;
  42. return false;
  43. }
  44. }
  45. [NotNull]
  46. private static HotReloadSettingsObject LoadSettingsOrDefault() {
  47. var settings = LoadSettings();
  48. if (settings == null) {
  49. // load defaults
  50. settings = CreateInstance<HotReloadSettingsObject>();
  51. }
  52. return settings;
  53. }
  54. [CanBeNull]
  55. private static HotReloadSettingsObject LoadSettings() {
  56. HotReloadSettingsObject settings;
  57. if (Application.isEditor) {
  58. #if UNITY_EDITOR
  59. settings = AssetDatabase.LoadAssetAtPath<HotReloadSettingsObject>(editorAssetPath);
  60. #else
  61. settings = null;
  62. #endif
  63. } else {
  64. // load from Resources (assumes that build includes the resource)
  65. settings = Resources.Load<HotReloadSettingsObject>(resourceName);
  66. }
  67. return settings;
  68. }
  69. #endregion
  70. #region settings
  71. /// <summary>Set default values.</summary>
  72. /// <remarks>
  73. /// This is called by the Unity editor when the ScriptableObject is first created.
  74. /// This function is only called in editor mode.
  75. /// </remarks>
  76. private void Reset() {
  77. EnsurePrefabSetCorrectly();
  78. }
  79. /// <summary>
  80. /// Path to the prefab asset file.
  81. /// </summary>
  82. const string prefabAssetPath = "Packages/com.singularitygroup.hotreload/Runtime/HotReloadPrompts.prefab";
  83. // Call this during build, just to be sure the field is correct. (I had some issues with it while editing the prefab)
  84. public void EnsurePrefabSetCorrectly() {
  85. #if UNITY_EDITOR
  86. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabAssetPath);
  87. if (prefab == null) {
  88. // when you use HotReload as a unitypackage, prefab is somewhere inside your assets folder
  89. var guids = AssetDatabase.FindAssets("HotReloadPrompts t:prefab", new string[]{"Assets"});
  90. var paths = guids.Select(guid => AssetDatabase.GUIDToAssetPath(guid));
  91. var promptsPrefabPath = paths.FirstOrDefault(assetpath => Path.GetFileName(assetpath) == "HotReloadPrompts.prefab");
  92. if (promptsPrefabPath != null) {
  93. prefab = AssetDatabase.LoadAssetAtPath<GameObject>(promptsPrefabPath);
  94. }
  95. }
  96. if (prefab == null) {
  97. throw new Exception("Failed to find PromptsPrefab (are you using Hot Reload as a package?");
  98. }
  99. PromptsPrefab = prefab;
  100. #endif
  101. }
  102. public void EnsurePrefabNotInBuild() {
  103. #if UNITY_EDITOR
  104. PromptsPrefab = null;
  105. #endif
  106. }
  107. // put the stored settings here
  108. [Header("Build Settings")]
  109. [Tooltip("Should the Hot Reload runtime be included in development builds? HotReload is never included in release builds.")]
  110. public bool IncludeInBuild = true;
  111. [Header("Player Settings")]
  112. public bool AllowAndroidAppToMakeHttpRequests = false;
  113. #region hidden
  114. /// Reference to the Prefab, for loading it at runtime
  115. [HideInInspector]
  116. public GameObject PromptsPrefab;
  117. #endregion
  118. #endregion settings
  119. }
  120. }
  121. #endif