久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

        UGUI實(shí)現(xiàn)ScrollView無限滾動效果-創(chuàng)新互聯(lián)

        抽空做了一個UGUI的無限滾動的效果。只做了一半(向下無限滾動)。網(wǎng)上也看了很多教程,感覺還是按照自己的思路來寫可能比較好。搭建如下:

        創(chuàng)新互聯(lián)建站總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有做網(wǎng)站、成都做網(wǎng)站、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計、網(wǎng)站維護(hù)、公眾號搭建、微信小程序、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動行銷領(lǐng)域創(chuàng)造價值而不懈努力!

        UGUI實(shí)現(xiàn)ScrollView無限滾動效果

        content節(jié)點(diǎn)不添加任何組件。布局組件默認(rèn)是會重新排版子節(jié)點(diǎn)的,所以如果子節(jié)點(diǎn)的位置變化,會重新排版,不能達(dá)到效果。Size Fitter組件也不加,自己寫代碼調(diào)整Size大?。ú徽{(diào)整大小,無法滑動)。

        最主要的實(shí)現(xiàn)過程就是用Queue來搬運(yùn)Cell。在向下滾動的過程中(鼠標(biāo)上滑),頂部滑出View Port的Cell被搬運(yùn)到底部續(xù)上。這點(diǎn)類似于Queue的先見先出原則,再把Dequeue出來的元素添加到末尾,就很類似于ScrollView的無限滾動的原理了。在鼠標(biāo)上滑的過程中,content的PosY值是一直增加的,所以觸發(fā)滾動的條件就可以設(shè)定為位移之差大于Cell的高度值即可。

         數(shù)據(jù)的刷新,數(shù)據(jù)到頭之后,不能再次進(jìn)行滾動輪換了,這里用一組值來記錄初始化的一組Cell顯示的是數(shù)據(jù)的哪一段。例如HeadNum和TaiNum。比如用20個Cell顯示100條數(shù)據(jù)。初始化后,HeadNum就是0,TailNum就是19。上滑一行數(shù)據(jù)后,HeadNum=4,TailNum=23(這里假設(shè)是20個Cell排成4列)。

        下面是完整代碼:

        public class UIScrollViewTest : MonoBehaviour {
         
         public RectTransform content;
         public GameObject cell;
         // cell的初始化個數(shù)
         public int cellAmount = 0;
         // 鼠標(biāo)上滑時,存儲Cell的Queue。正序存儲
         public Queue F_cellQuee = new Queue();
         // 鼠標(biāo)下滑時,存儲Cell的Queue。到序存儲
         public Queue B_cellQuee = new Queue();
         // cell的Size
         public Vector2 cellSize = new Vector2(100,100);
         // cell的間隔
         public Vector2 cellOffset = new Vector2(0,0);
         // 列數(shù)
         public int columnCount = 0;
         private int rowCount;
         // 上一次content的位置
         public float lastPos;
         // 滾動的次數(shù)
         public int loopCount = 0;
         // cell顯示的數(shù)據(jù)段的開頭和結(jié)尾序號
         public int HeadNum = 0;
         public int TailNum;
         
         public Sprite[] sp;
         public List<Sprite> data;
         
         
         void Start()
         {
          for (int i = 0; i < sp.Length; i++)
          {
           data.Add(sp[i]);
          }
         
          InitialScrollView(data);
          TailNum = cellAmount-1;
          lastPos = content.localPosition.y;
          //Debug.LogError("行數(shù)是:::" + rowCount);
         
          //Debug.LogError("+++++++++++++++++ " + (5>>3));
         }
         
         
         void Update()
         {
          // 觸發(fā)滾動。
          if (content.localPosition.y - lastPos > cellSize.y && data.Count - cellAmount - loopCount*columnCount >0)
          {
           //Debug.LogError("11111111111 " + (data.Count - cellAmount - loopCount * columnCount));
           LoopScrolView(data);
           lastPos = content.localPosition.y;
          }
         }
         
         
         
         
         // 初始化cell
         void InitialScrollView(List<Sprite> data)
         {
          for (int i = 0; i < cellAmount; i++)
          {
           GameObject obj = Instantiate(cell.gameObject);
           obj.transform.SetParent(content);
           obj.name = "cell0" + i.ToString();
           obj.transform.GetChild(0).GetComponent<Text>().text = "cell0"+i.ToString();
           // 顯示默認(rèn)的數(shù)據(jù)
           obj.GetComponent<Image>().sprite = data[i];
          }
          // 初始化Queue
          for (int i = content.childCount-1; i >= 0; i--)
          {
           B_cellQuee.Enqueue(content.GetChild(i).gameObject);
          }
          for (int i = 0; i < content.childCount; i++)
          {
           F_cellQuee.Enqueue(content.GetChild(i).gameObject);
          }
         
          // 計算行數(shù)
          if (cellAmount % columnCount >0)
          {
           rowCount = cellAmount / columnCount + 1;
          } else {
           rowCount = cellAmount / columnCount;
          }
         
          // 排列cell的位置
          int index = 0;
          for (int r = 1; r <= rowCount; r++)
          {
           for (int c = 1; c <= columnCount; c++)
           {
            if (index < cellAmount)
            {
             Vector2 pos = new Vector2(cellSize.x / 2 + (cellSize.x + cellOffset.x) * (c-1), -cellSize.y / 2 - (cellOffset.y + cellSize.y) * (r-1));
             content.GetChild(index).GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 100);
             content.GetChild(index).GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 100);
             content.GetChild(index).GetComponent<RectTransform>().anchoredPosition = pos;
             index++;
            }
           }
          }
         
          Vector2 v = content.sizeDelta;
          // 初始化content的size
          content.sizeDelta = new Vector2(v.x, rowCount * cellSize.y + cellOffset.y*(rowCount-1));
         }
         
         
         /// 保持content的大小,這里是保持大小為在cell的行數(shù)基礎(chǔ)上,向下多出bottomCount行的距離
         void SetContentSize(int upperCount, int bottomCount)
         {
          if (content.sizeDelta != new Vector2(content.sizeDelta.x, content.sizeDelta.y + bottomCount * (cellSize.y + cellOffset.y)))
          {
           content.sizeDelta = new Vector2(content.sizeDelta.x, content.sizeDelta.y + bottomCount*(cellSize.y + cellOffset.y));
          }
         }
         
         // 計算頂部的Cell輪換到底部時的位置。以當(dāng)前最后一行的最后一個Cell的位置為基準(zhǔn)計算。
         void SetBottomCellPosition(int index, RectTransform rect, Vector2 pos)
         {
          Vector2 v = Vector2.zero;
          if (cellAmount % columnCount == 0) // 整除。每一行都滿的情況。
          {
           float x = pos.x - cellSize.x * (columnCount - index-1) - cellOffset.x * (columnCount-index-1);
           float y = pos.y - cellSize.y - cellOffset.y;
           v = new Vector2(x,y);
          }
          // 出現(xiàn)不滿行的情況。例如數(shù)據(jù)有103個,可以用23個cell來輪換。這樣就會出現(xiàn)不滿行的情況。
          // 這種情況下是頂部的一行cell順次接到底部不滿的行。例如23號cell后面接1號和2號cell,3號和4號cell填充到第“7”行
          else if (cellAmount % columnCount + index+1<=columnCount) 
          {
           float x = pos.x + cellSize.x * (index+1) + cellOffset.x * (index+1);
           float y = pos.y;
           v = new Vector2(x, y);
          }
          else
          {
           float x = pos.x - cellSize.x * (columnCount - index-1) - cellOffset.x * (columnCount - index-1);
           float y = pos.y - cellSize.y - cellOffset.y;
           v = new Vector2(x, y);
          }
          //Debug.LogError("++++++++++++++ " + pos+ "     "+ v);
          rect.anchoredPosition = v;
          rect.SetAsLastSibling();
         }
         
         // 計算底部的cell輪換到頂部是的位置,基準(zhǔn)位置是當(dāng)前行的第一個cell。
         void SetUpperCellPosition(int index, RectTransform rect, Vector2 pos)
         {
          Vector2 v = Vector2.zero;
          if (cellAmount % columnCount == 0) // 整除
          {
           float x = pos.x + cellSize.x * index + cellOffset.x * index;
           float y = pos.y + cellSize.y + cellOffset.y;
           v = new Vector2(x, y);
          }
          //else if (cellAmount % columnCount + index + 1 <= columnCount)
          //{
          // float x = pos.x + cellSize.x * (index + 1) + cellOffset.x * (index + 1);
          // float y = pos.y;
          // v = new Vector2(x, y);
          //}
          //else
          //{
          // float x = pos.x - cellSize.x * (columnCount - index - 1) - cellOffset.x * (columnCount - index - 1);
          // float y = pos.y - cellSize.y - cellOffset.y;
          // v = new Vector2(x, y);
          //}
          //Debug.LogError("++++++++++++++ " + pos+ "     "+ v);
          rect.anchoredPosition = v;
          rect.SetAsFirstSibling();
         }
         
         
         // 鼠標(biāo)上滑時,顯示當(dāng)前cell的數(shù)據(jù)。同時記錄數(shù)據(jù)段的序號遞增。
         void ShowRestCellData(Image cell, int index)
         {
          if (TailNum< data.Count-1)
          {
           Debug.LogError("當(dāng)前的序號是::::" + TailNum);
           TailNum++;
           HeadNum++;
           cell.sprite = data[TailNum];
          }
         }
         
         void ShowPreviousCellData(Image cell, int index)
         {
          if (HeadNum > 0)
          {
           Debug.LogError("當(dāng)前的序號是::::" + HeadNum);
           TailNum--;
           HeadNum--;
           cell.sprite = data[HeadNum];
          }
         }
         
         
         // 輪換的函數(shù)。每次亂換一行的cell。
         void LoopScrolView(List<Sprite> data)
         {
          SetContentSize(0, 1);
          loopCount++;
          RectTransform rect2 = content.GetChild(content.childCount - 1).GetComponent<RectTransform>();
          for (int i = 0; i < columnCount; i++)
          {
           GameObject obj = F_cellQuee.Dequeue() as GameObject;
           RectTransform rect = obj.GetComponent<RectTransform>();
           ShowRestCellData(obj.GetComponent<Image>(), i);
           SetBottomCellPosition(i, rect, rect2.anchoredPosition);
           F_cellQuee.Enqueue(obj);
          }
         }
         
        }
        

        另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

        分享標(biāo)題:UGUI實(shí)現(xiàn)ScrollView無限滾動效果-創(chuàng)新互聯(lián)
        URL鏈接:http://sd-ha.com/article10/djpsgo.html

        成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、營銷型網(wǎng)站建設(shè)、Google、關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)網(wǎng)站設(shè)計公司

        廣告

        聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

        成都seo排名網(wǎng)站優(yōu)化