Cesium for Unity 1.16.0
Loading...
Searching...
No Matches
CesiumCreditSystem.cs
Go to the documentation of this file.
1using Reinterop;
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using UnityEngine;
6using UnityEngine.SceneManagement;
7using UnityEngine.Networking;
8
9#if UNITY_EDITOR
10using UnityEditor.SceneManagement;
11using UnityEditor;
12#endif
13
14namespace CesiumForUnity
15{
21 internal class CesiumCreditComponent
22 {
23 private string _text;
24 private string _link;
25 private int _imageId = -1;
26
30 public string text
31 {
32 get => this._text;
33 }
34
39 public string link
40 {
41 get => this._link;
42 }
43
51 public int imageId
52 {
53 get => this._imageId;
54 }
55
56 public CesiumCreditComponent(string text, string link, int imageId)
57 {
58 this._text = text;
59 this._link = link;
60 this._imageId = imageId;
61 }
62 }
63
68 internal class CesiumCredit
69 {
70 private List<CesiumCreditComponent> _components;
71
75 public List<CesiumCreditComponent> components
76 {
77 get => this._components;
78 }
79
80 public CesiumCredit() : this(new List<CesiumCreditComponent>())
81 { }
82
83 public CesiumCredit(List<CesiumCreditComponent> components)
84 {
85 this._components = components;
86 }
87 }
88
92 [ExecuteInEditMode]
93 [ReinteropNativeImplementation("CesiumForUnityNative::CesiumCreditSystemImpl", "CesiumCreditSystemImpl.h")]
94 [AddComponentMenu("Cesium/Cesium Credit System")]
95 [IconAttribute("Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
96 public partial class CesiumCreditSystem : MonoBehaviour
97 {
98 private static CesiumCreditSystem _defaultCreditSystem;
99
100 private List<CesiumCredit> _onScreenCredits;
101 private List<CesiumCredit> _popupCredits;
102 private List<Texture2D> _images;
103 private int _numLoadingImages = 0;
104
105 const string base64Prefix = "data:image/png;base64,";
106 const string creditSystemPrefabName = "CesiumCreditSystem";
107 const string defaultName = "CesiumCreditSystemDefault";
108
109 #region Fields and Events
113 internal List<CesiumCredit> onScreenCredits
114 {
115 get => this._onScreenCredits;
116 }
117
122 internal List<CesiumCredit> popupCredits
123 {
124 get => this._popupCredits;
125 }
126
130 internal List<Texture2D> images
131 {
132 get => this._images;
133 }
134
141 internal delegate void CreditsUpdateDelegate(List<CesiumCredit> onScreenCredits, List<CesiumCredit> onPopupCredits);
142
151 internal event CreditsUpdateDelegate OnCreditsUpdate;
152 #endregion
153
154 #region Unity Messages
155
156 private void OnEnable()
157 {
158 this._onScreenCredits = new List<CesiumCredit>();
159 this._popupCredits = new List<CesiumCredit>();
160 this._images = new List<Texture2D>();
161
162 Cesium3DTileset.OnSetShowCreditsOnScreen += this.ForceUpdateCredits;
163 }
164
165 private void Update()
166 {
167 this.UpdateCredits(false);
168 }
169
170 private void OnDestroy()
171 {
172 Cesium3DTileset.OnSetShowCreditsOnScreen -= this.ForceUpdateCredits;
173
174 for (int i = 0, count = this._images.Count; i < count; i++)
175 {
176 if (this._images != null)
177 {
178 UnityLifetime.Destroy(this._images[i]);
179 }
180 }
181
182 this._images.Clear();
183
184 if (this == _defaultCreditSystem)
185 {
186 _defaultCreditSystem = null;
187 }
188 }
189
194 private void OnApplicationQuit()
195 {
196 UnityLifetime.Destroy(this.gameObject);
197 }
198 #endregion
199
205 private void ForceUpdateCredits()
206 {
207 this.UpdateCredits(true);
208 }
209
214 private partial void UpdateCredits(bool forceUpdate);
215
216 internal void BroadcastCreditsUpdate()
217 {
218 if (this.OnCreditsUpdate != null)
219 {
220 this.OnCreditsUpdate(this._onScreenCredits, this._popupCredits);
221 }
222 }
223
233 private static CesiumCreditSystem CreateDefaultCreditSystem()
234 {
235 GameObject creditSystemPrefab = Resources.Load<GameObject>(creditSystemPrefabName);
236 GameObject creditSystemGameObject = UnityEngine.Object.Instantiate(creditSystemPrefab);
237 creditSystemGameObject.name = defaultName;
238 creditSystemGameObject.hideFlags = HideFlags.HideAndDontSave;
239
240 return creditSystemGameObject.GetComponent<CesiumCreditSystem>();
241 }
242
248 {
249 if (_defaultCreditSystem == null)
250 {
251 CesiumCreditSystem[] creditSystems = Resources.FindObjectsOfTypeAll<CesiumCreditSystem>();
252 for (int i = 0; i < creditSystems.Length; i++)
253 {
254 if (creditSystems[i].gameObject.name == defaultName)
255 {
256 _defaultCreditSystem = creditSystems[i];
257 break;
258 }
259 }
260 }
261
262 if (_defaultCreditSystem == null)
263 {
264 _defaultCreditSystem = CreateDefaultCreditSystem();
265 }
266
267 return _defaultCreditSystem;
268 }
269
270 internal bool HasLoadingImages()
271 {
272 return this._numLoadingImages > 0;
273 }
274
275 internal IEnumerator LoadImage(string url)
276 {
277 int index = this._images.Count;
278
279 // Initialize a texture of arbitrary size as a placeholder,
280 // so that when other images are loaded, their IDs align properly
281 // with the current list of images.
282 Texture2D texture = new Texture2D(1, 1);
283 this._images.Add(texture);
284
285 if (url.LastIndexOf(base64Prefix, base64Prefix.Length) == 0)
286 {
287 // Load an image from a string that contains the
288 // "data:image/png;base64," prefix
289 string byteString = url.Substring(base64Prefix.Length);
290 byte[] bytes = Convert.FromBase64String(byteString);
291 if (!texture.LoadImage(bytes))
292 {
293 Debug.Log("Could not parse image from base64 string.");
294 }
295 }
296 else
297 {
298 // Load an image from a URL.
299 UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
300 this._numLoadingImages++;
301 yield return request.SendWebRequest();
302
303 if (request.result == UnityWebRequest.Result.ConnectionError ||
304 request.result == UnityWebRequest.Result.ProtocolError)
305 {
306 Debug.Log(request.error);
307 }
308 else
309 {
310 texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
311
312 Texture2D placeholderTexture = this._images[index];
313 this._images[index] = texture;
314 UnityLifetime.Destroy(placeholderTexture);
315 }
316
317 this._numLoadingImages--;
318 }
319
320 texture.wrapMode = TextureWrapMode.Clamp;
321 }
322 }
323}
Manages credits / attribution for Cesium3DTileset and CesiumRasterOverlay.
static CesiumCreditSystem GetDefaultCreditSystem()
Gets the default credit system, or creates a new default credit system instance if none exist.