EncyclopediaCategoryUi.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using KairoEngine.UI.Encyclopedia;
  6. using Sirenix.OdinInspector;
  7. using TMPro;
  8. namespace KairoEngine.UI.Encyclopedia
  9. {
  10. public class EncyclopediaCategoryUi : MonoBehaviour, IEncyclopediaCategoryDataView
  11. {
  12. public enum UiType
  13. {
  14. Title,
  15. Id,
  16. Icon,
  17. Image,
  18. Layout,
  19. Articles
  20. }
  21. public UiType uiType = UiType.Title;
  22. [ShowIf("@!IsImage()")] public TextMeshProUGUI textMeshPro;
  23. [ShowIf("@IsImage()")] public Image imageComponent;
  24. public bool IsImage() => uiType == UiType.Image || uiType == UiType.Icon;
  25. public void Setup(EncyclopediaCategory category)
  26. {
  27. string result = "";
  28. Sprite spriteImage = null;
  29. switch(uiType)
  30. {
  31. case UiType.Title:
  32. result = category.title;
  33. break;
  34. case UiType.Id:
  35. result = category.id;
  36. break;
  37. case UiType.Icon:
  38. spriteImage = category.icon;
  39. break;
  40. case UiType.Image:
  41. spriteImage = category.image;
  42. break;
  43. case UiType.Layout:
  44. result = category.layoutName;
  45. break;
  46. case UiType.Articles:
  47. result = $"{category.GetArticles().Count} {(category.GetArticles().Count == 1 ? category.titleSingle : category.titlePlural)}";
  48. break;
  49. default:
  50. break;
  51. }
  52. if (!IsImage() && textMeshPro != null)
  53. {
  54. textMeshPro.text = result;
  55. if(imageComponent != null) imageComponent.gameObject.SetActive(false);
  56. }
  57. else if(IsImage() && imageComponent != null)
  58. {
  59. if(spriteImage != null)
  60. {
  61. imageComponent.sprite = spriteImage;
  62. imageComponent.gameObject.SetActive(true);
  63. }
  64. else imageComponent.gameObject.SetActive(false);
  65. }
  66. }
  67. }
  68. }