源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 rivejä
3.4 KiB

4 viikkoa sitten
  1. --UI对象池
  2. UIObjPool = UIObjPool or BaseClass()
  3. local UIObjPool = UIObjPool
  4. --类型ID,从1开始递增
  5. UIObjPool.UIType = {
  6. AwardItem = 1,
  7. }
  8. --缓存对象信息
  9. UIObjPool.UIInfo = {
  10. [UIObjPool.UIType.AwardItem] = {id = 1, max_num = 15, class_type = "AwardItem"},
  11. }
  12. function UIObjPool:__init()
  13. UIObjPool.Instance = self
  14. self.is_init = false
  15. self.ui_pool = {}
  16. for k,v in pairs(UIObjPool.UIType) do
  17. self.ui_pool[v] = Array.New()
  18. end
  19. local pool_cont = GameObject.Find("UIObjPoolContainer")
  20. if not IsNull(pool_cont) then
  21. self.parent_cont = pool_cont.transform
  22. self.parent_cont.gameObject:SetActive(false)
  23. else
  24. self.parent_cont = panelMgr:GetParent("UI")
  25. end
  26. self:InitUIPool()
  27. end
  28. function UIObjPool:getInstance()
  29. if UIObjPool.Instance == nil then
  30. UIObjPool.New()
  31. end
  32. return UIObjPool.Instance
  33. end
  34. function UIObjPool:__delete( )
  35. self:CancelStepTimer()
  36. for i=1, TableSize(UIObjPool.UIType) do
  37. local pool = self.ui_pool[i]
  38. for k,v in pairs(pool) do
  39. v:DeleteMe()
  40. end
  41. end
  42. self.ui_pool = {}
  43. end
  44. function UIObjPool:InitUIPool( )
  45. --避免重复执行
  46. if self.is_init then
  47. return
  48. end
  49. self.is_init = true
  50. --分步初始化对象池
  51. self.pool_index, self.info_index = 1, 1
  52. local pool_len = TableSize(UIObjPool.UIType)
  53. local item
  54. local function on_init( )
  55. if self.pool_index > pool_len then
  56. self:CancelStepTimer()
  57. return
  58. end
  59. local info = UIObjPool.UIInfo[self.pool_index]
  60. if not info then
  61. self:CancelStepTimer()
  62. return
  63. end
  64. if self.info_index > info.max_num then
  65. self.pool_index = self.pool_index + 1
  66. else
  67. item = _G[info.class_type].New(self.parent_cont)
  68. item:SetVisible(false)
  69. self.ui_pool[self.pool_index]:PushFront(item)
  70. self.info_index = self.info_index + 1
  71. end
  72. -- print("huangcong:UIObjPool [start:68] TableSize():", TableSize(self.ui_pool))
  73. end
  74. if not self.step_init_id then
  75. self.step_init_id = GlobalTimerQuest:AddPeriodQuest(on_init, 0.02, -1)
  76. end
  77. end
  78. function UIObjPool:CancelStepTimer( )
  79. if self.step_init_id then
  80. GlobalTimerQuest:CancelQuest(self.step_init_id)
  81. self.step_init_id = nil
  82. end
  83. end
  84. --外部接口,从对象池获取对象(类型id, 对象的父节点)
  85. function UIObjPool:PopItem( ui_type, ui_parent)
  86. local pool = self.ui_pool[ui_type]
  87. local info = UIObjPool.UIInfo[ui_type]
  88. local item
  89. if pool and pool:GetSize() > 0 then
  90. item = pool:PopFront()
  91. if ui_parent then
  92. item:SetVisible(true)
  93. item.parent = ui_parent
  94. item.transform:SetParent(ui_parent)
  95. item:SetPosition(0,0)
  96. end
  97. if item.ResetInfo then
  98. item:ResetInfo()
  99. end
  100. else
  101. if info and ui_parent then
  102. item = _G[info.class_type].New(ui_parent)
  103. end
  104. end
  105. return item
  106. end
  107. --外部接口,存入对象池(类型id, ui对象)
  108. function UIObjPool:PushItem( ui_type, ui_item )
  109. -------------------------
  110. if ui_type == UIObjPool.UIType.AwardItem then
  111. if not ui_item.SetItemSize then
  112. GlobalEventSystem:Fire(LuaErrorModel.SEND_LUAERROR_MESSAGE,"存了一个奇怪的东西进AwardItem对象池,请把日志移交前端")
  113. print('---- UIObjPool.lua -- 存了一个奇怪的东西进AwardItem对象池')
  114. PrintCallStack()
  115. end
  116. end
  117. -------------------------
  118. local info = UIObjPool.UIInfo[ui_type]
  119. local pool = self.ui_pool[ui_type]
  120. if (ui_item and ui_item.is_loaded) and pool:GetSize() < info.max_num then
  121. ui_item.transform:SetParent(self.parent_cont)
  122. ui_item:SetVisible(false)
  123. pool:PushFront(ui_item)
  124. else
  125. if ui_item then
  126. ui_item:DeleteMe()
  127. ui_item = nil
  128. end
  129. end
  130. end