Cesium for Unity 1.15.2
Loading...
Searching...
No Matches
CesiumRuntimeSettings.cs
Go to the documentation of this file.
1using UnityEngine;
2using System;
3
4
5#if UNITY_EDITOR
6using UnityEditor;
7#endif
8
9namespace CesiumForUnity
10{
14 public sealed class CesiumRuntimeSettings : ScriptableObject
15 {
16 private static readonly string _settingsName = "CesiumSettings";
17 private static readonly string _filePath =
18 "Assets/" + _settingsName + "/Resources/CesiumRuntimeSettings.asset";
19
20 private static CesiumRuntimeSettings _instance;
21
27 {
28 get
29 {
30 if (_instance != null)
31 {
32 return _instance;
33 }
34
35 #if UNITY_EDITOR
36 _instance = AssetDatabase.LoadAssetAtPath(_filePath, typeof(CesiumRuntimeSettings))
38 #else
39 _instance =
40 Resources.Load("CesiumRuntimeSettings") as CesiumRuntimeSettings;
41 #endif
42
43 #if UNITY_EDITOR
44 if (_instance == null)
45 {
46 // Create the necessary folders if they don't already exist.
47 if (!AssetDatabase.IsValidFolder("Assets/" + _settingsName))
48 {
49 AssetDatabase.CreateFolder("Assets", _settingsName);
50 }
51
52 if (!AssetDatabase.IsValidFolder("Assets/" + _settingsName + "/Resources"))
53 {
54 AssetDatabase.CreateFolder("Assets/" + _settingsName, "Resources");
55 }
56
57 string typeString = "t:"+ typeof(CesiumRuntimeSettings).Name;
58
59 string[] instanceGUIDS = AssetDatabase.FindAssets(typeString);
60
61 // If a CesiumRuntimeSettings asset is found outside of the preferred
62 // file path, move it to the correct location.
63 if (instanceGUIDS.Length > 0)
64 {
65 if (instanceGUIDS.Length > 1)
66 {
67 Debug.LogWarning("Found multiple CesiumRuntimeSettings assets " +
68 "in the project folder. The first asset found will be used.");
69 }
70
71 string oldPath = AssetDatabase.GUIDToAssetPath(instanceGUIDS[0]);
72 _instance =
73 AssetDatabase.LoadAssetAtPath(oldPath, typeof(CesiumRuntimeSettings))
75 if(_instance != null)
76 {
77 string result = AssetDatabase.MoveAsset(oldPath, _filePath);
78 AssetDatabase.Refresh();
79 if (string.IsNullOrEmpty(result))
80 {
81 Debug.LogWarning("A CesiumRuntimeSettings asset was found outside " +
82 "the Assets/" + _settingsName + "/Resources folder and has been moved " +
83 "appropriately.");
84
85 return _instance;
86 }
87 else
88 {
89 Debug.LogWarning("A CesiumRuntimeSettings asset was found outside " +
90 "the Assets/" + _settingsName + "/Resources folder, but could not " +
91 "be moved to the appropriate location. A new settings asset will be " +
92 "created instead.");
93 }
94 }
95 else
96 {
97 Debug.LogWarning("An invalid CesiumRuntimeSettings asset was found " +
98 "outside the Assets/" + _settingsName + "/Resources folder. " +
99 "A new settings asset will be created instead.");
100 }
101 }
102 }
103 #endif
104
105 if (_instance == null)
106 {
107 // Create an instance even if the game is not running in the editor
108 // to prevent a crash.
109 _instance = ScriptableObject.CreateInstance<CesiumRuntimeSettings>();
110 #if UNITY_EDITOR
111 AssetDatabase.CreateAsset(_instance, _filePath);
112 AssetDatabase.Refresh();
113 #else
114 Debug.LogError("Cannot find a CesiumRuntimeSettings asset. " +
115 "Any assets that use the project's default token will not load.");
116 #endif
117 }
118
119 return _instance;
120 }
121 }
122
123 [SerializeField]
124 private string _defaultIonAccessTokenID = "";
125
129 [Obsolete("Define a CesiumIonServer instead.")]
130 public static string defaultIonAccessTokenID
131 {
132 get => instance._defaultIonAccessTokenID;
133 #if UNITY_EDITOR
134 set
135 {
136 instance._defaultIonAccessTokenID = value;
137 EditorUtility.SetDirty(_instance);
138 AssetDatabase.SaveAssetIfDirty(_instance);
139 AssetDatabase.Refresh();
140 }
141 #endif
142 }
143
144 [SerializeField]
145 private string _defaultIonAccessToken = "";
146
150 [Obsolete("Define a CesiumIonServer instead.")]
151 public static string defaultIonAccessToken
152 {
153 get => instance._defaultIonAccessToken;
154 #if UNITY_EDITOR
155 set
156 {
157 instance._defaultIonAccessToken = value;
158 EditorUtility.SetDirty(_instance);
159 AssetDatabase.SaveAssetIfDirty(_instance);
160 AssetDatabase.Refresh();
161 }
162 #endif
163 }
164
165 [SerializeField]
166 [Tooltip("The number of requests to handle before each prune of old cached results from the database. Must restart Unity to apply changes.")]
167 private int _requestsPerCachePrune = 10000;
168
172 public static int requestsPerCachePrune
173 {
174 get => instance._requestsPerCachePrune;
175 }
176
177 [SerializeField]
178 [Tooltip("The maximum number of items should be kept in the Sqlite database after pruning. Must restart Unity to apply changes.")]
179 private ulong _maxItems = 4096;
183 public static ulong maxItems
184 {
185 get => instance._maxItems;
186 }
187 }
188}
Holds Cesium settings used at runtime.
static string defaultIonAccessToken
The default Cesium ion access token value to use within the project.
static ulong maxItems
The maximum number of items should be kept in the Sqlite database after pruning.
static string defaultIonAccessTokenID
The ID of the default Cesium ion access token to use within the project.
static CesiumRuntimeSettings instance
Gets the singleton instance of this class.
static int requestsPerCachePrune
The number of requests to handle before each prune of old cached results from the database.