DunGenSettings.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. #endif
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEngine;
  7. using DunGen.Tags;
  8. using DunGen.Collision;
  9. namespace DunGen
  10. {
  11. public sealed class DunGenSettings : ScriptableObject
  12. {
  13. #region Singleton
  14. private static DunGenSettings instance;
  15. public static DunGenSettings Instance
  16. {
  17. get
  18. {
  19. if (instance != null)
  20. return instance;
  21. else
  22. {
  23. instance = FindOrCreateInstanceAsset();
  24. return instance;
  25. }
  26. }
  27. }
  28. public static DunGenSettings FindOrCreateInstanceAsset()
  29. {
  30. // Try to find an existing instance in a resource folder
  31. instance = Resources.Load<DunGenSettings>("DunGen Settings");
  32. // Create a new instance if one is not found
  33. if (instance == null)
  34. {
  35. #if UNITY_EDITOR
  36. instance = CreateInstance<DunGenSettings>();
  37. if (!Directory.Exists(Application.dataPath + "/Resources"))
  38. AssetDatabase.CreateFolder("Assets", "Resources");
  39. AssetDatabase.CreateAsset(instance, "Assets/Resources/DunGen Settings.asset");
  40. instance.defaultSocket = instance.GetOrAddSocketByName("Default");
  41. #else
  42. throw new System.Exception("No instance of DunGen settings was found.");
  43. #endif
  44. }
  45. return instance;
  46. }
  47. #endregion
  48. public DoorwaySocket DefaultSocket { get { return defaultSocket; } }
  49. public TagManager TagManager { get { return tagManager; } }
  50. /// <summary>
  51. /// Optional broadphase settings for speeding up collision tests
  52. /// </summary>
  53. [SubclassSelector]
  54. [SerializeReference]
  55. public BroadphaseSettings BroadphaseSettings = new SpatialHashBroadphaseSettings();
  56. /// <summary>
  57. /// If true, sprite components will be ignored when automatically calculating bounding volumes around tiles
  58. /// </summary>
  59. public bool BoundsCalculationsIgnoreSprites = false;
  60. /// <summary>
  61. /// If true, tile bounds will be automatically recalculated whenever a tile is saved. Otherwise, bounds must be recalculated manually using the button in the Tile inspector
  62. /// </summary>
  63. public bool RecalculateTileBoundsOnSave = true;
  64. /// <summary>
  65. /// If true, tile instances will be re-used instead of destroyed and re-created, improving generation performance at the cost of increased memory consumption
  66. /// </summary>
  67. public bool EnableTilePooling = false;
  68. /// <summary>
  69. /// If true, a window will be displayed when a generation failure occurs, allowing you to inspect the failure report
  70. /// </summary>
  71. public bool DisplayFailureReportWindow = true;
  72. /// <summary>
  73. /// Should the DunGen folder be checked for files that are no longer in use? If true, when loading DunGen will check if any old files are still present in the DunGen directory from previous DunGen version and will present the user with a list of files to potentially delete
  74. /// </summary>
  75. public bool CheckForUnusedFiles = true;
  76. [SerializeField]
  77. private DoorwaySocket defaultSocket = null;
  78. [SerializeField]
  79. private TagManager tagManager = new TagManager();
  80. private void OnValidate()
  81. {
  82. if(defaultSocket == null)
  83. defaultSocket = GetOrAddSocketByName("Default");
  84. }
  85. #if UNITY_EDITOR
  86. private DoorwaySocket GetOrAddSocketByName(string name)
  87. {
  88. string path = AssetDatabase.GetAssetPath(this);
  89. var socket = AssetDatabase.LoadAllAssetsAtPath(path)
  90. .OfType<DoorwaySocket>()
  91. .FirstOrDefault(x => x.name == name);
  92. if (socket != null)
  93. return socket;
  94. socket = CreateInstance<DoorwaySocket>();
  95. socket.name = name;
  96. AssetDatabase.AddObjectToAsset(socket, this);
  97. #if UNITY_2021_1_OR_NEWER
  98. AssetDatabase.SaveAssetIfDirty(socket);
  99. #else
  100. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(socket));
  101. #endif
  102. return socket;
  103. }
  104. #endif
  105. }
  106. }