Cesium for Unity 1.18.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
274 internal int GetNumberOfLoadingImages()
275 {
276 return this._numLoadingImages;
277 }
278
283 internal IEnumerator LoadImage(string url)
284 {
285 int index = this._images.Count;
286 this._numLoadingImages++;
287
288 // Initialize a texture of arbitrary size as a placeholder,
289 // so that when other images are loaded, their IDs align properly
290 // with the current list of images.
291 Texture2D texture = new Texture2D(1, 1);
292 this._images.Add(texture);
293
294 if (url.LastIndexOf(base64Prefix, base64Prefix.Length) == 0)
295 {
296 // Load an image from a string that contains the
297 // "data:image/png;base64," prefix
298 string byteString = url.Substring(base64Prefix.Length);
299 try
300 {
301 byte[] bytes = Convert.FromBase64String(byteString);
302 if (!texture.LoadImage(bytes))
303 {
304 Debug.Log("Credit image could not be loaded into Texture2D.");
305 }
306 }
307 catch (FormatException e)
308 {
309 Debug.Log("Could not parse credit image from base64 string.");
310 }
311 }
312 else
313 {
314 // Load an image from a URL.
315 UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
316 yield return request.SendWebRequest();
317
318 if (request.result == UnityWebRequest.Result.ConnectionError ||
319 request.result == UnityWebRequest.Result.ProtocolError)
320 {
321 Debug.LogError(request.error);
322 }
323 else
324 {
325 texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
326
327 Texture2D placeholderTexture = this._images[index];
328 this._images[index] = texture;
329 UnityLifetime.Destroy(placeholderTexture);
330 }
331
332 }
333
334 texture.wrapMode = TextureWrapMode.Clamp;
335 this._numLoadingImages--;
336 }
337 }
338}
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.