ViewList.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. 
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6. using DG.Tweening;
  7. /// <summary>
  8. /// 滑动列表的优化,用少量元素显示很多物体. 修改自UIWarpConten 和gLoop --ljp20170328
  9. /// </summary>
  10. public class ViewList : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// 元素初始化
  14. /// </summary>
  15. /// <param name="item">游戏对象</param>
  16. /// <param name="wrapIndex">对象在prefab中的序号</param>
  17. /// <param name="realIndex">对象在数组中的序号</param>
  18. public delegate void OnInitializeItem(GameObject item, int wrapIndex, int realIndex);
  19. /// <summary>
  20. /// 元素初始化
  21. /// </summary>
  22. public OnInitializeItem onInitializeItem;
  23. /// <summary>
  24. /// 排列
  25. /// </summary>
  26. public enum Arrangement
  27. {
  28. /// <summary>
  29. /// 横
  30. /// </summary>
  31. Horizontal,
  32. /// <summary>
  33. /// 竖
  34. /// </summary>
  35. Vertical,
  36. }
  37. /// <summary>
  38. /// 排列方式
  39. /// </summary>
  40. public Arrangement arrangeType = Arrangement.Horizontal;
  41. /// <summary>
  42. /// The width of each of the cells.
  43. /// </summary>
  44. public float cellWidth = 200f;
  45. /// <summary>
  46. /// The height of each of the cells.
  47. /// </summary>
  48. public float cellHeight = 200f;
  49. /// <summary>
  50. /// 窗口能容纳的 行数 或 列数
  51. /// </summary>
  52. public int viewCount = 5;
  53. /// <summary>
  54. /// 对象最小序号
  55. /// </summary>
  56. public int minIndex = 0;
  57. /// <summary>
  58. /// 对象最大序号
  59. /// </summary>
  60. public int maxIndex = 0;
  61. /// <summary>
  62. ///当前对象
  63. /// </summary>
  64. Transform mTrans;
  65. /// <summary>
  66. /// 当前RectTransform对象
  67. /// </summary>
  68. RectTransform mRTrans;
  69. /// <summary>
  70. /// ScrollRect
  71. /// </summary>
  72. ScrollRect mScroll;
  73. /// <summary>
  74. /// 元素预制物体的容器对象
  75. /// </summary>
  76. public Transform content = null;
  77. /// <summary>
  78. /// 元素预制物体
  79. /// </summary>
  80. public GameObject goItemPrefab = null;
  81. /// <summary>
  82. /// 最少需要多少预制物体
  83. /// </summary>
  84. public int mMinNum = 0;
  85. /// <summary>
  86. /// 滚动方向
  87. /// </summary>
  88. bool mHorizontal;
  89. /// <summary>
  90. /// 元素链表
  91. /// </summary>
  92. List<Transform> mChild = new List<Transform>();
  93. /// <summary>
  94. /// 显示区域长度或高度的一半
  95. /// </summary>
  96. float extents = 0;
  97. Vector2 SR_size = Vector2.zero;//SrollRect的尺寸
  98. Vector3[] conners = new Vector3[4];//ScrollRect四角的世界坐标
  99. Vector2 startPos;//ScrollRect的初始位置
  100. /// <summary>
  101. /// 是否隐藏裁剪部分
  102. /// </summary>
  103. public bool cullContent = true;
  104. /// <summary>
  105. /// 将已有的对象初始化一下.
  106. /// </summary>
  107. public void SetData()
  108. {
  109. for (int i = 0; i < mChild.Count; i++)
  110. {
  111. Transform temp = mChild[i];
  112. UpdateItem(temp, i, i);
  113. }
  114. }
  115. /// <summary>
  116. /// 加载动画
  117. /// </summary>
  118. /// <param name="temp"></param>
  119. /// <param name="destPos"></param>
  120. /// <param name="i"></param>
  121. private void Dotween(Transform temp, Vector3 destPos, int i)
  122. {
  123. Tweener tweener = null;
  124. RectTransform panelTransform = temp.GetComponent<RectTransform>();
  125. temp.localPosition = new Vector3(destPos.x, -cellHeight * 5, 0);
  126. tweener = panelTransform.DOLocalMove(destPos, 0.3f);
  127. tweener.SetEase(Ease.Linear);//动画曲线
  128. tweener.SetDelay(i * 0.01f);
  129. }
  130. /// <summary>
  131. /// 实例化预设对象 、添加实例化对象到指定的子对象下
  132. /// </summary>
  133. /// <param name="goPrefab"> 元素预制物体 </param>
  134. /// <param name="parent"> 父节点 </param>
  135. /// <returns> 新创建的元素对象 </returns>
  136. private GameObject addChild(GameObject goPrefab, Transform parent)
  137. {
  138. if (goPrefab == null || parent == null)
  139. {
  140. LogHelper.LogError("异常。UIWarpContent addChild(goPrefab = null || parent = null)");
  141. return null;
  142. }
  143. GameObject goChild = GameObject.Instantiate(goPrefab) as GameObject;
  144. goChild.transform.SetParent(parent, false);
  145. goChild.SetActive(true);
  146. return goChild;
  147. }
  148. public void Init()
  149. {
  150. for (int i = 0; i < mMinNum; i++)
  151. {
  152. addChild(goItemPrefab, content);
  153. }
  154. }
  155. /// <summary>
  156. /// 开始函数,将动画放到这里,每当第一次运行会显示动画.
  157. /// </summary>
  158. void Start()
  159. {
  160. InitList();
  161. }
  162. /// <summary>
  163. /// 初始化mChild链表
  164. /// </summary>
  165. void InitList()
  166. {
  167. int i, ChildCount;
  168. InitValue();
  169. mChild.Clear();
  170. for (i = 0, ChildCount = mTrans.childCount; i < ChildCount; i++)
  171. mChild.Add(mTrans.GetChild(i));
  172. ////动画放到下面这个函数里
  173. ResetChildPosition();
  174. // LogHelper.Log(mTrans.localPosition.y + " " + startPos.y + " " + cellHeight);
  175. if (mTrans.localPosition.y > 2 * cellHeight && mTrans.localPosition.y<6 *cellHeight)
  176. {
  177. mTrans.localPosition = new Vector3(startPos.x, startPos.y - 3*cellHeight, 0);
  178. }
  179. }
  180. void InitValue()
  181. {
  182. if (viewCount <= 0)
  183. viewCount = 1;
  184. if (minIndex > maxIndex) minIndex = maxIndex;
  185. mTrans = transform;
  186. mRTrans = transform.GetComponent<RectTransform>();
  187. mScroll = transform.parent.GetComponent<ScrollRect>();
  188. mHorizontal = mScroll.horizontal;
  189. SR_size = transform.parent.GetComponent<RectTransform>().rect.size;
  190. //四角坐标 横着数
  191. conners[0] = new Vector3(-SR_size.x / 2f, SR_size.y / 2f, 0);
  192. conners[1] = new Vector3(SR_size.x / 2f, SR_size.y / 2f, 0);
  193. conners[2] = new Vector3(-SR_size.x / 2f, -SR_size.y / 2f, 0);
  194. conners[3] = new Vector3(SR_size.x / 2f, -SR_size.y / 2f, 0);
  195. for (int i = 0; i < 4; i++)
  196. {
  197. Vector3 temp = transform.parent.TransformPoint(conners[i]);
  198. conners[i].x = temp.x;
  199. conners[i].y = temp.y;
  200. }
  201. mRTrans.pivot = new Vector2(0, 1);//设置panel的中心在左上角
  202. mScroll.onValueChanged.AddListener(delegate { WrapContent(); });//添加滚动事件回调
  203. startPos = mTrans.localPosition;
  204. }
  205. //初始化各Item的坐标
  206. [ContextMenu("RePosition")]
  207. public virtual void RePosition()
  208. {
  209. InitList();
  210. }
  211. void Update()
  212. {
  213. if (Application.isPlaying) enabled = false;
  214. RePosition();
  215. }
  216. void ResetChildPosition()
  217. {
  218. int rows = 1, cols = 1;
  219. Vector2 startAxis = new Vector2(cellWidth / 10f, -cellHeight / 10f);//起始位置
  220. int i;
  221. int imax = mChild.Count;//Item元素数量
  222. //初始化行列数
  223. if (arrangeType == Arrangement.Vertical) //垂直排列 则适应行数
  224. {
  225. rows = viewCount;
  226. cols = (int)Mathf.Ceil((float)imax / (float)rows);
  227. extents = (float)(cols * cellWidth) * 0.5f;
  228. }
  229. else if (arrangeType == Arrangement.Horizontal) //水平排列则适应列数
  230. {
  231. cols = viewCount;
  232. rows = (int)Mathf.Ceil((float)imax / (float)cols);
  233. extents = (float)(rows * cellHeight) * 0.5f;
  234. }
  235. for (i = 0; i < imax; i++)
  236. {
  237. Transform temp = mChild[i];
  238. int x = 0, y = 0;//行列号
  239. if (arrangeType == Arrangement.Horizontal) { x = i / cols; y = i % cols; }
  240. else if (arrangeType == Arrangement.Vertical) { x = i % rows; y = i / rows; }
  241. Vector3 destPos = new Vector3(startAxis.x + y * cellWidth, startAxis.y - x * cellHeight, 0);
  242. ////动画显示,缓动到目的位置;
  243. //if (arrangeType == Arrangement.Vertical)
  244. //{
  245. // if (mRTrans.sizeDelta != new Vector2(destPos.x + cellWidth, viewCount * cellHeight))
  246. // {
  247. // Dotween(temp, destPos, i);
  248. // }
  249. // else
  250. // {
  251. // temp.localPosition = destPos;
  252. // }
  253. //}
  254. //else
  255. //{
  256. // if (mRTrans.sizeDelta != new Vector2(viewCount * cellWidth, -destPos.y + cellHeight))
  257. // {
  258. // Dotween(temp, destPos, i);
  259. // }
  260. // else
  261. // {
  262. // temp.localPosition = destPos;
  263. // }
  264. //}
  265. temp.localPosition = destPos;
  266. //
  267. if (minIndex == maxIndex || (i >= minIndex && i <= maxIndex))
  268. {
  269. cullContent = true;
  270. temp.gameObject.SetActive(true);
  271. UpdateRectsize(temp.localPosition);//更新panel的尺寸
  272. mTrans.localPosition = startPos;
  273. UpdateItem(temp, i, i);
  274. // Dotween(temp, destPos, i);
  275. }
  276. else cullContent = temp.gameObject.activeInHierarchy;//如果预制Item数超过maxIndex则将超过部分隐藏 并 设置cullCintent为ufalse 并且不再更新 panel尺寸
  277. }
  278. }
  279. /// <summary>
  280. /// 更新panel的尺寸
  281. /// </summary>
  282. /// <param name="pos"></param>
  283. void UpdateRectsize(Vector2 pos)
  284. {
  285. if (arrangeType == Arrangement.Vertical)
  286. {
  287. mRTrans.sizeDelta = new Vector2(pos.x + cellWidth, viewCount * cellHeight);
  288. }
  289. else
  290. {
  291. mRTrans.sizeDelta = new Vector2(viewCount * cellWidth, -pos.y + cellHeight);
  292. }
  293. }
  294. int getRealIndex(Vector2 pos)//计算realindex
  295. {
  296. int x = (int)Mathf.Ceil(-pos.y / cellHeight) - 1;//行号
  297. int y = (int)Mathf.Ceil(pos.x / cellWidth) - 1;//列号
  298. int realIndex;
  299. if (arrangeType == Arrangement.Horizontal) realIndex = x * viewCount + y;
  300. else realIndex = x + viewCount * y;
  301. return realIndex;
  302. }
  303. void WrapContent()
  304. {
  305. // LogHelper.Log("ffdds");
  306. Vector3[] conner_local = new Vector3[4];
  307. for (int i = 0; i < 4; i++)
  308. {
  309. conner_local[i] = mTrans.InverseTransformPoint(conners[i]);
  310. }
  311. //计算ScrollRect的中心坐标 相对于this的坐标
  312. Vector2 center = (conner_local[3] + conner_local[0]) / 2f;
  313. if (mHorizontal)
  314. {
  315. float min = conner_local[0].x - cellWidth;//显示区域
  316. float max = conner_local[3].x + cellWidth;
  317. for (int i = 0, imax = mChild.Count; i < imax; i++)
  318. {
  319. Transform temp = mChild[i];
  320. float distance = temp.localPosition.x - center.x;
  321. if (distance < -extents)
  322. {
  323. Vector2 pos = temp.localPosition;
  324. pos.x += extents * 2f;
  325. int realIndex = getRealIndex(pos);
  326. if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
  327. {
  328. UpdateRectsize(pos);
  329. temp.localPosition = pos;
  330. //设置Item内容
  331. UpdateItem(temp, i, realIndex);
  332. }
  333. }
  334. if (distance > extents)
  335. {
  336. Vector2 pos = temp.localPosition;
  337. pos.x -= extents * 2f;
  338. int realIndex = getRealIndex(pos);
  339. if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
  340. {
  341. temp.localPosition = pos;
  342. //设置Item内容
  343. UpdateItem(temp, i, realIndex);
  344. }
  345. }
  346. if (cullContent)//设置裁剪部分是否隐藏
  347. {
  348. Vector2 pos = temp.localPosition;
  349. temp.gameObject.SetActive((pos.x > min && pos.x < max) ? true : false);
  350. }
  351. }
  352. }
  353. else
  354. {
  355. float min = conner_local[3].y - cellHeight;//显示区域
  356. float max = conner_local[0].y + cellHeight;
  357. for (int i = 0, imax = mChild.Count; i < imax; i++)
  358. {
  359. Transform temp = mChild[i];
  360. float distance = temp.localPosition.y - center.y;
  361. if (distance < -extents)
  362. {
  363. Vector2 pos = temp.localPosition;
  364. pos.y += extents * 2f;
  365. int realIndex = getRealIndex(pos);
  366. if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
  367. {
  368. temp.localPosition = pos;
  369. //设置Item内容
  370. UpdateItem(temp, i, realIndex);
  371. }
  372. }
  373. if (distance > extents)
  374. {
  375. Vector2 pos = temp.localPosition;
  376. pos.y -= extents * 2f;
  377. int x = (int)Mathf.Ceil(-pos.y / cellHeight) - 1;//行号
  378. int y = (int)Mathf.Ceil(pos.x / cellWidth) - 1;//列号
  379. int realIndex;
  380. if (arrangeType == Arrangement.Horizontal) realIndex = x * viewCount + y;
  381. else realIndex = x + viewCount * y;
  382. if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
  383. {
  384. UpdateRectsize(pos);
  385. temp.localPosition = pos;
  386. //设置Item内容
  387. UpdateItem(temp, i, realIndex);
  388. }
  389. }
  390. if (cullContent)//设置裁剪部分是否隐藏
  391. {
  392. Vector2 pos = temp.localPosition;
  393. temp.gameObject.SetActive((pos.y > min && pos.y < max) ? true : false);
  394. }
  395. }
  396. }
  397. }
  398. void UpdateItem(Transform item, int index, int realIndex)
  399. {
  400. // LogHelper.Log(item + "----"+index +"----"+realIndex);
  401. if (onInitializeItem != null)
  402. {
  403. onInitializeItem(item.gameObject, index, realIndex);
  404. }
  405. }
  406. }