标签:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class CUIChatScrollView : UIScrollView { public delegate void OnMoveCallBack(); /// <summary> /// 每次滑动结束的回调 /// </summary> public OnMoveCallBack mOnMoveCallback; public delegate void OnMoveNextPage(int pageindex); public OnMoveNextPage mOnMoveNextPage; private float mTopOffSetValue = 250; private float mBottomOffSetValue = 250; /// <summary> /// 当前数据总数 /// </summary> private int mTotalCount = 0; /// <summary> /// 子物体当前临时坐标 /// </summary> private float mTempPosition = 0; /// <summary> /// 子物体当前临时相对坐标(与UIPanel偏移、DynamicComponentTran、自己本身坐标之和,如果UIpanel裁剪区域有偏移还需要加上UIPanel FinalClipRegion的偏移值(X或者Y)) /// </summary> private float mBorderValue = 0; /// <summary> /// 当前显示出来的物体列表 /// </summary> private List<CBaseComponent> mCurrentComList = new List<CBaseComponent>(); /// <summary> /// 当前循环临时加载的列表(达到临界值需要移动的Components) /// </summary> private List<CBaseComponent> mTempComList = new List<CBaseComponent>(); /// <summary> /// 挂载UIGrid的组件物体,是components父物体,UIPanel子物体 /// </summary> private Transform DynamicComponentTran; /// <summary> /// 当前ScrollView依赖的UIPanel /// </summary> private UIPanel mUIPanel; /// <summary> /// UIPanel最终的绘制区域、坐标 /// </summary> private Vector4 mPanelFinalClipRegion; /// <summary> /// 是否需要请求下一页数据(用于多数据分页加载情况) /// </summary> private bool bNeedReqNextPage; /// <summary> /// 每页的数量(用于分页) /// </summary> private int mPrePageNumber; /// <summary> /// 是否是排行榜模式 /// </summary> private bool bIsRankModel; /// <summary> /// 当前自己的排名 /// </summary> private int mCurrentMyRank; /// <summary> /// 缓存开关 /// </summary> private Dictionary<int, bool> bHasRequestDic = new Dictionary<int, bool>(); /// <summary> /// 用于比较的X坐标 /// </summary> private float xPos; /// <summary> /// 用于比较的Y坐标 /// </summary> private float yPos; /// <summary> /// 是否需要3D显示效果 /// </summary> private bool bShow3DStyle = false; /// <summary> /// 居中的子物体 /// </summary> public Transform mCenterTranform; /// <summary> /// 子物体的depath /// </summary> private UIPanel mTempUIPanel; /// <summary> /// ScrollView 的UIPanel depath。 /// </summary> private int mUIPanelDepth = 1; private int m_Index = 0; private int m_maxShowNum = 0; public void Init<T>(List<T> components, int maxShowNum, int totalNumber, Transform dyTransorm, Movement moveMent = Movement.Horizontal, bool needrequest = false, int prepagenum = 0, bool isrank = false, int myrank = 0, bool bshow3d = false) where T : CBaseComponent { m_Index = maxShowNum-1; m_maxShowNum = maxShowNum; mCurrentComList.Clear(); for (int i = 0, length = components.Count; i < length; i++) { if (components[i] is CBaseComponent) { mCurrentComList.Add(components[i]); } } //mCurrentComList = components as List<CBaseComponent>; DynamicComponentTran = dyTransorm; mTotalCount = totalNumber; movement = moveMent; mPanelFinalClipRegion = (mUIPanel ?? (mUIPanel = gameObject.GetComponent<UIPanel>())).finalClipRegion; mUIPanelDepth = mUIPanel.depth + 10;//子物体固定高出父物体深度10个单位 //if (movement == Movement.Vertical) // mOffSetValue = mPanelFinalClipRegion.w / 2 + mItemIntervalValue / 2f; //else if (moveMent == Movement.Horizontal) // mOffSetValue = mPanelFinalClipRegion.z / 2 + mItemIntervalValue / 2f; bNeedReqNextPage = needrequest; mPrePageNumber = prepagenum; bIsRankModel = isrank; mCurrentMyRank = myrank; bHasRequestDic.Clear(); bShow3DStyle = bshow3d; momentumAmount = 35f; } public override void MoveRelative(Vector3 relative) { base.MoveRelative(relative); CheckScrollView(relative); } public override void AdjustSpring(Vector3 relative) { base.AdjustSpring(relative); CheckScrollView(relative); } private void CheckScrollView(Vector3 relative) { if (mCurrentComList == null || mCurrentComList.Count == 0) return; if (movement == Movement.Horizontal) { xPos = relative.x; mCurrentComList.Sort(delegate (CBaseComponent a, CBaseComponent b) { return a.LocalPosition.x.CompareTo(b.LocalPosition.x); }); } else if (movement == Movement.Vertical) { yPos = relative.y; mCurrentComList.Sort(delegate (CBaseComponent a, CBaseComponent b) { return b.LocalPosition.y.CompareTo(a.LocalPosition.y); }); } mTempComList.Clear(); if ((yPos > 0 && movement == Movement.Vertical) || (xPos < 0 && movement == Movement.Horizontal)) { Transform temptran = mCurrentComList[mCurrentComList.Count - 1].ComTransform; if (movement == Movement.Horizontal) mTempPosition = temptran.localPosition.x; else if (movement == Movement.Vertical) mTempPosition = temptran.localPosition.y; mTempPosition = Mathf.Abs(mTempPosition); if (true) { for (int i = 0, j = mCurrentComList.Count; i < j; i++) { temptran = mCurrentComList[i].ComTransform; if (movement == Movement.Horizontal) { mBorderValue = (temptran.localPosition.x*temptran.localScale.x + transform.localPosition.x + DynamicComponentTran.localPosition.x + Mathf.Abs(mPanelFinalClipRegion.x)); if (mBorderValue > -mTopOffSetValue) { break; } } else if (movement == Movement.Vertical) { mBorderValue = (temptran.localPosition.y * temptran.localScale.y + transform.localPosition.y + DynamicComponentTran.localPosition.y + Mathf.Abs(mPanelFinalClipRegion.y)); if (mBorderValue < mTopOffSetValue) { break; } } mTempComList.Add(mCurrentComList[i]); } } for (int i = 0, j = mTempComList.Count; i < j; i++) { if (m_Index >= mTotalCount - 1) break; m_Index++; if (movement == Movement.Vertical) { float spriteHeight = NGUIMath.CalculateRelativeWidgetBounds(transform, mCurrentComList[mCurrentComList.Count - 1].ComTransform, true).size.y; mTempComList[i].LocalPosition = new Vector3(0, -(spriteHeight +mTempPosition), 0); } //mTempComList[i].LocalPosition = new Vector3(0, -(mItemIntervalValue * (i + 1) + mTempPosition), 0); mTempComList[i].InitData(m_Index); } } else if ((yPos < 0 && movement == Movement.Vertical) || (xPos > 0 && movement == Movement.Horizontal)) { Transform temptran = mCurrentComList[0].ComTransform; if (movement == Movement.Horizontal) mTempPosition = temptran.localPosition.x; else if (movement == Movement.Vertical) mTempPosition = temptran.localPosition.y; mTempPosition = Mathf.Abs(mTempPosition); if (true) { for (int i = mCurrentComList.Count - 1; i >= 0; i--) { temptran = mCurrentComList[i].ComTransform; if (movement == Movement.Horizontal) { mBorderValue = (temptran.localPosition.x + transform.localPosition.x + DynamicComponentTran.localPosition.x + Mathf.Abs(mPanelFinalClipRegion.x)); if (mBorderValue <mBottomOffSetValue) { break; } } else if (movement == Movement.Vertical) { mBorderValue = (temptran.localPosition.y + transform.localPosition.y + DynamicComponentTran.localPosition.y + Mathf.Abs(mPanelFinalClipRegion.y)); if (mBorderValue > -mBottomOffSetValue) { break; } } mTempComList.Add(mCurrentComList[i]); } } for (int i = 0, j = mTempComList.Count; i < j; i++) { if (m_Index <= m_maxShowNum-1) break; m_Index--; //mTempComList[i].LocalPosition = new Vector3(0, -(mTempPosition - mItemIntervalValue * (i + 1)), 0); mTempComList[i].InitData(m_Index - m_maxShowNum+1); if (movement == Movement.Vertical) { float spriteHeight = NGUIMath.CalculateRelativeWidgetBounds(transform, mTempComList[i].ComTransform, true).size.y; Vector3 newPosition; if (i == 0) newPosition = new Vector3(0, -(mTempPosition - spriteHeight), 0); else newPosition = new Vector3(0, -(Mathf.Abs(mTempComList[i - 1].LocalPosition.y) - spriteHeight), 0); mTempComList[i].LocalPosition = newPosition; } } } if (mOnMoveCallback != null) { mOnMoveCallback(); } } }
标签:
原文地址:http://blog.csdn.net/chrisfxs/article/details/51312641