1. 程式人生 > 其它 >unity滑動列表簡單優化(資源複用)

unity滑動列表簡單優化(資源複用)

參考:https://download.csdn.net/download/ai418976280/9619764?spm=1001.2014.3001.5503 實現功能:基於UGUI自帶的ScrollRect,對列表元素進行復用 基本思路:在原生ScrollRect的基礎上,新增onValueChanged監聽,通過Content節點的anchoredPosition來判斷當前可見的第一個元素的索引值,回收超出檢視的部分,加載出檢視上的所有元素(對回收的元素進行復用) 與上一版的區別:刪除了網格佈局(Grid Layout Group)和內容大小介面卡(Content Size Fitter)的使用 PS:一定要先計算好Content的sizeDelta,這邊元素的預製體錨點設定在(0.5, 0.5)。 核心部分程式碼:
 function
M:initialize() --- 當前的index self._index = -1 --- 可視的item連結串列 self._itemList = {} --- 未使用的池子 self._unUseQueue = {} --- 儲存已經載入過的sprite self._picSprites = {} end --- 初始化數值引數 function M:initParams() self._rowNum = 2 --- 列表數量 self._count
= 20 self._colNum = Mathf.CeilToInt(self._count/self._rowNum) --- item的大小 self._cellSize = Vector2(225, 225)--self._gridGroup.cellSize --- 間隔 self._spacing = Vector2(6.5, 10)--self._gridGroup.spacing --- 可視列數量 self._viewCount = Mathf.CeilToInt(CAMERA_WIDTH * 200
/(self._cellSize.x + self._spacing.x)) + 2 --- 設定content的大小 self._content.sizeDelta = Vector2(self._cellSize.x * self._colNum + (self._spacing.x * self._colNum - 1), self._cellSize.y * self._rowNum + self._spacing.y * (self._rowNum - 1)) end --- 值變化 function M:onValueChanged(pos) local index = self:getPosIndex() if self._index ~= index and index > -1 then self._index = index for i = #self._itemList, 1, -1 do local item = self._itemList[i] if item._col < index or item._col > index + self._viewCount then --- 收入回收池 self:addUnUseQueue(item, i) end end for i = self._index, self._index + self._viewCount do if i > 0 and i <= self._colNum then local isOk = false for k,v in pairs(self._itemList) do if v._col == i then isOk = true -- break end end if not isOk then self:createItem(1, i) self:createItem(2, i) end end end end end --- 收入回收池 function M:addUnUseQueue(item, i) table.remove(self._itemList, i) item:unUse() table.insert(self._unUseQueue, item) end --- 建立元素 function M:createItem(row, col) if (col - 1) * 2 + row > self._count then return end local item = nil if #self._unUseQueue > 0 then item = table.remove(self._unUseQueue, 1) else item = self:createNewItem() end item:recycle() item:updateData(row, col) item._rectTF.anchoredPosition = self:getPosition(row, col) table.insert(self._itemList, item) end --- 建立一個新的可用的元素 function M:createNewItem() local go = UnityEngine.GameObject.Instantiate(self._iconPre, self._content) local item = go.transform:GetLuaTable() return item end --- 獲取當前位置的index function M:getPosIndex() -- print(self._content.anchoredPosition.x) return Mathf.FloorToInt(self._content.anchoredPosition.x / -(self._cellSize.x + self._spacing.x)) end --- 獲取當前位置 function M:getPosition(row, col) local y = row == 1 and (self._cellSize.y + self._spacing.y) or -(self._cellSize.y + self._spacing.y) return Vector3((col - 0.5) * (self._cellSize.x + self._spacing.x), y/2, 0) end