turn page

//NGUI............................................................

using System;
using UnityEngine;
using System.Collections;


public class ScrollViewTurnPage : MonoBehaviour
{
    [Header("一页的宽度,一般就是ScrollView的宽度")]
    [SerializeField] private int m_pageWidth = 600;
    [Range(5.0f, 55.0f)]
    [SerializeField] private int m_springStrength = 35;
    private int m_totalPages = 4; //固定的时候,写的页数
    [Header("列数")]
    [SerializeField] private int m_columns = 4;
    [Header("行数")]
    [SerializeField] private int m_row = 3;


#region Public Interface
    public void PreviousPage()
    {
        if (m_bHorizontal)
        {
            if (m_currPage > 1) Page(m_pageWidth);
        }
        else
        {
            if (m_currPage < m_totalPages) Page(m_pageWidth);
        }
    }


    public void NextPage()
    {
        if (m_bHorizontal)
        {
            if (m_currPage < m_totalPages) Page(-m_pageWidth);
        }
        else
        {
            if (m_currPage > 1) Page(-m_pageWidth);
        }
    }


    public int CurrentPage
    {
        get { return m_currPage; }
    }


    public int TotalPages
    {
        get { return m_totalPages; }
        set { m_totalPages = value; }
    }


    public int OnePageItemsMaxNum
    {
        get { return m_columns * m_row; }
    }


    public int ItemColumns
    {
        get { return m_columns; }
    }


    public int PageWidth
    {
        get { return m_pageWidth; }
    }


    #endregion


#region private interface
    private int m_currPage = 1;
    private UIScrollView m_scrollView = null;
    private float m_nowLocation = 0;
    private bool m_bDrag = false;
    private bool m_bSpringMove = false;
    private SpringPanel m_springPanel = null;
    private bool m_bHorizontal = true;


    void Awake()
    {
        m_scrollView = gameObject.GetComponent<UIScrollView>();
        if (m_scrollView == null)
            m_scrollView = gameObject.AddComponent<UIScrollView>();


        m_springPanel = GetComponent<SpringPanel>();
        if (m_springPanel == null) m_springPanel = gameObject.AddComponent<SpringPanel>();
        m_springPanel.enabled = true;


        m_scrollView.onDragStarted = OnDragStarted;
        m_scrollView.onMomentumMove = onMomentumMove;
        m_scrollView.onStoppedMoving = onStoppedMoving;
        m_scrollView.onDragFinished = onDragFinished;
        m_bHorizontal = m_scrollView.movement == UIScrollView.Movement.Horizontal ? true : false;
        onStoppedMoving();
    }
    
    void onDragFinished()
    {
        onMomentumMove();
    }


    void OnDragStarted()
    {
        m_bDrag = false;
        SetNowLocation();
    }


    void onMomentumMove()
    {
        if (m_bDrag) return;
        Vector3 v3 = transform.localPosition;
        float value = 0;
        if (m_bHorizontal)
        {
            value = m_nowLocation - v3.x;
            //if (Mathf.Abs(value) < m_springStrength) return;
            if (value > 0)
            {
                if (m_currPage < m_totalPages) Page(-m_pageWidth);
            }
            else
            {
                if (m_currPage > 1) Page(m_pageWidth);
            }
        }
        else
        {
            value = m_nowLocation - v3.y;
            //if (Mathf.Abs(value) < m_springStrength) return;
            if (value > 0)
            {
                if (m_currPage > 1) Page(-m_pageWidth);
            }
            else
            {
                if (m_currPage < m_totalPages) Page(m_pageWidth);
            }
        }
    }


    void Page(float value)
    {
        m_bSpringMove = true;
        m_bDrag = true;
        //m_springPanel = GetComponent<SpringPanel>();
        //if (m_springPanel == null) m_springPanel = gameObject.AddComponent<SpringPanel>();
        m_springPanel.enabled = false;
        Vector3 pos = m_springPanel.target;
        pos = m_bHorizontal ? new Vector3(pos.x + value, pos.y, pos.z) : new Vector3(pos.x, pos.y + value, pos.z);
        if (!SetIndexPage(pos)) return;
        SpringPanel.Begin(gameObject, pos, m_springStrength).strength = 10.0f;
        m_springPanel.onFinished = SpringPanleMoveEnd;
        //Debug.Log("current Page ==" + m_currPage);
    }


    void SpringPanleMoveEnd()
    {
        m_bSpringMove = false;
        //重新定位
    }


    void onStoppedMoving()
    {
        m_bDrag = false;
        SetNowLocation();
    }


    void SetNowLocation()
    {
        if (m_bHorizontal)
        {
            m_nowLocation = gameObject.transform.localPosition.x;
        }
        else
        {
            m_nowLocation = gameObject.transform.localPosition.y;
        }
    }


    bool SetIndexPage(Vector3 v3)
    {
        float value = m_bHorizontal ? v3.x : v3.y;
        //if (m_bHorizontal)
        //{
        //    if (value > 0 || value < (m_totalPages) * -m_pageWidth) return false;
        //}
        //else
        //{
        //    if (value < 0 || value > (m_totalPages - 1) * m_pageWidth) return false;
        //}
        value = Mathf.Abs(value);
        m_currPage = (int) (value / m_pageWidth) + 1;
        return true;
    }
#endregion
}

 

 

简单的实现了下NGUI的上下翻页。有两个接口,分别是上一页和下一页。

用法如下。

如上图,第一个参数为 Page的宽或高,具体由你的滑动组件设定的方向定。

第二个参数为滑动系数,即,滑动了多远就翻页

第三个参数为总页数。

依赖组件。UIScrollView,UICenterOnChild。

脚本和Scroll一起

分页父物体绑上居中脚本。为Scroll的子物体

这个基本功能没问题。但是有一个小缺陷。就是Panel的坐标必须为中心点。如果需要位移需要再加一个父物体。让滑动组件依赖的Panel的相对坐标都为0.

原因是因为我用的坐标去计算的当前页。大家如果有空。可以改为偏移量就能解决这问题。

因为我也用不上这个。这个是写给学员参考的。所以就懒得改了。大家需要的改的话。可以自行修改。

最后。上脚本。

 

  1. usingUnityEngine;
  2. usingSystem.Collections;
  3.  
  4. publicclassYouKeTurnPage:MonoBehaviour
  5. {
  6. ///
  7. /// 每页宽度(游-客-学-院)
  8. ///
  9. publicfloat pageWidth;
  10. ///
  11. /// 翻页力度(游.客.学.院)
  12. ///
  13. publicintEffortsFlip=50;
  14. ///
  15. /// 总页数
  16. ///
  17. publicint pageNums=0;
  18.  
  19. ///
  20. /// 当前所在页
  21. ///
  22. publicint pageIndex
  23. {
  24. get
  25. {
  26. return mPageIndex;
  27. }
  28. }
  29. ///
  30. /// 当前所在页
  31. ///
  32. privateint mPageIndex=1;
  33. privateUIScrollView mScrollView=null;
  34. privatefloat nowLocation=0;
  35. private bool isDrag=false;
  36. private bool isSpringMove=false;
  37. privateSpringPanel mSp=null;
  38. private bool isHorizontal=true;
  39.  
  40. voidAwake()
  41. {
  42. mScrollView= gameObject.GetComponent();
  43. if(mScrollView==null)
  44. {
  45. mScrollView= gameObject.AddComponent();
  46. }
  47.  
  48. mScrollView.onDragStarted=OnDragStarted;
  49. mScrollView.onMomentumMove= onMomentumMove;
  50. mScrollView.onStoppedMoving= onStoppedMoving;
  51. if(mScrollView.movement==UIScrollView.Movement.Horizontal)
  52. {
  53. isHorizontal=true;
  54. }
  55. else
  56. {
  57. isHorizontal=false;
  58. }
  59. onStoppedMoving();
  60. }
  61.  
  62. voidOnDragStarted()
  63. {
  64. isDrag=false;
  65. SetNowLocation();
  66. }
  67.  
  68. void onMomentumMove()
  69. {
  70. if(isDrag)return;
  71. Vector3 v3= transform.localPosition;
  72. float value=0;
  73. if(isHorizontal)
  74. {
  75. value= nowLocation- v3.x;
  76. if(Mathf.Abs(value)<EffortsFlip)return;
  77. if(value>0)
  78. {
  79. if(mPageIndex< pageNums)Page(-pageWidth);
  80. }
  81. else
  82. {
  83. if(mPageIndex>1)Page(pageWidth);
  84. }
  85. }
  86. else
  87. {
  88. value= nowLocation- v3.y;
  89. if(Mathf.Abs(value)<EffortsFlip)return;
  90. if(value>0)
  91. {
  92. if(mPageIndex>1)Page(-pageWidth);
  93. }
  94. else
  95. {
  96. if(mPageIndex< pageNums)Page(pageWidth);
  97. }
  98. }
  99. }
  100.  
  101. voidPage(float value)
  102. {
  103. isSpringMove=true;
  104. isDrag=true;
  105. mSp=GetComponent();
  106. if(mSp==null)mSp= gameObject.AddComponent();
  107. //mSp.enabled = false;
  108. Vector3 pos= mSp.target;
  109. pos= isHorizontal?newVector3(pos.x+ value, pos.y, pos.z):newVector3(pos.x, pos.y+ value, pos.z);
  110. if(!SetIndexPage(pos))return;
  111. SpringPanel.Begin(gameObject, pos,13f).strength=8f;
  112. mSp.onFinished=SpringPanleMoveEnd;
  113. Debug.Log("page index="+mPageIndex);
  114. }
  115.  
  116. voidSpringPanleMoveEnd()
  117. {
  118. isSpringMove=false;
  119. }
  120.  
  121. void onStoppedMoving()
  122. {
  123. isDrag=false;
  124. SetNowLocation();
  125. }
  126.  
  127. voidSetNowLocation()
  128. {
  129. if(isHorizontal)
  130. {
  131. nowLocation= gameObject.transform.localPosition.x;
  132. }
  133. else
  134. {
  135. nowLocation= gameObject.transform.localPosition.y;
  136. }
  137. }
  138.  
  139. boolSetIndexPage(Vector3 v3)
  140. {
  141. float value= isHorizontal? v3.x: v3.y;
  142. //Debug.Log((pageNums - 1) * pageWidth);
  143. if(isHorizontal)
  144. {
  145. if(value>0|| value<(pageNums-1)*-pageWidth)returnfalse;
  146. }
  147. else
  148. {
  149. if(value<0|| value>(pageNums-1)* pageWidth)returnfalse;
  150. }
  151.  
  152. value=Mathf.Abs(value);
  153. mPageIndex=(int)(value/ pageWidth)+1;
  154.  
  155. returntrue;
  156. }
  157.  
  158. #region 公共接口 游*客*学*院
  159.  
  160. ///
  161. /// 上一页
  162. ///
  163. publicvoidPreviousPage()
  164. {
  165. if(isHorizontal)
  166. {
  167. if(mPageIndex>1)Page(pageWidth);
  168. }
  169. else
  170. {
  171. if(mPageIndex< pageNums)Page(pageWidth);
  172. }
  173. }
  174.  
  175. ///
  176. /// 下一页
  177. ///
  178. publicvoidNextPage()
  179. {
  180. if(isHorizontal)
  181. {
  182. if(mPageIndex< pageNums)Page(-pageWidth);
  183. }
  184. else
  185. {
  186. if(mPageIndex>1)Page(-pageWidth);
  187. }
  188. }
  189. #endregion
  190. }

 

//UGUI..............................................\

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class GridItem {
    public string cnName;
    public string usName;

    public GridItem(string cnN,string usN) {
        cnName = cnN;
        usName = usN;
    }
}

public class PaginationPanel : MonoBehaviour
{
    /// <summary>
    /// 当前页面索引
    /// </summary>
    public int m_PageIndex = 1;

    /// <summary>
    /// 总页数
    /// </summary>
    public int m_PageCount = 0;

    /// <summary>
    /// 每页元素数
    /// </summary>
    public int m_PerPageCount = 0;

    /// <summary>
    /// 元素总个数
    /// </summary>
    public int m_ItemsCount = 0;

    /// <summary>
    /// 元素列表
    /// </summary>
    public List<GridItem> m_ItemsList;

    /// <summary>
    /// 上一页
    /// </summary>
    public Button m_BtnPrevious;

    /// <summary>
    /// 下一页
    /// </summary>
    public Button m_BtnNext;

    /// <summary>
    /// 显示当前页数的标签
    /// </summary>
    public Text m_PanelText;

    public Transform m_FrontPanel;
    public Transform m_BackPanel;

    public bool m_IsTouching;
    [Range(-1,1)]
    public float m_TouchDelta;

    public float m_MoveSpeed = 5f;
    public float m_FadeDistance;
    public Vector3 m_PrePos;
    public Vector3 m_CenterPos;
    public Vector3 m_NextPos;

    public bool m_DoPrevious;
    public bool m_DoNext;

    public float m_CurrAlpha;
    private bool m_HasPrevious;
    private bool m_HasNext;
    private bool m_IsSaveBeforeTouch;
    public int m_FrontPanelPageIndex;
    private bool m_CanChangePage;
    private bool m_IsBackPageIndex;
    void Start()
    {
        InitGUI();
        InitItems();
    }

    /// <summary>
    /// 初始化GUI
    /// </summary>
    private void InitGUI()
    {
        //m_BtnNext = GameObject.Find("Canvas/Panel/BtnNext").GetComponent<Button>();
        //m_BtnPrevious = GameObject.Find("Canvas/Panel/BtnPrevious").GetComponent<Button>();
        //m_PanelText = GameObject.Find("Canvas/Panel/Text").GetComponent<Text>();

        //为上一页和下一页添加事件
        m_BtnNext.onClick.AddListener(() => { OnNextBtnClick(); });
        m_BtnPrevious.onClick.AddListener(() => { OnPreviousBtnClick(); });
        m_PerPageCount = m_FrontPanel.childCount;
        m_FadeDistance = Mathf.Abs(m_FrontPanel.localPosition.y - m_BackPanel.localPosition.y);
        m_PrePos = m_FrontPanel.localPosition;
        m_PrePos.y += m_FadeDistance;
        m_NextPos = m_FrontPanel.localPosition;
        m_NextPos.y -= m_FadeDistance;
        m_CenterPos = m_FrontPanel.localPosition;
        m_CanChangePage = true;
        m_FrontPanelPageIndex = 1;
    }

    /// <summary>
    /// 初始化元素
    /// </summary>
    private void InitItems()
    {
        //准备一个存储着12生肖信息的数组
        GridItem[] items = new GridItem[]
        {
            new GridItem("鼠","Mouse"),
            new GridItem("牛","Ox"),
            new GridItem("虎","Tiger"),
            new GridItem("兔","Rabbit"),
            new GridItem("龙","Dragon"),
            new GridItem("蛇","Snake"),
            new GridItem("马","Horse"),
            new GridItem("羊","Goat"),
            new GridItem("猴","Monkey"),
            new GridItem("鸡","Rooster"),
            new GridItem("狗","Dog"),
            new GridItem("猪","Pig")
        };

        //利用12生肖数组来随机生成列表
        m_ItemsList = new List<GridItem>();
        for (int i = 0; i < items.Length; i++)
        {
            m_ItemsList.Add(items[i]);
        }

        //计算元素总个数
        m_ItemsCount = m_ItemsList.Count;
        //计算总页数
        m_PageCount = (m_ItemsCount % m_PerPageCount) == 0 ? m_ItemsCount / m_PerPageCount : (m_ItemsCount / m_PerPageCount) + 1;

        BindPage(m_FrontPanel, m_PageIndex);
        
        //更新界面页数
        m_PanelText.text = string.Format("{0}/{1}", m_PageIndex.ToString(), m_PageCount.ToString());
    }


    private void Update()
    {
        
        if (m_IsTouching)
        {
            if (!m_IsSaveBeforeTouch) {
                m_IsSaveBeforeTouch = true;
                m_FrontPanelPageIndex = m_PageIndex;
            }
            
            if (m_TouchDelta > 0)
            {
                if (!m_HasPrevious && m_FrontPanelPageIndex > 1)
                {
                    m_HasNext = false;
                    m_HasPrevious = true;
                    TouchPrevious();
                }
                if (m_DoPrevious)
                {
                    m_IsBackPageIndex = false;
                    m_BackPanel.localPosition = Vector3.Lerp(m_PrePos, m_CenterPos, Mathf.Abs(m_TouchDelta));
                    m_BackPanel.GetComponent<CanvasGroup>().alpha = Mathf.Abs(m_TouchDelta);
                }
                m_FrontPanel.localPosition = Vector3.Lerp(m_CenterPos, m_NextPos, Mathf.Abs(m_TouchDelta));
                m_FrontPanel.GetComponent<CanvasGroup>().alpha = 1 - Mathf.Abs(m_TouchDelta);
            }
            else if (m_TouchDelta < 0)
            {
                if (!m_HasNext && m_FrontPanelPageIndex < m_PageCount)
                {
                    m_HasNext = true;
                    m_HasPrevious = false;
                    TouchNext();
                }
                if (m_DoNext)
                {
                    m_IsBackPageIndex = false;
                    m_BackPanel.localPosition = Vector3.Lerp(m_NextPos, m_CenterPos, Mathf.Abs(m_TouchDelta));
                    m_BackPanel.GetComponent<CanvasGroup>().alpha = Mathf.Abs(m_TouchDelta);
                }
                m_FrontPanel.localPosition = Vector3.Lerp(m_CenterPos, m_PrePos, Mathf.Abs(m_TouchDelta));
                m_FrontPanel.GetComponent<CanvasGroup>().alpha = 1 - Mathf.Abs(m_TouchDelta);
            }
        }
        else {
            m_HasNext = false;
            m_HasPrevious = false;
            m_IsSaveBeforeTouch = false;
            float a = Mathf.Abs(m_FrontPanel.localPosition.y - m_CenterPos.y) / m_FadeDistance;
            m_CurrAlpha = a;
            if (m_TouchDelta >= 0.5f)
            {
                //float a = Mathf.Abs(m_FrontPanel.localPosition.y - m_CenterPos.y) / m_FadeDistance;
                if (m_DoPrevious)
                {
                    m_BackPanel.localPosition = Vector3.Lerp(m_BackPanel.localPosition, m_CenterPos, m_MoveSpeed * Time.deltaTime);
                    m_BackPanel.GetComponent<CanvasGroup>().alpha = a;

                    m_FrontPanel.localPosition = Vector3.Lerp(m_FrontPanel.localPosition, m_NextPos, m_MoveSpeed * Time.deltaTime);
                    m_FrontPanel.GetComponent<CanvasGroup>().alpha = 1 - a;
                    if (1 - a < 0.01f)
                    {
                        m_TouchDelta = 0;
                        SwitchPanel();
                    }
                }
                else {
                    m_TouchDelta = 0;
                }
                
            }
            else if (m_TouchDelta <= -0.5f)
            {
                //float a = Mathf.Abs(m_FrontPanel.localPosition.y - m_CenterPos.y) / m_FadeDistance;
                if (m_DoNext)
                {
                    m_BackPanel.localPosition = Vector3.Lerp(m_BackPanel.localPosition, m_CenterPos, m_MoveSpeed * Time.deltaTime);
                    m_BackPanel.GetComponent<CanvasGroup>().alpha = a;

                    m_FrontPanel.localPosition = Vector3.Lerp(m_FrontPanel.localPosition, m_PrePos, m_MoveSpeed * Time.deltaTime);
                    m_FrontPanel.GetComponent<CanvasGroup>().alpha = 1 - a;
                    if (1 - a < 0.01f)
                    {
                        m_TouchDelta = 0;
                        SwitchPanel();
                    }
                }
                else {
                    m_TouchDelta = 0;
                }
                
            }
            else {
                //float a = Mathf.Abs(m_FrontPanel.localPosition.y - m_CenterPos.y) / m_FadeDistance;
                if (m_DoPrevious)
                {
                    if (!m_IsBackPageIndex) {
                        m_IsBackPageIndex = true;
                        m_PageIndex = m_FrontPanelPageIndex;
                    }
                    if (m_PageIndex < m_PageCount && m_PageIndex > 1) {
                        m_BackPanel.localPosition = Vector3.Lerp(m_BackPanel.localPosition, m_PrePos, m_MoveSpeed * Time.deltaTime);
                        m_BackPanel.GetComponent<CanvasGroup>().alpha = a;
                    }
                    
                }
                else if (m_DoNext) {
                    if (!m_IsBackPageIndex)
                    {
                        m_IsBackPageIndex = true;
                        m_PageIndex = m_FrontPanelPageIndex; 
                    }
                    if (m_PageIndex < m_PageCount && m_PageIndex > 1) {
                        m_BackPanel.localPosition = Vector3.Lerp(m_BackPanel.localPosition, m_NextPos, m_MoveSpeed * Time.deltaTime);
                        m_BackPanel.GetComponent<CanvasGroup>().alpha = a;
                    }
                    
                }
                m_FrontPanel.localPosition = Vector3.Lerp(m_FrontPanel.localPosition,m_CenterPos, m_MoveSpeed * Time.deltaTime);
                m_FrontPanel.GetComponent<CanvasGroup>().alpha = 1 - a;

                if (m_TouchDelta != 0 && a < 0.01f)
                {
                    m_TouchDelta = 0;
                    m_DoNext = false;
                    m_DoPrevious = false;
                    m_CanChangePage = true;
                }
            }
        }
    }


    private void SwitchPanel() {
        m_DoPrevious = false;
        m_DoNext = false;

        Transform temp = m_FrontPanel;
        m_FrontPanel = m_BackPanel;
        m_BackPanel = temp;

        m_CanChangePage = true;
    }

    public void OnNextBtnClick() {
        if (!m_CanChangePage) return;
        m_TouchDelta = -0.6f;

        Next();
    }

    /// <summary>
    /// 下一页
    /// </summary>
    public void Next()
    {
        if (m_PageCount <= 0)
            return;
        //最后一页禁止向后翻页
        if (m_PageIndex >= m_PageCount)
            return;

        m_CanChangePage = false;

        m_PageIndex += 1;
        if (m_PageIndex >= m_PageCount)
            m_PageIndex = m_PageCount;
        
        m_BackPanel.localPosition = m_NextPos;
        BindPage(m_BackPanel, m_PageIndex);

        m_DoNext = true;
        m_DoPrevious = false;
        
        //更新界面页数
        m_PanelText.text = string.Format("{0}/{1}", m_PageIndex.ToString(), m_PageCount.ToString());
    }

    /// <summary>
    /// 下一页
    /// </summary>
    public void TouchNext()
    {
        if (m_PageCount <= 0)
            return;
        //最后一页禁止向后翻页
        if (m_FrontPanelPageIndex >= m_PageCount)
            return;

        m_CanChangePage = false;

        m_PageIndex = m_FrontPanelPageIndex + 1;
        if (m_PageIndex >= m_PageCount)
            m_PageIndex = m_PageCount;

        m_BackPanel.localPosition = m_NextPos;
        BindPage(m_BackPanel, m_PageIndex);

        m_DoNext = true;
        m_DoPrevious = false;

        //更新界面页数
        m_PanelText.text = string.Format("{0}/{1}", m_PageIndex.ToString(), m_PageCount.ToString());
    }

    public void OnPreviousBtnClick() {
        if (!m_CanChangePage) return;
        m_TouchDelta = 0.6f;
        Previous();
    }
    /// <summary>
    /// 上一页
    /// </summary>
    public void Previous()
    {
        if (m_PageCount <= 0)
            return;
        //第一页时禁止向前翻页
        if (m_PageIndex <= 1)
            return;
        m_CanChangePage = false;

        m_PageIndex -= 1;
        if (m_PageIndex < 1)
            m_PageIndex = 1;

        m_BackPanel.localPosition = m_PrePos;
        BindPage(m_BackPanel, m_PageIndex);
        m_DoPrevious = true;
        m_DoNext = false;
        
        //更新界面页数
        m_PanelText.text = string.Format("{0}/{1}", m_PageIndex.ToString(), m_PageCount.ToString());
    }

    /// <summary>
    /// 上一页
    /// </summary>
    public void TouchPrevious()
    {
        if (m_PageCount <= 0)
            return;
        //第一页时禁止向前翻页
        if (m_FrontPanelPageIndex <= 1)
            return;
        m_CanChangePage = false;

        m_PageIndex = m_FrontPanelPageIndex -1;
        if (m_PageIndex < 1)
            m_PageIndex = 1;

        m_BackPanel.localPosition = m_PrePos;
        BindPage(m_BackPanel, m_PageIndex);
        m_DoPrevious = true;
        m_DoNext = false;

        //更新界面页数
        m_PanelText.text = string.Format("{0}/{1}", m_PageIndex.ToString(), m_PageCount.ToString());
    }

    /// <summary>
    /// 绑定指定索引处的页面元素
    /// </summary>
    /// <param name="index">页面索引</param>
    private void BindPage(Transform tran,int index)
    {
        //列表处理
        if (m_ItemsList == null || m_ItemsCount <= 0)
            return;

        //索引处理
        if (index < 0 || index > m_ItemsCount)
            return;

        //按照元素个数可以分为1页和1页以上两种情况
        if (m_PageCount == 1)
        {
            int canDisplay = 0;
            for (int i = m_PerPageCount; i > 0; i--)
            {
                if (canDisplay < m_PerPageCount && canDisplay< m_ItemsList.Count)
                {
                    BindGridItem(tran.GetChild(canDisplay), m_ItemsList[m_PerPageCount - i]);
                    tran.GetChild(canDisplay).gameObject.SetActive(true);
                }
                else
                {
                    //对超过canDispaly的物体实施隐藏
                    tran.GetChild(canDisplay).gameObject.SetActive(false);
                }
                canDisplay += 1;
            }
        }
        else if (m_PageCount > 1)
        {
            //1页以上需要特别处理的是最后1页
            //和1页时的情况类似判断最后一页剩下的元素数目
            //第1页时显然剩下的为12所以不用处理
            if (index == m_PageCount)
            {
                int canDisplay = 0;
                for (int i = m_PerPageCount; i > 0; i--)
                {
                    //最后一页剩下的元素数目为 m_ItemsCount - 12 * (index-1)
                    if (canDisplay < m_ItemsCount - m_PerPageCount * (index - 1))
                    {
                        BindGridItem(tran.GetChild(canDisplay), m_ItemsList[m_PerPageCount * index - i]);
                        tran.GetChild(canDisplay).gameObject.SetActive(true);
                    }
                    else
                    {
                        //对超过canDispaly的物体实施隐藏
                        tran.GetChild(canDisplay).gameObject.SetActive(false);
                    }
                    canDisplay += 1;
                }
            }
            else
            {
                for (int i = m_PerPageCount; i > 0; i--)
                {
                    BindGridItem(tran.GetChild(m_PerPageCount - i), m_ItemsList[m_PerPageCount * index - i]);
                    tran.GetChild(m_PerPageCount - i).gameObject.SetActive(true);
                }
            }
        }
    }


    /// <summary>
    /// 加载一个Sprite
    /// </summary>
    /// <param name="assetName">资源名称</param>
    private Sprite LoadSprite(string assetName)
    {
        Texture texture = (Texture)Resources.Load(assetName);

        Sprite sprite = Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        return sprite;
    }

    /// <summary>
    /// 将一个GridItem实例绑定到指定的Transform上
    /// </summary>
    /// <param name="trans"></param>
    /// <param name="gridItem"></param>
    private void BindGridItem(Transform trans, GridItem gridItem)
    {
        //trans.GetComponent<Image>().sprite = LoadSprite(gridItem.ItemSprite);
        trans.GetComponent<Text>().text = gridItem.cnName;
        
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值