ContentIconList.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using BeinLab.Util;
  2. using ShadowStudio.Model;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using XRTool.Util;
  9. using XRTool.WorldUI;
  10. public class ContentIconList : MonoBehaviour
  11. {
  12. private List<GameObject> IconViewList;
  13. //private List<ArtInfo> ArtInfoList;
  14. public GameObject IconItem;
  15. private RectTransform contentRect;
  16. public ArtType artType;
  17. private int rightnum;
  18. private int leftnum;
  19. //private void CreateSuccess(ArtContainer container)
  20. //{
  21. // for (int i = 0; i < IconViewList.Count; i++)
  22. // {
  23. // IconItem Item = IconViewList[i].transform.GetComponent<IconItem>();
  24. // if (Item.info.ArtId == container.GetGoodsInfo().art_id)
  25. // {
  26. // Item.CreateSuccess(container);
  27. // }
  28. // }
  29. //}
  30. private void Start()
  31. {
  32. IconViewList = new List<GameObject>();
  33. contentRect = transform.GetComponent<RectTransform>();
  34. if (ArtInfoMgr.Instance != null && ArtInfoMgr.Instance.IsInit)
  35. {
  36. //Init();
  37. }
  38. else
  39. {
  40. ArtInfoMgr.Instance.InitComplete += () =>
  41. {
  42. //Init();
  43. };
  44. }
  45. }
  46. public void Init()
  47. {
  48. var ArtInfoList = ArtInfoMgr.Instance.GetArtInfoList(artType);
  49. //UnityLog.Instance.LogError("artType"+ artType+ "count:"+ArtInfoList.Count);
  50. OnItemCreat(ArtInfoList.Count);
  51. for (int i = 0; i < IconViewList.Count; i++)
  52. {
  53. IconItem Item = IconViewList[i].transform.GetComponent<IconItem>();
  54. Item.Init(ArtInfoList[i]);
  55. }
  56. //ArtInfoList.Clear();
  57. //ArtInfoList = null;
  58. }
  59. void Update()
  60. {
  61. //CheckContent();
  62. }
  63. void CheckContent()
  64. {
  65. if (contentRect.anchoredPosition3D.x > 0)
  66. {
  67. contentRect.anchoredPosition3D = Vector3.zero;
  68. }
  69. for (int i = 0; i < IconViewList.Count; i++)
  70. {
  71. RectTransform rectTransform;
  72. rectTransform = IconViewList[i].transform.GetComponent<RectTransform>();
  73. float num1 = Mathf.Abs(contentRect.anchoredPosition3D.x);
  74. if (rectTransform.anchoredPosition3D.x > 0)
  75. {
  76. //rectTransform.anchoredPosition3D = new Vector3(0, 0, 0);
  77. }
  78. if (rectTransform.anchoredPosition3D.x < num1 || rectTransform.anchoredPosition3D.x > 200 + num1)
  79. {
  80. IconViewList[i].transform.GetChild(0).gameObject.SetActive(false);
  81. }
  82. else
  83. {
  84. IconViewList[i].transform.GetChild(0).gameObject.SetActive(true);
  85. }
  86. }
  87. }
  88. //void Checkright()
  89. //{
  90. // float num = 80 + contentRect.anchoredPosition3D.x;
  91. // if (num > 0)
  92. // {
  93. // if (num >= 40)
  94. // {
  95. // leftnum = -1;
  96. // rightnum = 6;
  97. // }
  98. // else
  99. // {
  100. // leftnum = 0;
  101. // rightnum = 7;
  102. // }
  103. // }
  104. // else
  105. // {
  106. // float num1 = num / 80;
  107. // float num2 = Mathf.Abs(num1);
  108. // int leftnumTemp = Mathf.CeilToInt(num2);
  109. // Debug.Log(leftnumTemp+"AAA");
  110. // Debug.Log(num2+"BBB");
  111. // if (leftnumTemp > num2 + 0.5)
  112. // {
  113. // leftnum = leftnumTemp - 1;
  114. // }
  115. // else
  116. // {
  117. // leftnum = leftnumTemp;
  118. // }
  119. // rightnum = 7 + leftnum;
  120. // Debug.Log(leftnum);
  121. // Debug.Log(rightnum);
  122. // }
  123. // for (int i = 0; i < IconChildList.Count; i++)
  124. // {
  125. // if (i <= leftnum || i >= rightnum)
  126. // {
  127. // IconChildList[i].SetActive(false);
  128. // }
  129. // else
  130. // {
  131. // IconChildList[i].SetActive(true);
  132. // }
  133. // }
  134. //}
  135. public void OnItemCreat(int _count)
  136. {
  137. if (_count > 0)
  138. {
  139. //IconItem.SetActive(true); //第一个item实例已经放在列表第一个位置,直接激活
  140. //IconViewList.Add(IconItem);
  141. int i = 0;
  142. while (i <= _count - 1)
  143. {
  144. GameObject obj = GameObject.Instantiate(IconItem) as GameObject;
  145. obj.transform.SetParent(transform); //设置为 Content 的子对象
  146. IconViewList.Add(obj);
  147. RectTransform t = IconViewList[i].GetComponent<RectTransform>(); //获取前一个 item 的位置
  148. //当前 item 位置放在在前一个 item 下方
  149. obj.GetComponent<RectTransform>().localPosition =
  150. new Vector3(t.localPosition.x, t.localPosition.y - t.rect.height, t.localPosition.z);
  151. obj.GetComponent<RectTransform>().localEulerAngles = new Vector3(0, 0, 0);
  152. obj.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
  153. i++;
  154. }
  155. //根据当前 item 个数更新 Content 高度
  156. transform.GetComponent<RectTransform>().sizeDelta =
  157. new Vector2(transform.GetComponent<RectTransform>().sizeDelta.x, 30);
  158. }
  159. }
  160. }