123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
-
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using System.Collections.Generic;
- using DG.Tweening;
- /// <summary>
- /// 滑动列表的优化,用少量元素显示很多物体. 修改自UIWarpConten 和gLoop --ljp20170328
- /// </summary>
- public class ViewList : MonoBehaviour
- {
- /// <summary>
- /// 元素初始化
- /// </summary>
- /// <param name="item">游戏对象</param>
- /// <param name="wrapIndex">对象在prefab中的序号</param>
- /// <param name="realIndex">对象在数组中的序号</param>
- public delegate void OnInitializeItem(GameObject item, int wrapIndex, int realIndex);
- /// <summary>
- /// 元素初始化
- /// </summary>
- public OnInitializeItem onInitializeItem;
- /// <summary>
- /// 排列
- /// </summary>
- public enum Arrangement
- {
- /// <summary>
- /// 横
- /// </summary>
- Horizontal,
- /// <summary>
- /// 竖
- /// </summary>
- Vertical,
- }
- /// <summary>
- /// 排列方式
- /// </summary>
- public Arrangement arrangeType = Arrangement.Horizontal;
- /// <summary>
- /// The width of each of the cells.
- /// </summary>
- public float cellWidth = 200f;
- /// <summary>
- /// The height of each of the cells.
- /// </summary>
- public float cellHeight = 200f;
- /// <summary>
- /// 窗口能容纳的 行数 或 列数
- /// </summary>
- public int viewCount = 5;
- /// <summary>
- /// 对象最小序号
- /// </summary>
- public int minIndex = 0;
- /// <summary>
- /// 对象最大序号
- /// </summary>
- public int maxIndex = 0;
- /// <summary>
- ///当前对象
- /// </summary>
- Transform mTrans;
- /// <summary>
- /// 当前RectTransform对象
- /// </summary>
- RectTransform mRTrans;
- /// <summary>
- /// ScrollRect
- /// </summary>
- ScrollRect mScroll;
- /// <summary>
- /// 元素预制物体的容器对象
- /// </summary>
- public Transform content = null;
- /// <summary>
- /// 元素预制物体
- /// </summary>
- public GameObject goItemPrefab = null;
- /// <summary>
- /// 最少需要多少预制物体
- /// </summary>
- public int mMinNum = 0;
- /// <summary>
- /// 滚动方向
- /// </summary>
- bool mHorizontal;
- /// <summary>
- /// 元素链表
- /// </summary>
- List<Transform> mChild = new List<Transform>();
- /// <summary>
- /// 显示区域长度或高度的一半
- /// </summary>
- float extents = 0;
- Vector2 SR_size = Vector2.zero;//SrollRect的尺寸
- Vector3[] conners = new Vector3[4];//ScrollRect四角的世界坐标
- Vector2 startPos;//ScrollRect的初始位置
- /// <summary>
- /// 是否隐藏裁剪部分
- /// </summary>
- public bool cullContent = true;
- /// <summary>
- /// 将已有的对象初始化一下.
- /// </summary>
- public void SetData()
- {
- for (int i = 0; i < mChild.Count; i++)
- {
- Transform temp = mChild[i];
- UpdateItem(temp, i, i);
- }
- }
- /// <summary>
- /// 加载动画
- /// </summary>
- /// <param name="temp"></param>
- /// <param name="destPos"></param>
- /// <param name="i"></param>
- private void Dotween(Transform temp, Vector3 destPos, int i)
- {
- Tweener tweener = null;
- RectTransform panelTransform = temp.GetComponent<RectTransform>();
- temp.localPosition = new Vector3(destPos.x, -cellHeight * 5, 0);
- tweener = panelTransform.DOLocalMove(destPos, 0.3f);
- tweener.SetEase(Ease.Linear);//动画曲线
- tweener.SetDelay(i * 0.01f);
- }
- /// <summary>
- /// 实例化预设对象 、添加实例化对象到指定的子对象下
- /// </summary>
- /// <param name="goPrefab"> 元素预制物体 </param>
- /// <param name="parent"> 父节点 </param>
- /// <returns> 新创建的元素对象 </returns>
- private GameObject addChild(GameObject goPrefab, Transform parent)
- {
- if (goPrefab == null || parent == null)
- {
- LogHelper.LogError("异常。UIWarpContent addChild(goPrefab = null || parent = null)");
- return null;
- }
- GameObject goChild = GameObject.Instantiate(goPrefab) as GameObject;
- goChild.transform.SetParent(parent, false);
- goChild.SetActive(true);
- return goChild;
- }
- public void Init()
- {
- for (int i = 0; i < mMinNum; i++)
- {
- addChild(goItemPrefab, content);
- }
-
- }
- /// <summary>
- /// 开始函数,将动画放到这里,每当第一次运行会显示动画.
- /// </summary>
- void Start()
- {
- InitList();
- }
- /// <summary>
- /// 初始化mChild链表
- /// </summary>
- void InitList()
- {
- int i, ChildCount;
- InitValue();
- mChild.Clear();
- for (i = 0, ChildCount = mTrans.childCount; i < ChildCount; i++)
- mChild.Add(mTrans.GetChild(i));
- ////动画放到下面这个函数里
- ResetChildPosition();
- // LogHelper.Log(mTrans.localPosition.y + " " + startPos.y + " " + cellHeight);
- if (mTrans.localPosition.y > 2 * cellHeight && mTrans.localPosition.y<6 *cellHeight)
- {
- mTrans.localPosition = new Vector3(startPos.x, startPos.y - 3*cellHeight, 0);
- }
- }
- void InitValue()
- {
- if (viewCount <= 0)
- viewCount = 1;
- if (minIndex > maxIndex) minIndex = maxIndex;
- mTrans = transform;
- mRTrans = transform.GetComponent<RectTransform>();
- mScroll = transform.parent.GetComponent<ScrollRect>();
- mHorizontal = mScroll.horizontal;
- SR_size = transform.parent.GetComponent<RectTransform>().rect.size;
- //四角坐标 横着数
- conners[0] = new Vector3(-SR_size.x / 2f, SR_size.y / 2f, 0);
- conners[1] = new Vector3(SR_size.x / 2f, SR_size.y / 2f, 0);
- conners[2] = new Vector3(-SR_size.x / 2f, -SR_size.y / 2f, 0);
- conners[3] = new Vector3(SR_size.x / 2f, -SR_size.y / 2f, 0);
- for (int i = 0; i < 4; i++)
- {
- Vector3 temp = transform.parent.TransformPoint(conners[i]);
- conners[i].x = temp.x;
- conners[i].y = temp.y;
- }
- mRTrans.pivot = new Vector2(0, 1);//设置panel的中心在左上角
- mScroll.onValueChanged.AddListener(delegate { WrapContent(); });//添加滚动事件回调
- startPos = mTrans.localPosition;
-
- }
- //初始化各Item的坐标
- [ContextMenu("RePosition")]
- public virtual void RePosition()
- {
-
- InitList();
- }
- void Update()
- {
- if (Application.isPlaying) enabled = false;
- RePosition();
- }
- void ResetChildPosition()
- {
-
- int rows = 1, cols = 1;
- Vector2 startAxis = new Vector2(cellWidth / 10f, -cellHeight / 10f);//起始位置
- int i;
- int imax = mChild.Count;//Item元素数量
- //初始化行列数
- if (arrangeType == Arrangement.Vertical) //垂直排列 则适应行数
- {
- rows = viewCount;
- cols = (int)Mathf.Ceil((float)imax / (float)rows);
- extents = (float)(cols * cellWidth) * 0.5f;
- }
- else if (arrangeType == Arrangement.Horizontal) //水平排列则适应列数
- {
- cols = viewCount;
- rows = (int)Mathf.Ceil((float)imax / (float)cols);
- extents = (float)(rows * cellHeight) * 0.5f;
- }
- for (i = 0; i < imax; i++)
- {
- Transform temp = mChild[i];
- int x = 0, y = 0;//行列号
- if (arrangeType == Arrangement.Horizontal) { x = i / cols; y = i % cols; }
- else if (arrangeType == Arrangement.Vertical) { x = i % rows; y = i / rows; }
- Vector3 destPos = new Vector3(startAxis.x + y * cellWidth, startAxis.y - x * cellHeight, 0);
-
- ////动画显示,缓动到目的位置;
- //if (arrangeType == Arrangement.Vertical)
- //{
- // if (mRTrans.sizeDelta != new Vector2(destPos.x + cellWidth, viewCount * cellHeight))
- // {
- // Dotween(temp, destPos, i);
- // }
- // else
- // {
- // temp.localPosition = destPos;
- // }
- //}
- //else
- //{
- // if (mRTrans.sizeDelta != new Vector2(viewCount * cellWidth, -destPos.y + cellHeight))
- // {
- // Dotween(temp, destPos, i);
- // }
- // else
- // {
- // temp.localPosition = destPos;
- // }
- //}
- temp.localPosition = destPos;
-
- //
- if (minIndex == maxIndex || (i >= minIndex && i <= maxIndex))
- {
- cullContent = true;
- temp.gameObject.SetActive(true);
-
- UpdateRectsize(temp.localPosition);//更新panel的尺寸
- mTrans.localPosition = startPos;
- UpdateItem(temp, i, i);
- // Dotween(temp, destPos, i);
- }
- else cullContent = temp.gameObject.activeInHierarchy;//如果预制Item数超过maxIndex则将超过部分隐藏 并 设置cullCintent为ufalse 并且不再更新 panel尺寸
- }
- }
- /// <summary>
- /// 更新panel的尺寸
- /// </summary>
- /// <param name="pos"></param>
- void UpdateRectsize(Vector2 pos)
- {
- if (arrangeType == Arrangement.Vertical)
- {
- mRTrans.sizeDelta = new Vector2(pos.x + cellWidth, viewCount * cellHeight);
- }
- else
- {
- mRTrans.sizeDelta = new Vector2(viewCount * cellWidth, -pos.y + cellHeight);
- }
- }
-
- int getRealIndex(Vector2 pos)//计算realindex
- {
- int x = (int)Mathf.Ceil(-pos.y / cellHeight) - 1;//行号
- int y = (int)Mathf.Ceil(pos.x / cellWidth) - 1;//列号
- int realIndex;
- if (arrangeType == Arrangement.Horizontal) realIndex = x * viewCount + y;
- else realIndex = x + viewCount * y;
- return realIndex;
- }
- void WrapContent()
- {
- // LogHelper.Log("ffdds");
- Vector3[] conner_local = new Vector3[4];
- for (int i = 0; i < 4; i++)
- {
- conner_local[i] = mTrans.InverseTransformPoint(conners[i]);
- }
- //计算ScrollRect的中心坐标 相对于this的坐标
- Vector2 center = (conner_local[3] + conner_local[0]) / 2f;
- if (mHorizontal)
- {
- float min = conner_local[0].x - cellWidth;//显示区域
- float max = conner_local[3].x + cellWidth;
- for (int i = 0, imax = mChild.Count; i < imax; i++)
- {
- Transform temp = mChild[i];
- float distance = temp.localPosition.x - center.x;
- if (distance < -extents)
- {
- Vector2 pos = temp.localPosition;
- pos.x += extents * 2f;
- int realIndex = getRealIndex(pos);
- if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
- {
- UpdateRectsize(pos);
- temp.localPosition = pos;
- //设置Item内容
- UpdateItem(temp, i, realIndex);
- }
- }
- if (distance > extents)
- {
- Vector2 pos = temp.localPosition;
- pos.x -= extents * 2f;
- int realIndex = getRealIndex(pos);
- if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
- {
- temp.localPosition = pos;
- //设置Item内容
- UpdateItem(temp, i, realIndex);
- }
- }
- if (cullContent)//设置裁剪部分是否隐藏
- {
- Vector2 pos = temp.localPosition;
- temp.gameObject.SetActive((pos.x > min && pos.x < max) ? true : false);
- }
- }
- }
- else
- {
- float min = conner_local[3].y - cellHeight;//显示区域
- float max = conner_local[0].y + cellHeight;
- for (int i = 0, imax = mChild.Count; i < imax; i++)
- {
- Transform temp = mChild[i];
- float distance = temp.localPosition.y - center.y;
- if (distance < -extents)
- {
- Vector2 pos = temp.localPosition;
- pos.y += extents * 2f;
- int realIndex = getRealIndex(pos);
- if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
- {
- temp.localPosition = pos;
- //设置Item内容
- UpdateItem(temp, i, realIndex);
- }
- }
- if (distance > extents)
- {
- Vector2 pos = temp.localPosition;
- pos.y -= extents * 2f;
- int x = (int)Mathf.Ceil(-pos.y / cellHeight) - 1;//行号
- int y = (int)Mathf.Ceil(pos.x / cellWidth) - 1;//列号
- int realIndex;
- if (arrangeType == Arrangement.Horizontal) realIndex = x * viewCount + y;
- else realIndex = x + viewCount * y;
- if (minIndex == maxIndex || (realIndex >= minIndex && realIndex < maxIndex))
- {
- UpdateRectsize(pos);
- temp.localPosition = pos;
- //设置Item内容
- UpdateItem(temp, i, realIndex);
- }
- }
- if (cullContent)//设置裁剪部分是否隐藏
- {
- Vector2 pos = temp.localPosition;
- temp.gameObject.SetActive((pos.y > min && pos.y < max) ? true : false);
- }
- }
- }
- }
- void UpdateItem(Transform item, int index, int realIndex)
- {
- // LogHelper.Log(item + "----"+index +"----"+realIndex);
- if (onInitializeItem != null)
- {
- onInitializeItem(item.gameObject, index, realIndex);
- }
- }
-
- }
|