Cesium for Unity 1.25.1
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 = new List<CesiumCredit>();
101 private List<CesiumCredit> _popupCredits = new List<CesiumCredit>();
102 private List<Texture2D> _images = new List<Texture2D>();
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.Clear();
159 this._popupCredits.Clear();
160 this.ClearImages();
161
162 Cesium3DTileset.OnSetShowCreditsOnScreen += this.ForceUpdateCredits;
163 }
164
165 private void Update()
166 {
167 this.UpdateCredits(false);
168 }
169
170 private void OnDisable()
171 {
172 this._onScreenCredits.Clear();
173 this._popupCredits.Clear();
174 this.ClearImages();
175
176 Cesium3DTileset.OnSetShowCreditsOnScreen -= this.ForceUpdateCredits;
177 }
178
179 private void OnDestroy()
180 {
181 if (this == _defaultCreditSystem)
182 {
183 _defaultCreditSystem = null;
184 }
185 }
186
191 private void OnApplicationQuit()
192 {
193 UnityLifetime.Destroy(this.gameObject);
194 }
195 #endregion
196
202 private void ForceUpdateCredits()
203 {
204 this.UpdateCredits(true);
205 }
206
211 private partial void UpdateCredits(bool forceUpdate);
212
213 internal void BroadcastCreditsUpdate()
214 {
215 if (this.OnCreditsUpdate != null)
216 {
217 this.OnCreditsUpdate(this._onScreenCredits, this._popupCredits);
218 }
219 }
220
230 private static CesiumCreditSystem CreateDefaultCreditSystem()
231 {
232 GameObject creditSystemPrefab = Resources.Load<GameObject>(creditSystemPrefabName);
233 GameObject creditSystemGameObject = UnityEngine.Object.Instantiate(creditSystemPrefab);
234 creditSystemGameObject.name = defaultName;
235 creditSystemGameObject.hideFlags = HideFlags.HideAndDontSave;
236
237 return creditSystemGameObject.GetComponent<CesiumCreditSystem>();
238 }
239
245 {
246 if (_defaultCreditSystem == null)
247 {
248 CesiumCreditSystem[] creditSystems = Resources.FindObjectsOfTypeAll<CesiumCreditSystem>();
249 for (int i = 0; i < creditSystems.Length; i++)
250 {
251 if (creditSystems[i].gameObject.name == defaultName)
252 {
253 _defaultCreditSystem = creditSystems[i];
254 break;
255 }
256 }
257 }
258
259 if (_defaultCreditSystem == null)
260 {
261 _defaultCreditSystem = CreateDefaultCreditSystem();
262 }
263
264 return _defaultCreditSystem;
265 }
266
271 internal int GetNumberOfLoadingImages()
272 {
273 return this._numLoadingImages;
274 }
275
280 internal IEnumerator LoadImage(string url)
281 {
282 int index = this._images.Count;
283 this._numLoadingImages++;
284
285 // Initialize a texture of arbitrary size as a placeholder,
286 // so that when other images are loaded, their IDs align properly
287 // with the current list of images.
288 Texture2D texture = new Texture2D(1, 1);
289 this._images.Add(texture);
290
291 if (url.LastIndexOf(base64Prefix, base64Prefix.Length) == 0)
292 {
293 // Load an image from a string that contains the
294 // "data:image/png;base64," prefix
295 string byteString = url.Substring(base64Prefix.Length);
296 try
297 {
298 byte[] bytes = Convert.FromBase64String(byteString);
299 if (!texture.LoadImage(bytes))
300 {
301 Debug.Log("Credit image could not be loaded into Texture2D.");
302 }
303 }
304 catch (FormatException)
305 {
306 Debug.Log("Could not parse credit image from base64 string.");
307 }
308 }
309 else
310 {
311 // Load an image from a URL.
312 UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
313 yield return request.SendWebRequest();
314
315 if (request.result == UnityWebRequest.Result.ConnectionError ||
316 request.result == UnityWebRequest.Result.ProtocolError)
317 {
318 Debug.LogError(request.error);
319 }
320 else
321 {
322 texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
323
324 Texture2D placeholderTexture = this._images[index];
325 this._images[index] = texture;
326 UnityLifetime.Destroy(placeholderTexture);
327 }
328
329 }
330
331 texture.wrapMode = TextureWrapMode.Clamp;
332 this._numLoadingImages--;
333 }
334
335 private void ClearImages()
336 {
337 for (int i = 0, count = this._images.Count; i < count; i++)
338 {
339 UnityLifetime.Destroy(this._images[i]);
340 }
341
342 this._images.Clear();
343 }
344 }
345}
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.