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