12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- using Sirenix.OdinInspector;
- namespace KairoEngine.UI.Encyclopedia
- {
- [HideMonoScript]
- public class EncyclopediaArticleUi : MonoBehaviour, IEncyclopediaArticleDataView
- {
- public enum UiType
- {
- String,
- Integer,
- Float,
- Image,
- }
-
- public string articleDataKey = "title";
- public UiType uiType = UiType.String;
- [ShowIf("@uiType != UiType.Image")] public TextMeshProUGUI textMeshPro;
- [ShowIf("@uiType == UiType.Image")] public Image imageComponent;
- public bool showDebug = false;
- public void Setup(EncyclopediaArticle article)
- {
- string result = "";
- Sprite spriteImage = null;
- switch (uiType)
- {
- case UiType.String:
- article.content.TryGetValue(articleDataKey, out result);
- break;
- case UiType.Integer:
- article.integers.TryGetValue(articleDataKey, out int intValue);
- result += intValue;
- break;
- case UiType.Float:
- article.floats.TryGetValue(articleDataKey, out float floatValue);
- result += floatValue;
- break;
- case UiType.Image:
- article.images.TryGetValue(articleDataKey, out spriteImage);
- break;
- default:
- break;
- }
- if(uiType != UiType.Image && textMeshPro != null)
- {
- textMeshPro.text = result;
- if(imageComponent != null) imageComponent.gameObject.SetActive(false);
- }
- else if(uiType == UiType.Image && imageComponent != null)
- {
- if(spriteImage != null)
- {
- imageComponent.sprite = spriteImage;
- imageComponent.gameObject.SetActive(true);
- }
- else imageComponent.gameObject.SetActive(false);
-
- }
- if(showDebug && (result == null || result == ""))
- Debug.Log($"Article did not contain content key {articleDataKey}. Content key count: {article.content.Keys.Count}", this.gameObject);
- }
- }
- }
|