GCZLLayer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityAsync;
  5. using WaitUntil = UnityAsync.WaitUntil;
  6. using System.Threading.Tasks;
  7. using UnityEngine.UI;
  8. public class GCZLLayer : MonoBehaviour
  9. {
  10. public GameObject classPrefab;
  11. public GameObject informationPrefab;
  12. public RectTransform classContent;
  13. public RectTransform informationContent;
  14. public YZTLayer zTLayer;
  15. public Sprite[] sprites;
  16. private List<GameObject> informations = new List<GameObject>();
  17. private GameObject lastClassBtn;
  18. private int currentClass = -99;
  19. // Start is called before the first frame update
  20. async void Start()
  21. {
  22. await InitData();
  23. StaticLod.instance.OnFoucusStatic(0);
  24. InitButton();
  25. InitInformation(0);
  26. }
  27. void InitButton() {
  28. for (int i = 0; i < zTLayer.layerDatas.Length; i++) {
  29. string name = zTLayer.layerDatas[i].layerName;
  30. int temp = i;
  31. GameObject obj = Instantiate(classPrefab);
  32. obj.transform.SetParent(classContent);
  33. obj.GetComponentInChildren<Text>().text = name;
  34. obj.transform.localScale = Vector3.one;
  35. if (i == 0)
  36. {
  37. obj.GetComponent<Image>().sprite = sprites[0];
  38. lastClassBtn = obj;
  39. }
  40. else
  41. {
  42. obj.GetComponent<Image>().sprite = sprites[1];
  43. }
  44. obj.GetComponent<Button>().onClick.AddListener(()=> {
  45. InitInformation(temp);
  46. lastClassBtn.GetComponent<Image>().sprite = sprites[1];
  47. obj.GetComponent<Image>().sprite = sprites[0];
  48. lastClassBtn = obj;
  49. });
  50. }
  51. }
  52. void InitInformation(int index) {
  53. if (currentClass == index) return;
  54. for (int i = informations.Count - 1; i >= 0; i--) {
  55. Destroy(informations[i]);
  56. }
  57. informations.Clear();
  58. currentClass = index;
  59. if (currentClass == 0)
  60. {
  61. for (int i = 0; i < GlobalData.layerUnitDatas.Count; i++)
  62. {
  63. if (GlobalData.layerUnitDatas[i].special)
  64. {
  65. GameObject obj = Instantiate(informationPrefab);
  66. string realName = GlobalData.layerUnitDatas[i].name;
  67. string priName = GlobalData.layerUnitDatas[i].name_pri;
  68. obj.transform.SetParent(informationContent);
  69. obj.transform.Find("Name").GetComponentInChildren<Text>().text = realName;
  70. obj.transform.localScale = Vector3.one;
  71. informations.Add(obj);
  72. obj.GetComponent<Button>().onClick.AddListener(() =>
  73. {
  74. StaticLod.instance.OnFoucusStatic(priName);
  75. });
  76. }
  77. }
  78. }
  79. else {
  80. for (int i = 0; i < GlobalData.layerUnitDatas.Count; i++)
  81. {
  82. if (GlobalData.layerUnitDatas[i].type == (LayerUnitType)currentClass)
  83. {
  84. GameObject obj = Instantiate(informationPrefab);
  85. string realName = GlobalData.layerUnitDatas[i].name;
  86. string priName = GlobalData.layerUnitDatas[i].name_pri;
  87. obj.transform.SetParent(informationContent);
  88. obj.transform.Find("Name").GetComponentInChildren<Text>().text = realName;
  89. obj.transform.localScale = Vector3.one;
  90. informations.Add(obj);
  91. obj.GetComponent<Button>().onClick.AddListener(() =>
  92. {
  93. StaticLod.instance.OnFoucusStatic(priName);
  94. });
  95. }
  96. }
  97. }
  98. }
  99. async Task InitData()
  100. {
  101. await new WaitUntil(() =>
  102. {
  103. return GlobalData.layerUnitDatas.Count > 0;
  104. });
  105. }
  106. // Update is called once per frame
  107. void Update()
  108. {
  109. }
  110. }