2using System.Collections;
3using System.Collections.Generic;
5using UnityEngine.EventSystems;
6using UnityEngine.UIElements;
9using UnityEngine.InputSystem.UI;
22 [RequireComponent(typeof(UIDocument))]
23 [AddComponentMenu(
"Cesium/Cesium Credit System UI")]
24 [IconAttribute(
"Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
25 internal class CesiumCreditSystemUI : MonoBehaviour
27 private CesiumCreditSystem _creditSystem;
29 private UIDocument _uiDocument;
31 private VisualElement _onScreenCredits;
32 private VisualElement _popupCredits;
36 private string _delimiter =
"\u2022";
38 private void OnEnable()
40 this._creditSystem = this.GetComponent<CesiumCreditSystem>();
42 if (this._creditSystem ==
null)
44 this._creditSystem = CesiumCreditSystem.GetDefaultCreditSystem();
47 this._creditSystem.OnCreditsUpdate += this.SetCredits;
49 this._uiDocument = this.GetComponent<UIDocument>();
51 if (this._uiDocument.rootVisualElement !=
null)
53 this._onScreenCredits = this._uiDocument.rootVisualElement.Q(
"OnScreenCredits");
54 this._popupCredits = this._uiDocument.rootVisualElement.Q(
"PopupCredits");
58 if (!EditorApplication.isPlaying)
65 if (EventSystem.current ==
null)
67 GameObject eventSystemGameObject =
new GameObject(
"EventSystem");
68 eventSystemGameObject.AddComponent<EventSystem>();
70#if ENABLE_INPUT_SYSTEM
71 eventSystemGameObject.AddComponent<InputSystemUIInputModule>();
72#elif ENABLE_LEGACY_INPUT_MANAGER
73 eventSystemGameObject.AddComponent<StandaloneInputModule>();
79 private void AddCreditsToSceneView(SceneView sceneView)
81 if (sceneView.rootVisualElement ==
null)
86 if (sceneView.rootVisualElement.Q(
"OnScreenCredits") ==
null)
88 VisualTreeAsset visualTreeAsset = this._uiDocument.visualTreeAsset;
89 TemplateContainer tree = visualTreeAsset.Instantiate();
94 VisualElement onScreenCredits = tree.Q(
"OnScreenCredits");
95 VisualElement popupCredits = tree.Q(
"PopupCredits");
96 sceneView.rootVisualElement.Add(onScreenCredits);
97 sceneView.rootVisualElement.Add(popupCredits);
99 this.UpdateCreditsInSceneView(
101 this._creditSystem.onScreenCredits,
102 this._creditSystem.popupCredits);
106 private void UpdateCreditsInSceneView(
108 List<CesiumCredit> onScreenCredits,
109 List<CesiumCredit> popupCredits)
111 if (sceneView.rootVisualElement ==
null)
116 VisualElement onScreenElement = sceneView.rootVisualElement.Q(
"OnScreenCredits");
117 VisualElement popupElement = sceneView.rootVisualElement.Q(
"PopupCredits");
119 if (onScreenElement !=
null && popupElement !=
null)
121 this.SetCreditsOnVisualElements(
130 private void RemoveCreditsFromSceneView(SceneView sceneView)
132 if (sceneView.rootVisualElement !=
null)
134 VisualElement onScreenElement = sceneView.rootVisualElement.Q(
"OnScreenCredits");
135 if (onScreenElement !=
null)
137 VisualElement parent = onScreenElement.parent;
138 parent.Remove(onScreenElement);
141 VisualElement popupElement = sceneView.rootVisualElement.Q(
"PopupCredits");
142 if (popupElement !=
null)
144 VisualElement parent = popupElement.parent;
145 parent.Remove(popupElement);
152 private void Update()
154 if (this._creditSystem ==
null)
160 ArrayList sceneViews = SceneView.sceneViews;
161 for (
int i = 0; i < sceneViews.Count; i++)
163 this.AddCreditsToSceneView((SceneView)sceneViews[i]);
175 private Label CreateLabelFromText(
string text,
bool removeExtraSpace)
177 Label label =
new Label();
179 label.style.whiteSpace = WhiteSpace.Normal;
181 if (removeExtraSpace)
183 label.style.marginLeft =
new StyleLength(0.0f);
184 label.style.paddingLeft =
new StyleLength(0.0f);
185 label.style.paddingRight =
new StyleLength(0.0f);
198 private List<VisualElement> ConvertCreditToVisualElements(CesiumCredit credit,
bool removeExtraSpace)
200 List<VisualElement> visualElements =
new List<VisualElement>();
202 for (
int i = 0, componentCount = credit.components.Count; i < componentCount; i++)
204 CesiumCreditComponent creditComponent = credit.components[i];
205 VisualElement element;
207 bool hasLink = !
string.IsNullOrEmpty(creditComponent.link);
209 if (creditComponent.imageId >= 0)
211 Texture2D image =
null;
212 if (creditComponent.imageId <
this._creditSystem.images.Count)
214 image = this._creditSystem.images[creditComponent.imageId];
222 element =
new VisualElement();
223 element.style.backgroundImage =
new StyleBackground(image);
224 element.style.width =
new StyleLength(image.width);
225 element.style.height =
new StyleLength(image.height);
229 string text = creditComponent.text;
233 text =
string.Format(
"<u>{0}</u>", text);
236 element = this.CreateLabelFromText(text, removeExtraSpace);
241 element.AddManipulator(
new Clickable(evt => Application.OpenURL(creditComponent.link)));
244 visualElements.Add(element);
247 return visualElements;
256 private VisualElement CreateDataAttributionElement(VisualElement popupElement)
258 Label label =
new Label();
259 label.text =
"<u>Data Attribution</u>";
260 label.AddManipulator(
new Clickable(evt =>
262 if (popupElement.style.display == DisplayStyle.Flex)
264 popupElement.style.display = DisplayStyle.None;
268 popupElement.style.display = DisplayStyle.Flex;
284 private VisualElement CreatePopupCreditElement(
bool removeExtraSpace)
286 VisualElement popupCreditElement =
new VisualElement();
287 popupCreditElement.style.flexDirection = FlexDirection.Row;
288 popupCreditElement.style.flexWrap = Wrap.Wrap;
289 popupCreditElement.style.alignItems = Align.Center;
291 if (!removeExtraSpace)
293 popupCreditElement.style.marginTop =
new StyleLength(2.5f);
297 popupCreditElement.style.marginTop =
new StyleLength(0.0f);
298 popupCreditElement.style.marginBottom =
new StyleLength(0.0f);
299 popupCreditElement.style.paddingTop =
new StyleLength(0.0f);
300 popupCreditElement.style.paddingBottom =
new StyleLength(0.0f);
303 return popupCreditElement;
306 private void SetCredits(List<CesiumCredit> onScreenCredits, List<CesiumCredit> popupCredits)
308 if (this._onScreenCredits !=
null && this._popupCredits !=
null)
310 this.SetCreditsOnVisualElements(
311 this._onScreenCredits,
319 ArrayList sceneViews = SceneView.sceneViews;
320 for (
int i = 0; i < sceneViews.Count; i++)
322 this.UpdateCreditsInSceneView((SceneView)sceneViews[i], onScreenCredits, popupCredits);
338 private void SetCreditsOnVisualElements(
339 VisualElement onScreenElement,
340 List<CesiumCredit> onScreenCredits,
341 VisualElement popupElement,
342 List<CesiumCredit> popupCredits,
343 bool removeExtraSpace)
345 onScreenElement.Clear();
346 popupElement.Clear();
348 for (
int i = 0, creditCount = onScreenCredits.Count; i < creditCount; i++)
350 CesiumCredit credit = onScreenCredits[i];
351 List<VisualElement> visualElements = this.ConvertCreditToVisualElements(credit, removeExtraSpace);
355 onScreenElement.Add(this.CreateLabelFromText(this._delimiter,
false));
358 for (
int j = 0, elementCount = visualElements.Count; j < elementCount; j++)
360 onScreenElement.Add(visualElements[j]);
364 for (
int i = 0, creditCount = popupCredits.Count; i < creditCount; i++)
366 CesiumCredit credit = popupCredits[i];
367 List<VisualElement> visualElements = this.ConvertCreditToVisualElements(credit, removeExtraSpace);
370 VisualElement popupCreditElement = CreatePopupCreditElement(removeExtraSpace);
372 for (
int j = 0, elementCount = visualElements.Count; j < elementCount; j++)
374 popupCreditElement.Add(visualElements[j]);
377 popupElement.Add(popupCreditElement);
380 if (popupCredits.Count > 0)
382 if (onScreenCredits.Count > 0)
384 onScreenElement.Add(this.CreateLabelFromText(this._delimiter,
false));
387 onScreenElement.Add(this.CreateDataAttributionElement(popupElement));
391 private void OnDisable()
394 ArrayList sceneViews = SceneView.sceneViews;
395 for (
int i = 0; i < sceneViews.Count; i++)
397 this.RemoveCreditsFromSceneView((SceneView)sceneViews[i]);