EncyclopediaArticleUi.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using Sirenix.OdinInspector;
  7. [HideMonoScript]
  8. public class EncyclopediaArticleUi : MonoBehaviour, IEncyclopediaArticleDataView
  9. {
  10. public enum UiType
  11. {
  12. String,
  13. Integer,
  14. Float,
  15. Image,
  16. }
  17. public string articleDataKey = "title";
  18. public UiType uiType = UiType.String;
  19. [ShowIf("@uiType != UiType.Image")] public TextMeshProUGUI textMeshPro;
  20. [ShowIf("@uiType == UiType.Image")] public Image imageComponent;
  21. public bool showDebug = false;
  22. public void Setup(EncyclopediaArticle article)
  23. {
  24. string result = "";
  25. Sprite spriteImage = null;
  26. switch (uiType)
  27. {
  28. case UiType.String:
  29. article.content.TryGetValue(articleDataKey, out result);
  30. break;
  31. case UiType.Integer:
  32. article.integers.TryGetValue(articleDataKey, out int intValue);
  33. result += intValue;
  34. break;
  35. case UiType.Float:
  36. article.floats.TryGetValue(articleDataKey, out float floatValue);
  37. result += floatValue;
  38. break;
  39. case UiType.Image:
  40. article.images.TryGetValue(articleDataKey, out spriteImage);
  41. break;
  42. default:
  43. break;
  44. }
  45. if(uiType != UiType.Image && textMeshPro != null)
  46. {
  47. textMeshPro.text = result;
  48. if(imageComponent != null) imageComponent.gameObject.SetActive(false);
  49. }
  50. else if(uiType == UiType.Image && imageComponent != null)
  51. {
  52. if(spriteImage != null)
  53. {
  54. imageComponent.sprite = spriteImage;
  55. imageComponent.gameObject.SetActive(true);
  56. }
  57. else imageComponent.gameObject.SetActive(false);
  58. }
  59. if(showDebug && (result == null || result == ""))
  60. Debug.Log($"Article did not contain content key {articleDataKey}. Content key count: {article.content.Keys.Count}", this.gameObject);
  61. }
  62. }