---@class UIScrollView:UIBaseComponent local UIScrollView = BaseClass("UIScrollView", UIBaseContainer) local base = UIBaseContainer local GridLayoutGroup = CS.UnityEngine.UI.GridLayoutGroup local TextAnchor = CS.UnityEngine.TextAnchor ScrollType= { Horizontal = "Horizontal" --swipe vertically , Vertical = "Vertical" --Horizontal sliding } -- Create functionUIScrollView:OnCreate() base.OnCreate(self) self.Scrollrect = nil self.content = nil self.layout = nil self.scrollType = nil self.fixedCount = nil self.itemPrefab = nil self.totalCount = 0 --Total number of data self.dataList = {} --data entity list self.objectList = {} self.headIndex = 0 --head index self.tailIndex = 0 --tail index self.firstItemAnchoredPos = Vector2.one self.__onrefresh = nil end -- Set refresh callback function UIScrollView:SetOnRefresh(...) self.__onrefresh = BindCallback(...) end -- Create -- sliding type -- Generate quantity -- Prefab -- View of the current instance --item script to be added function UIScrollView:Init(scrollType, fixedCount, itemPrefab, view, target) self.Scrollrect = self.transform:GetComponentInParent(typeof(CS.UnityEngine.UI.ScrollRect)) self.content = self.Scrollrect.content self.content.localPosition = Vector2(0, 0, 0) self.layout = self.content:GetComponent(typeof(CS.UnityEngine.UI.GridLayoutGroup)) self.layout.enabled = true self.scrollType = scrollType self.fixedCount = fixedCount self.itemPrefab = itemPrefab self.view = view --Set layout self:SetLayout() --Set the head and tail subscripts self.headIndex = 0 self.tailIndex = fixedCount - 1 --Instantiate Item self:InitItem(target) --Set Content size self:SetContentSize() --Get the anchor point position of the first Item self:GetFirstItemAnchoredPos() self.__onmove = function(vec2) self:OnScroll(vec2) end self.Scrollrect.onValueChanged:AddListener(self.__onmove) end -- <summary> -- Set layout -- </summary> function UIScrollView:SetLayout() self.layout.startCorner = GridLayoutGroup.Corner.UpperLeft self.layout.startAxis = GridLayoutGroup.Axis.Horizontal self.layout.childAlignment = TextAnchor.UpperLeft self.layout.constraintCount = 1 if (self.scrollType == ScrollType.Vertical) then self.Scrollrect.horizontal = false self.Scrollrect.vertical = true self.layout.constraint = GridLayoutGroup.Constraint.FixedColumnCount end end -- <summary> --Set Content size -- </summary> function UIScrollView:SetContentSize() self.content.sizeDelta = Vector2 ( self.layout.cellSize.y, self.layout.padding.top + self.layout.padding.bottom + self.totalCount * (self.layout.cellSize.y + self.layout.spacing.y) - self.layout.spacing.y ) end -- <summary> --Get the anchor point position of the first data -- </summary> function UIScrollView:GetFirstItemAnchoredPos() self.firstItemAnchoredPos = Vector2(0, -self.layout.padding.top - self.layout.cellSize.y / 2) end -- <summary> --Set display -- </summary> function UIScrollView:SetShow(item, index) item.gameObject.name = index if self.__onrefresh ~= nil then self.__onrefresh(item, index) end -- item:SetData(self.itemdata[index], self, type) end -- <summary> --Instantiate the specified quantity itme and assign it a value -- </summary> function UIScrollView:InitItem(component) local child_count = self.content.transform.childCount for i = 0, child_count - 1 do local passitem = self.dataList[i + 1] if passitem == nil then local obj = self.content.transform:GetChild(i).gameObject obj.name = i table.insert(self.objectList, obj) passitem = self.view:AddComponent(component, obj) table.insert(self.dataList, passitem) end self:SetPos(passitem, i) self:SetShow(passitem, i) end end -- <summary> --Instantiate the specified quantity itme and assign it a value -- </summary> functionUIScrollView:GetdataList() return self.dataList end -- <summary> --specify location -- </summary> function UIScrollView:SetNormalizedPosition(v2) if self.Scrollrect then self.Scrollrect.normalizedPosition = v2 end end -- <summary> --Animation moves the specified position -- </summary> -- Location -- time --Curve (default constant speed) function UIScrollView:SetDoNormalizedPosition(v2, time, ease) if not ease then ease = Ease.Linear end if self.Scrollrect then self.Scrollrect:DONormalizedPos(v2, time):SetEase(ease) end end -- <summary> --Reset item position -- </summary> function UIScrollView:SetPos(trans, index) if self.Scrollrect then if (self.scrollType == ScrollType.Vertical) then local y = nil if index == 0 then y = -self.layout.padding.top else y = -self.layout.padding.top - index * (self.layout.cellSize.y + -self.layout.spacing.y) end trans.localPosition = Vector2(0, y) -- Logger.Log(trans.anchoredPosition) end end end -- <summary> --Set the number of sliding objects -- </summary> function UIScrollView:SetTotalCount(count) self.totalCount = count end -- <summary> --Sliding -- </summary> functionUIScrollView:OnScroll(v) if self.Scrollrect == nil then return end if (self.scrollType == ScrollType.Vertical) then while self.content.anchoredPosition.y >= self.layout.padding.top + (self.headIndex + 1) * (self.layout.cellSize.y + self.layout.spacing.y) and self.tailIndex ~= self.totalCount - 1 do self.layout.enabled = false local item = self.dataList[1] local recitem = self.dataList[1].rectTransform table.remove(self.dataList, 1) table.insert(self.dataList, item) self.headIndex = self.headIndex + 1 self.tailIndex = self.tailIndex + 1 self:SetPos(rectitem, self.tailIndex) self:SetShow(item, self.tailIndex) print(self.headIndex .. "---------:---------" .. self.tailIndex) end while self.content.anchoredPosition.y <= self.layout.padding.top + self.headIndex * (self.layout.cellSize.y + self.layout.spacing.y) and self.headIndex ~= 0 do self.layout.enabled = false local item = self.dataList[#self.dataList] local rectitem = self.dataList[#self.dataList].rectTransform table.remove(self.dataList, #self.dataList) table.insert(self.dataList, 1, item) self.headIndex = self.headIndex - 1 self.tailIndex = self.tailIndex - 1 self:SetPos(rectitem, self.headIndex) self:SetShow(item, self.headIndex) print(self.headIndex .. "---------:---------" .. self.tailIndex) end elseif (self.scrollType == ScrollType.Horizontal) then while self.content.anchoredPosition.x <= -self.layout.padding.left - (self.headIndex + 1) * (self.layout.cellSize.x + self.layout.spacing.x) and self.tailIndex ~= self.totalCount - 1 do self.layout.enabled = false local item = self.dataList[1] local recitem = self.dataList[1].rectTransform table.remove(self.dataList, 1) table.insert(self.dataList, item) self.headIndex = self.headIndex + 1 self.tailIndex = self.tailIndex + 1 self:SetPos(rectitem, self.tailIndex) self:SetShow(item, self.tailIndex) print(self.headIndex .. "---------:---------" .. self.tailIndex) end while self.content.anchoredPosition.y >= self.layout.padding.left - self.headIndex * (self.layout.cellSize.x + self.layout.spacing.x) and self.headIndex ~= 0 do self.layout.enabled = false local item = self.dataList[#self.dataList] local rectitem = self.dataList[#self.dataList].rectTransform table.remove(self.dataList, #self.dataList) table.insert(self.dataList, 1, item) self.headIndex = self.headIndex - 1 self.tailIndex = self.tailIndex - 1 self:SetPos(rectitem, self.headIndex) self:SetShow(item, self.headIndex) print(self.headIndex .. "---------:---------" .. self.tailIndex) end end end functionUIScrollView:Refresh() if self.Scrollrect then if self.__onmove ~= nil then self.Scrollrect.onValueChanged:RemoveListener(self.__onmove) end self.Scrollrect.normalizedPosition = Vector2(0, 0) self.Scrollrect = nil self.content.sizeDelta = Vector2(0, 0) self.content.localPosition = Vector2(0, 0, 0) self.content = nil self.layout.enabled = true self.layout = nil self.scrollType = nil self.fixedCount = nil self.itemPrefab = nil self.totalCount = nil --Total number of data self.headIndex = nil --head index self.tailIndex = nil --tail index local index = 0 local index2 = 0 for i = 1, #self.dataList, 1 do for j = 1, #self.dataList - i, 1 do index = tonumber(self.dataList[j].__name) index2 = tonumber(self.dataList[j + 1].__name) if index > index2 then local temp = self.dataList[j] self.dataList[j] = self.dataList[j + 1] self.dataList[j + 1] = temp end end end end end functionUIScrollView:OnDestroy() if self.Scrollrect then if self.__onmove ~= nil then self.Scrollrect.onValueChanged:RemoveListener(self.__onmove) end self.Scrollrect.normalizedPosition = Vector2(0, 0) self.Scrollrect = nil self.content.sizeDelta = Vector2(0, 0) self.content.localPosition = Vector2(0, 0, 0) self.content = nil self.layout.enabled = true self.layout = nil self.scrollType = nil self.fixedCount = nil self.itemPrefab = nil self.totalCount = nil --Total number of data self.dataList = {} --data entity list self.headIndex = nil --head index self.tailIndex = nil --tail index end base.OnDestroy(self) end returnUIScrollView