源战役客户端
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.

1022 lines
33 KiB

  1. --lua端的资源管理器
  2. LuaResManager = LuaResManager or BaseClass()
  3. local LuaResManager = LuaResManager
  4. local soundMgr = soundMgr
  5. local resMgr = resMgr
  6. local Array_New = Array.New
  7. local Time = Time
  8. LuaResManager.RES_TYPE = {
  9. PREFAB = 1,
  10. SPRITE = 2,
  11. RENDERTEXTURE = 3,
  12. TEXTASSET = 4,
  13. }
  14. function LuaResManager:__init()
  15. LuaResManager.Instance = self
  16. --assetbundle包资源引用列表
  17. self.ref_count_list = {}
  18. --延迟unload的资源列表
  19. self.delay_unload_list = {}
  20. --界面显示模型对象列表
  21. self.role_mode_list = {}
  22. --3dUIRt对象列表
  23. self.ui_rt_mode_list = {}
  24. --每隔多长时间执行一次内存清理
  25. self.clear_memory_time = 10
  26. if SystemRuntimePlatform.IsIphone() then
  27. self.clear_memory_time = 5
  28. elseif SystemRuntimePlatform.IsAndroid() then
  29. if SystemMemoryLevel.Cur == SystemMemoryLevel.Low then
  30. self.clear_memory_time = 15
  31. end
  32. end
  33. --定时清理资源函数
  34. local function onCheckToClearObjPool()
  35. self:CheckToClearObjPool()
  36. end
  37. GlobalTimerQuest:AddPeriodQuest(onCheckToClearObjPool, self.clear_memory_time, -1)
  38. --资源对象池
  39. self.obj_pool_list = {}
  40. --对象池总ab包对象数量
  41. self.obj_pool_ab_count = 0
  42. --静态的缓存对象名字列表
  43. self.static_obj_name_list = {}
  44. --静态的缓存对象列表
  45. self.static_obj_pool_list = {}
  46. self.static_obj_pool_container = GameObject.New("static_obj_pool_container").transform
  47. self.static_obj_pool_container:SetParent(panelMgr:GetParent("SceneObjContainer"))
  48. self.static_obj_pool_container.gameObject:SetActive(false)
  49. self.canvas_root = GameObject.Find("root/Canvas").transform
  50. --外部资源使用记录,处理请求未返回时触发的二次请求
  51. self.outsize_list = {}
  52. local callfunc = function(module_name,func)
  53. if module_name == "res" and func == "objlist" then
  54. local data = {}
  55. for k,v in pairs(self.static_obj_pool_list) do
  56. print("---static----",k,v)
  57. data["static_" .. k] = true
  58. end
  59. for k,v in pairs(self.obj_pool_list) do
  60. for resname,obj in pairs(v["res_name_list"]) do
  61. print("---obj----",resname,obj)
  62. if not data["obj_" .. resname] then
  63. data["obj_" .. resname] = 0
  64. end
  65. data["obj_" .. resname] = data["obj_" .. resname] + 1
  66. end
  67. end
  68. GlobalEventSystem:Fire(EventName.SENT_MODULE_DEBUG_DATA,"res", data)
  69. end
  70. end
  71. GlobalEventSystem:Bind(EventName.DO_MODULE_DEBUG_FUNC,callfunc)
  72. local func = function()
  73. callfunc("res","objlist")
  74. end
  75. GlobalEventSystem:Bind(EventName.CHECK_MODULE_DEBUG_DATA,func)
  76. end
  77. function LuaResManager:getInstance()
  78. if LuaResManager.Instance == nil then
  79. LuaResManager.Instance = LuaResManager.New()
  80. end
  81. return LuaResManager.Instance
  82. end
  83. ----------------------------资源引用计数-------------------------------------------
  84. --增加引用数
  85. function LuaResManager:addRefCount(ref_tar,abName,count)
  86. count = count or 1
  87. if count > 0 and abName then
  88. local ref_info = self.ref_count_list[ref_tar] or {}
  89. local ref_count = ref_info[abName] and ref_info[abName] + count or count
  90. ref_info[abName] = ref_count
  91. self.ref_count_list[ref_tar] = ref_info
  92. --发现资源在待卸载列表,更新其使用时间
  93. local delay_info = self.delay_unload_list[abName]
  94. if delay_info then
  95. delay_info.use_time = Time.time
  96. end
  97. end
  98. end
  99. --减少引用数
  100. function LuaResManager:reduceRefCount(ref_tar,abName)
  101. if self.ref_count_list[ref_tar] and self.ref_count_list[ref_tar][abName] and self.ref_count_list[ref_tar][abName] > 0 then
  102. self.ref_count_list[ref_tar][abName] = self.ref_count_list[ref_tar][abName] - 1
  103. end
  104. end
  105. --清除关联对象的所有资源关联信息
  106. function LuaResManager:clearReference(ref_tar)
  107. self:clearRefCount(ref_tar)
  108. self:clearRoleMode(ref_tar)
  109. end
  110. --清除该对象的所有资源引用
  111. function LuaResManager:clearRefCount(ref_tar)
  112. local ref_count_obj = self.ref_count_list[ref_tar]
  113. if ref_count_obj then
  114. for abName,ref_count in pairs(ref_count_obj) do
  115. if ref_count and ref_count > 0 then
  116. local delay_info = self.delay_unload_list[abName]
  117. if delay_info == nil then
  118. delay_info = {
  119. use_time = Time.time,
  120. ref_count = ref_count,
  121. }
  122. self.delay_unload_list[abName] = delay_info
  123. else
  124. delay_info.use_time = Time.time
  125. delay_info.ref_count = delay_info.ref_count + ref_count
  126. end
  127. end
  128. end
  129. self.ref_count_list[ref_tar] = nil
  130. end
  131. end
  132. --清除该对象的所有角色模型的引用
  133. function LuaResManager:clearRoleMode(ref_tar)
  134. local ref_count_obj = self.role_mode_list[ref_tar]
  135. if ref_count_obj then
  136. for p,uiModelClass in pairs(ref_count_obj) do
  137. uiModelClass:DeleteMe()
  138. end
  139. self.role_mode_list[ref_tar] = nil
  140. end
  141. end
  142. --清除该对象的所有3d的引用
  143. function LuaResManager:clearRtUIMode(ref_tar)
  144. local ref_count_obj = self.ui_rt_mode_list[ref_tar]
  145. if ref_count_obj then
  146. for p,uiRtModelClass in pairs(ref_count_obj) do
  147. uiRtModelClass:DeleteMe()
  148. end
  149. self.ui_rt_mode_list[ref_tar] = nil
  150. end
  151. end
  152. -------------------------------------------对象缓存池管理-----------------------------------------------
  153. --添加游戏对象列表进入缓存池 通过loadPrefabs加载的资源对象列表
  154. function LuaResManager:AddObjListToPool(ref_tar, abName, resName_list, gameObject_list)
  155. if gameObject_list then
  156. self:CreateABObjPool(abName)
  157. local resName = "resName_list" .. table.concat(resName_list, "_")
  158. local res_list = self.obj_pool_list[abName]["res_name_list"][resName]
  159. if res_list == nil then
  160. res_list = Array_New()
  161. self.obj_pool_list[abName]["res_name_list"][resName] = res_list
  162. end
  163. if res_list:GetSize() > self:GetMaxChildPoolObjCount() then
  164. for i, go in ipairs(gameObject_list) do
  165. destroy(go)
  166. end
  167. else
  168. self:reduceRefCount(ref_tar, abName)
  169. for i, go in ipairs(gameObject_list) do
  170. go.transform:SetParent(self.obj_pool_list[abName].container)
  171. end
  172. res_list:PushBack(gameObject_list)
  173. self.obj_pool_list[abName].last_change_time = Time.time
  174. end
  175. end
  176. end
  177. --添加游戏对象进入缓存池 通过loadPrefab加载的资源对象
  178. function LuaResManager:AddObjToPool(ref_tar, abName, resName, gameObject, alpha, is_default_color)
  179. if gameObject then
  180. --非静态缓存对象
  181. if abName and (not self.static_obj_name_list[abName] or self.static_obj_pool_list[abName]) then
  182. self:CreateABObjPool(abName)
  183. local res_list = self.obj_pool_list[abName]["res_name_list"][resName]
  184. if res_list == nil then
  185. res_list = Array_New()
  186. self.obj_pool_list[abName]["res_name_list"][resName] = res_list
  187. end
  188. if res_list:GetSize() > self:GetMaxChildPoolObjCount() then
  189. --大于限制的相同对象缓存数量,不放入缓存
  190. destroy(gameObject)
  191. else
  192. if not IsNull(gameObject) then
  193. --放入缓存后减少引用计数
  194. self:reduceRefCount(ref_tar, abName)
  195. gameObject.transform:SetParent(self.obj_pool_list[abName].container)
  196. res_list:PushBack(gameObject)
  197. self.obj_pool_list[abName].last_change_time = Time.time
  198. end
  199. end
  200. else
  201. --静态缓存对象
  202. if not abName or self.static_obj_pool_list[abName] then
  203. --只缓存一个相同的静态对象
  204. destroy(gameObject)
  205. else
  206. --重置shader
  207. -- if (alpha and alpha < 1) or is_default_color == false then
  208. -- local skinned_mesh_renderer = gameObject:GetComponentInChildren(typeof(UnityEngine.SkinnedMeshRenderer))
  209. -- if skinned_mesh_renderer then
  210. -- local sd = SceneManager:getInstance():GetTextureShader()
  211. -- if sd then
  212. -- skinned_mesh_renderer.material.shader = sd
  213. -- end
  214. -- end
  215. -- end
  216. self:reduceRefCount(ref_tar, abName)
  217. self.static_obj_pool_list[abName] = gameObject
  218. gameObject.transform:SetParent(self.static_obj_pool_container)
  219. --设置引用计数10000,避免静态缓存对象异常情况引用归零被销毁
  220. self:addRefCount(self.static_obj_pool_list, abName, 10000)
  221. end
  222. end
  223. end
  224. end
  225. --添加缓存对象时初始化缓存信息
  226. function LuaResManager:CreateABObjPool(abName)
  227. if not self.obj_pool_container then
  228. self.obj_pool_container = UiFactory.createChild(panelMgr:GetParent("SceneObjContainer"), UIType.EmptyObject, "obj_pool_container").transform
  229. self.obj_pool_container.gameObject:SetActive(false)
  230. end
  231. if self.obj_pool_list[abName] == nil then
  232. --缓存AB包超过最大数量时,强制清理最久未使用的那个资源
  233. if self.obj_pool_ab_count >= self:GetMaxPoolObjCount() then
  234. local min_change_time = 9999999
  235. local min_abName = nil
  236. for pool_abName, ab_info in pairs(self.obj_pool_list) do
  237. if ab_info.last_change_time < min_change_time and ab_info then
  238. min_change_time = ab_info.last_change_time
  239. min_abName = pool_abName
  240. end
  241. end
  242. if min_abName then
  243. self:ClearObjPool(min_abName)
  244. end
  245. end
  246. self.obj_pool_ab_count = self.obj_pool_ab_count + 1
  247. self.obj_pool_list[abName] = {last_change_time = Time.time, res_name_list = {}, container = UiFactory.createChild(self.obj_pool_container, UIType.EmptyObject, abName .. "_container").transform}
  248. end
  249. end
  250. --从缓存池中获取游戏对象列表
  251. function LuaResManager:GetObjListFormPool(ref_tar, abName, resName_list)
  252. local resName = "resName_list" .. table.concat(resName_list, "_")
  253. if self.obj_pool_list[abName] and self.obj_pool_list[abName]["res_name_list"][resName] then
  254. local gameObject_list = self.obj_pool_list[abName]["res_name_list"][resName]:PopBack()
  255. if gameObject_list then
  256. self.obj_pool_list[abName].last_change_time = Time.time
  257. self:addRefCount(ref_tar, abName)
  258. return gameObject_list
  259. end
  260. end
  261. end
  262. --从缓存池中获取游戏对象
  263. function LuaResManager:GetObjFormPool(ref_tar, abName, resName)
  264. if self.static_obj_name_list[abName] then
  265. local go = self.static_obj_pool_list[abName]
  266. if go then
  267. self.static_obj_pool_list[abName] = nil
  268. self:addRefCount(ref_tar, abName)
  269. return go
  270. end
  271. end
  272. if self.obj_pool_list[abName] and self.obj_pool_list[abName]["res_name_list"][resName] then
  273. local gameObject = self.obj_pool_list[abName]["res_name_list"][resName]:PopBack()
  274. if gameObject then
  275. self.obj_pool_list[abName].last_change_time = Time.time
  276. self:addRefCount(ref_tar, abName)
  277. return gameObject
  278. end
  279. end
  280. end
  281. --清除单个ab包的缓存游戏对象
  282. function LuaResManager:ClearObjPool(abName, ab_info)
  283. ab_info = ab_info or self.obj_pool_list[abName]
  284. if ab_info then
  285. local all_ref_count = 0
  286. for resName, res_list in pairs(ab_info.res_name_list) do
  287. if res_list and res_list:GetSize() > 0 then
  288. all_ref_count = all_ref_count + res_list:GetSize()
  289. end
  290. end
  291. self:addRefCount(self.obj_pool_container, abName, all_ref_count)
  292. self:clearReference(self.obj_pool_container)
  293. destroy(ab_info.container.gameObject)
  294. self.obj_pool_list[abName] = nil
  295. self.obj_pool_ab_count = self.obj_pool_ab_count - 1
  296. end
  297. end
  298. --清除所有缓存池游戏对象,切换场景时调用
  299. function LuaResManager:ClearAllObjPool()
  300. local obj = nil
  301. if self.obj_pool_container then
  302. local all_ref_count = 0
  303. for abName, ab_info in pairs(self.obj_pool_list) do
  304. all_ref_count = 0
  305. for resName, res_list in pairs(ab_info.res_name_list) do
  306. if res_list and res_list:GetSize() > 0 then
  307. all_ref_count = all_ref_count + res_list:GetSize()
  308. end
  309. end
  310. self:addRefCount(self.obj_pool_container, abName, all_ref_count)
  311. end
  312. self:clearReference(self.obj_pool_container)
  313. destroy(self.obj_pool_container.gameObject, true)
  314. self.obj_pool_container = nil
  315. end
  316. self.obj_pool_list = {}
  317. self.obj_pool_ab_count = 0
  318. self:CheckToClearObjPool(true)
  319. end
  320. --有10秒定时器检测,切换场景时自动调用一次
  321. function LuaResManager:CheckToClearObjPool(force_unload)
  322. local unload_count = 0
  323. for abName,vo in pairs(self.delay_unload_list) do
  324. if force_unload or Time.time - vo.use_time >= self.clear_memory_time then --延迟一定时间unload ab包
  325. unload_count = unload_count + 1
  326. if not force_unload and unload_count > 10 then --自动unload的时候 避免一次性交互过多
  327. return
  328. end
  329. --print("-------UnloadAssetBundle---------",abName)
  330. resMgr:UnloadAssetBundle(abName,true,vo.ref_count)
  331. self.delay_unload_list[abName] = nil
  332. end
  333. end
  334. end
  335. --获得最大的缓存AB包数量
  336. function LuaResManager:GetMaxPoolObjCount()
  337. if SystemMemoryLevel.Cur == SystemMemoryLevel.Low and SystemRuntimePlatform.IsIphone() then
  338. return 10
  339. else
  340. if SystemRuntimePlatform.IsIphone() then
  341. return 50
  342. else
  343. return 100
  344. end
  345. end
  346. end
  347. --获得最大的缓存子对象数量
  348. function LuaResManager:GetMaxChildPoolObjCount()
  349. if SystemMemoryLevel.Cur == SystemMemoryLevel.Low and SystemRuntimePlatform.IsIphone() then
  350. return 3
  351. else
  352. if SystemRuntimePlatform.IsIphone() then
  353. return 10
  354. else
  355. return 10
  356. end
  357. end
  358. end
  359. ---------------------------------静态对象缓存------------------------------------
  360. --res_name 静态资源名字
  361. --res_type 资源类型 0默认 永不删除的类型
  362. function LuaResManager:AppendStaticObjName(res_name, res_type)
  363. if res_name and res_name ~= "" then
  364. self.static_obj_name_list[res_name] = res_type or 0
  365. end
  366. end
  367. --删除制定类型的静态资源
  368. function LuaResManager:RemoveStaticObj(remove_res_type)
  369. for res_name, res_type in pairs(self.static_obj_name_list) do
  370. if remove_res_type == res_type then
  371. self.static_obj_name_list[res_name] = nil
  372. local go = self.static_obj_pool_list[res_name]
  373. if go then
  374. self.static_obj_pool_list[res_name] = nil
  375. destroy(go)
  376. local delay_info = self.delay_unload_list[res_name]
  377. if delay_info == nil then
  378. delay_info = {
  379. use_time = Time.time,
  380. ref_count = 10000,
  381. }
  382. self.delay_unload_list[res_name] = delay_info
  383. else
  384. delay_info.use_time = Time.time
  385. delay_info.ref_count = delay_info.ref_count + 10000
  386. end
  387. end
  388. end
  389. end
  390. end
  391. ---------------------------------------------界面显示模型使用-----------------------------------------------
  392. function LuaResManager:GetPartModel(ref_tar, parent)
  393. local ref_info = self.role_mode_list[ref_tar] or {}
  394. local curr_roleModel = ref_info[parent]
  395. return curr_roleModel
  396. end
  397. --[[
  398. UI直接挂接模型的接口rt区分
  399. ref_tar:baseclass的对象
  400. parent:
  401. career
  402. sex:
  403. clothe_res_idid
  404. weapon_res_idid
  405. weapon_clothe_id:
  406. type
  407. layer_name:
  408. rotate:
  409. action_name_list:
  410. can_rotate:
  411. scale:
  412. position:
  413. fashion_model_id:id
  414. texture_id:id
  415. action_delta:
  416. free_param,
  417. ignore_param,
  418. skill_id:id
  419. callBack:modelClass
  420. wing_id:id
  421. image_id,
  422. head_wear_id,
  423. head_clothe_id,
  424. footmark_id:id
  425. layout_file:
  426. --]]
  427. function LuaResManager:SetRoleModel(ref_tar, parent, model_data)
  428. if parent and not ref_tar._use_delete_method then
  429. local ref_info = self.role_mode_list[ref_tar] or {}
  430. local curr_roleModel = ref_info[parent]
  431. if curr_roleModel then
  432. curr_roleModel:DeleteMe()
  433. ref_info[parent] = nil
  434. end
  435. model_data = self:ModelDataDefineVar(model_data)
  436. ref_info[parent] = UIModelClass.New(nil,parent, model_data)
  437. self.role_mode_list[ref_tar] = ref_info
  438. end
  439. end
  440. --[[
  441. 使targetTexture模式
  442. ref_tar必须是继承baseclass的对象
  443. parent:
  444. model_data = {
  445. career
  446. clothe_res_idid
  447. weapon_res_idid
  448. weapon_clothe_id:
  449. role_type
  450. size
  451. rotate
  452. action_name_list
  453. can_rotate
  454. scale
  455. position
  456. fashion_model_idid
  457. texture_id:id
  458. action_delta:
  459. renderSize:RenderTexture尺寸
  460. lCallBack:modelClass
  461. wing_id:id
  462. partner_id
  463. skill_id:
  464. head_wear_id
  465. image_id
  466. footmark_id
  467. use_bloom:使
  468. }
  469. ]]
  470. function LuaResManager:SetRoleModelByRT(ref_tar, parent, model_data)
  471. if ClientConfig.is_use_model_render then
  472. self:SetBackRoleModel(ref_tar, parent, model_data)
  473. else
  474. local function loadCameraCallBack(objs)
  475. if parent and not ref_tar._use_delete_method and objs and objs[0] then
  476. local go = newObject(objs[0])
  477. local ref_info = self.role_mode_list[ref_tar] or {}
  478. local curr_roleModel = ref_info[parent]
  479. if curr_roleModel then
  480. curr_roleModel:DeleteMe()
  481. ref_info[parent] = nil
  482. end
  483. model_data = self:ModelDataDefineVar(model_data)
  484. ref_info[parent] = UIModelClassByRT.New(go, parent, model_data)
  485. self.role_mode_list[ref_tar] = ref_info
  486. go.transform:SetParent(self.canvas_root)
  487. end
  488. end
  489. -- self:loadPrefab(ref_tar,"common","RoleMode", loadCameraCallBack)
  490. self:loadPrefab(ref_tar,"uiComponent","UIRoleMode", loadCameraCallBack)
  491. end
  492. end
  493. function LuaResManager:SetRtUIMode(ref_tar, parent, root, model_data)
  494. local function loadRtUImodelCallBack(objs)
  495. if parent and not ref_tar._use_delete_method and objs and objs[0] then
  496. local go = newObject(objs[0])
  497. local ref_info = self.ui_rt_mode_list[ref_tar] or {}
  498. local cur_rt_model = ref_info[parent]
  499. if cur_rt_model then
  500. cur_rt_model:DeleteMe()
  501. ref_info[parent] = nil
  502. end
  503. ref_info[parent] = UIRtModelClass.New(go, parent, root, model_data)
  504. self.ui_rt_mode_list[ref_tar] = ref_info
  505. go.transform:SetParent(self.canvas_root)
  506. end
  507. end
  508. self:loadPrefab(ref_tar,"uiComponent","UIRtMode", loadRtUImodelCallBack)
  509. end
  510. function LuaResManager:SetBackRoleModel(ref_tar, parent, model_data)
  511. if parent and not ref_tar._use_delete_method then
  512. local ref_info = self.role_mode_list[ref_tar] or {}
  513. local curr_roleModel = ref_info[parent]
  514. if curr_roleModel then
  515. curr_roleModel:DeleteMe()
  516. ref_info[parent] = nil
  517. end
  518. model_data = self:ModelDataDefineVar(model_data)
  519. ref_info[parent] = UIBackModelClass.New(nil,parent, model_data)
  520. self.role_mode_list[ref_tar] = ref_info
  521. end
  522. end
  523. function LuaResManager:ModelDataDefineVar(model_data)
  524. model_data = model_data or {}
  525. model_data.career = model_data.career
  526. model_data.clothe_res_id = model_data.clothe_res_id
  527. model_data.weapon_res_id = model_data.weapon_res_id
  528. model_data.weapon_clothe_id = model_data.weapon_clothe_id or ""
  529. model_data.role_type = model_data.type or model_data.role_type
  530. model_data.size = model_data.size
  531. model_data.rotate = model_data.rotate
  532. model_data.action_name_list = model_data.action_name_list
  533. model_data.can_rotate = model_data.can_rotate
  534. model_data.scale = model_data.scale
  535. model_data.position = model_data.position
  536. model_data.fashion_model_id = model_data.fashion_model_id
  537. model_data.texture_id = model_data.texture_id
  538. model_data.action_delta = model_data.action_delta
  539. model_data.renderSize = model_data.renderSize
  540. model_data.partner_id = model_data.partner_id
  541. model_data.skill_id = model_data.skill_id
  542. model_data.lCallBack = model_data.callBack
  543. model_data.wing_id = model_data.wing_id
  544. model_data.image_id = model_data.image_id
  545. model_data.head_wear_id = model_data.head_wear_id
  546. model_data.head_clothe_id = model_data.head_clothe_id
  547. model_data.footmark_id = model_data.footmark_id
  548. model_data.raycastParent = model_data.raycastParent
  549. model_data.show_shadow = model_data.show_shadow
  550. model_data.use_bloom = model_data.use_bloom
  551. model_data.skill_preview = model_data.skill_preview
  552. model_data.skill_preview_callback = model_data.skill_preview_callback
  553. model_data.camera_args = model_data.camera_args
  554. model_data.using_material = model_data.using_material
  555. model_data.using_sprite_bg = model_data.using_sprite_bg
  556. model_data.role_position_offset = model_data.role_position_offset
  557. model_data.light_id = model_data.light_id
  558. model_data.talisman_id = model_data.talisman_id
  559. model_data.layer_name = model_data.layer_name
  560. model_data.pearl_vo = model_data.pearl_vo
  561. model_data.pearl_pos = model_data.pearl_pos
  562. model_data.shadow2_plane_pos = model_data.shadow2_plane_pos
  563. model_data.shadow2_light_pos = model_data.shadow2_light_pos
  564. model_data.hat_wear_id = model_data.hat_wear_id
  565. model_data.dynamic_bone = model_data.dynamic_bone
  566. model_data.attach_game_root = model_data.attach_game_root
  567. model_data.use_green_screen = model_data.use_green_screen
  568. model_data.green_screen_camera_type = model_data.green_screen_camera_type
  569. model_data.model_rotation = model_data.model_rotation
  570. model_data.use_light_data = model_data.use_light_data
  571. model_data.psionic_effect = model_data.psionic_effect -- 圣物光环特效
  572. model_data.pokemon_diamond_effect = model_data.pokemon_diamond_effect -- 宠物宝石光环特效
  573. model_data.show_baby_attack_effect = model_data.show_baby_attack_effect -- 宝宝攻击特效
  574. model_data.show_jarvis_all_figure_data = model_data.show_jarvis_all_figure_data -- ai娘模型展示包括翅膀磁炮幻甲全套装备
  575. model_data.effect_obj_type = model_data.effect_obj_type --特效主体类型
  576. model_data.load_dance_pose = model_data.load_dance_pose --是否加载舞姿
  577. model_data.role_action_sound = model_data.role_action_sound --动作音效
  578. model_data.do_not_main_role_cloth_res_change = model_data.do_not_main_role_cloth_res_change --时装更改广播之后,不要刷新uimodelcommon的改变
  579. return model_data
  580. end
  581. --------------------------------------动态设置纹理使用------------------------------------------------------------
  582. --设置Sprite的图片:图片资源在外部icon或iconjpg目录的情况
  583. function LuaResManager:setOutsideSpriteRender(ref_tar, sp_render, respath,call_back)
  584. local function loadedCallBack(sp)
  585. if sp_render and not ref_tar._use_delete_method then
  586. if sp then
  587. sp_render.sprite = sp[0]
  588. end
  589. if call_back then
  590. call_back()
  591. end
  592. end
  593. end
  594. local abName, res_name = GameResPath.GetOutSideResAbName(respath)
  595. self:addRefCount(ref_tar,abName)
  596. resMgr:LoadSprites(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
  597. end
  598. --设置image的图片:图片资源在外部icon或iconjpg目录的情况
  599. function LuaResManager:setOutsideImageSprite(ref_tar, image, respath, setNativeSize, call_back, force_sprite)
  600. if image then
  601. self.outsize_list[image] = self.outsize_list[image] or {}
  602. local list = {ref_tar=ref_tar,image=image,respath=respath,setNativeSize=setNativeSize,call_back=call_back}
  603. table.insert(self.outsize_list[image], list)
  604. --资源已在请求中
  605. if #self.outsize_list[image] > 1 then return end
  606. end
  607. local function loadedCallBack(sp)
  608. if image then
  609. table.remove(self.outsize_list[image], 1)
  610. end
  611. if not image or #self.outsize_list[image]==0 then
  612. if image and not image:IsDestroyed() then
  613. if sp ~= nil and sp[0] then
  614. local sprite = sp[0]
  615. if force_sprite then
  616. -- if force_sprite or sprite:GetType().FullName ~= "UnityEngine.Sprite" then
  617. sprite = Util.TextureToSprite(sprite)
  618. end
  619. image.sprite = sprite
  620. if setNativeSize then
  621. image:SetNativeSize()
  622. end
  623. end
  624. end
  625. if not ref_tar._use_delete_method and call_back then
  626. call_back(sp)
  627. end
  628. else
  629. --已有新的加载请求
  630. local list = image and self.outsize_list[image][1]
  631. if list then
  632. local abName, res_name = GameResPath.GetOutSideResAbName(list.respath)
  633. self:addRefCount(ref_tar,abName)
  634. resMgr:LoadSprites(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
  635. end
  636. end
  637. end
  638. local abName, res_name = GameResPath.GetOutSideResAbName(respath)
  639. self:addRefCount(ref_tar,abName)
  640. resMgr:LoadSprites(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
  641. end
  642. --设置rawimage的图片:图片资源在外部icon或iconjpg目录的情况
  643. function LuaResManager:setOutsideRawImage(ref_tar, image, respath, setNativeSize, call_back, force_texture)
  644. local function loadedCallBack(texture)
  645. if image and not image:IsDestroyed() then
  646. if texture and texture[0] then
  647. local now_res = texture[0]
  648. if force_texture then
  649. -- if force_texture or now_res:GetType().FullName ~= "UnityEngine.Texture2D" then
  650. now_res = now_res.texture
  651. end
  652. image.texture = now_res
  653. if setNativeSize then
  654. image:SetNativeSize()
  655. end
  656. else
  657. logWarn("资源"..respath.."缺失!!")
  658. end
  659. if not ref_tar._use_delete_method and call_back then
  660. call_back()
  661. end
  662. end
  663. end
  664. local abName, res_name = GameResPath.GetOutSideResAbName(respath)
  665. self:addRefCount(ref_tar,abName)
  666. resMgr:LoadTexture(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
  667. end
  668. --设置image的图片:图片资源在UI预制中的情况
  669. --ref_tar必须是继承baseclass的对象
  670. --not_insert 不是外部设置新的图片,不用插入队列中
  671. function LuaResManager:setImageSprite(ref_tar,image,abName,resName,setNativeSize, call_back, load_level, not_insert)
  672. if not not_insert then
  673. if image then
  674. local list = {ref_tar=ref_tar,image=image,abName=abName,resName=resName,setNativeSize=setNativeSize,call_back=call_back,load_level=load_level}
  675. self.image_list = self.image_list or {}
  676. self.image_list[image] = self.image_list[image] or {}
  677. table.insert(self.image_list[image], list)
  678. if self.image_list[image] and #self.image_list[image] > 1 then return end
  679. end
  680. end
  681. load_level = load_level or ASSETS_LEVEL.HIGHT
  682. local function loadedCallBack(objs)
  683. if not ref_tar._use_delete_method and image and not image:IsDestroyed() then
  684. if objs and objs[0] then
  685. image.sprite = objs[0]
  686. if setNativeSize then
  687. image:SetNativeSize()
  688. end
  689. end
  690. if not ref_tar._use_delete_method and call_back then
  691. call_back(objs)
  692. end
  693. end
  694. if image and self.image_list[image] then
  695. if #self.image_list[image] > 0 then
  696. table.remove(self.image_list[image], 1)
  697. end
  698. if #self.image_list[image] > 0 then
  699. local next_image = self.image_list[image][1]
  700. self:setImageSprite(next_image.ref_tar,next_image.image,next_image.abName,next_image.resName,next_image.setNativeSize, next_image.call_back, next_image.load_level, true)
  701. end
  702. end
  703. end
  704. self:loadSprite(ref_tar, abName, resName, loadedCallBack, load_level)
  705. end
  706. --设置rawimage的图片:图片资源在UI预制中的情况
  707. --ref_tar必须是继承baseclass的对象
  708. function LuaResManager:setRawImage(ref_tar,image,abName,resName,setNativeSize, call_back)
  709. local function loadedCallBack(objs)
  710. if not ref_tar._use_delete_method and image and not image:IsDestroyed() then
  711. if objs and objs[0] then
  712. image.texture = objs[0]
  713. if setNativeSize then
  714. image:SetNativeSize()
  715. end
  716. end
  717. if not ref_tar._use_delete_method and call_back then
  718. call_back()
  719. end
  720. end
  721. end
  722. self:loadTexture(ref_tar,abName,{resName},loadedCallBack, ASSETS_LEVEL.HIGHT)
  723. end
  724. -----------------------------------------对象加载----------------------------------------------------------
  725. --目前只有baseview使用
  726. function LuaResManager:LoadRes(ref_tar, res_type, abName, pfNameList, callBack)
  727. if res_type == nil or res_type == LuaResManager.RES_TYPE.PREFAB then
  728. self:loadPrefabs(ref_tar, abName, pfNameList, callBack, nil, ASSETS_LEVEL.HIGHT)
  729. elseif res_type == LuaResManager.RES_TYPE.SPRITE then
  730. self:loadSprites(ref_tar, abName, pfNameList, callBack, ASSETS_LEVEL.HIGHT)
  731. end
  732. end
  733. --加载一个预设
  734. function LuaResManager:loadPrefab(ref_tar,abName,pfName,callBack, ignore_pool, load_level)
  735. load_level = load_level or ASSETS_LEVEL.NORMAL
  736. if not ref_tar or ref_tar._use_delete_method then return end
  737. local reload_prefab = true
  738. if not ignore_pool then
  739. local obj_pool = self:GetObjFormPool(ref_tar, abName, pfName)
  740. if obj_pool and not IsNull(obj_pool) then
  741. reload_prefab = false
  742. if callBack then
  743. --print("--------LoadPool-------",abName)
  744. callBack({[0] = obj_pool}, true)
  745. -- print("---------loadPrefab from pool -----",abName,pfName)
  746. end
  747. end
  748. end
  749. if reload_prefab then
  750. self:addRefCount(ref_tar,abName)
  751. -- print("--------LoadPrefab-------",abName)
  752. resMgr:LoadPrefab(abName, {pfName}, callBack, load_level)
  753. -- print("---------loadPrefab from create -----",abName,pfName)
  754. -- PrintCallStack()
  755. end
  756. end
  757. --加载多个预设
  758. function LuaResManager:loadPrefabs(ref_tar,abName,pfNameList,callBack, ignore_pool, load_level)
  759. load_level = load_level or ASSETS_LEVEL.NORMAL
  760. if not ref_tar or ref_tar._use_delete_method then return end
  761. if not ignore_pool then
  762. local obj_pool = self:GetObjListFormPool(ref_tar, abName, pfNameList)
  763. if obj_pool then
  764. if callBack then
  765. callBack(obj_pool, true)
  766. end
  767. else
  768. self:addRefCount(ref_tar,abName)
  769. resMgr:LoadPrefab(abName, pfNameList, callBack, load_level)
  770. end
  771. else
  772. self:addRefCount(ref_tar,abName)
  773. resMgr:LoadPrefab(abName, pfNameList, callBack, load_level)
  774. end
  775. end
  776. --加载一个对象
  777. function LuaResManager:loadObject(ref_tar,abName,pfName,callBack, ignore_pool, load_level)
  778. load_level = load_level or ASSETS_LEVEL.NORMAL
  779. if not ref_tar or ref_tar._use_delete_method then return end
  780. local reload_prefab = true
  781. if not ignore_pool then
  782. local obj_pool = self:GetObjFormPool(ref_tar, abName, pfName)
  783. if obj_pool then
  784. reload_prefab = false
  785. if callBack then
  786. callBack({[0] = obj_pool}, true)
  787. end
  788. end
  789. end
  790. if reload_prefab then
  791. self:addRefCount(ref_tar,abName)
  792. resMgr:LoadObject(abName, pfName, callBack, load_level)
  793. end
  794. end
  795. --加载一个材质
  796. function LuaResManager:loadMateaial(ref_tar,abName,pfName,callBack, load_level)
  797. load_level = load_level or ASSETS_LEVEL.NORMAL
  798. if not ref_tar or ref_tar._use_delete_method then return end
  799. self:addRefCount(ref_tar,abName)
  800. resMgr:LoadMaterial(abName, pfName, callBack, load_level)
  801. end
  802. --加载一个精灵
  803. function LuaResManager:loadSprite(ref_tar,abName,pfName,callBack,load_level)
  804. load_level = load_level or ASSETS_LEVEL.HIGHT
  805. self:addRefCount(ref_tar,abName)
  806. resMgr:LoadSprite(abName, pfName, callBack, load_level)
  807. end
  808. --加载多个精灵
  809. function LuaResManager:loadSprites(ref_tar,abName,pfNameList,callBack,load_level)
  810. load_level = load_level or ASSETS_LEVEL.HIGHT
  811. self:addRefCount(ref_tar,abName)
  812. resMgr:LoadSprites(abName, pfNameList, callBack, load_level)
  813. end
  814. --加载一张纹理
  815. function LuaResManager:loadTexture(ref_tar,abName,pfNameList,callBack,load_level)
  816. load_level = load_level or ASSETS_LEVEL.HIGHT
  817. self:addRefCount(ref_tar,abName)
  818. resMgr:LoadTexture(abName, pfNameList, callBack, load_level)
  819. end
  820. --加载二进制数据
  821. function LuaResManager:loadTextAsset(ref_tar,abName,pfName,callBack,load_level)
  822. load_level= load_level or ASSETS_LEVEL.NORMAL
  823. self:addRefCount(ref_tar,abName)
  824. resMgr:LoadTextAsset(abName, pfName, callBack,load_level)
  825. end
  826. --加载并播放声音
  827. function LuaResManager:loadSound(ref_tar, abName, resName, is_loop,vol_modulus, speed)
  828. local custom_speed = speed or 1
  829. if abName ~= LuaSoundManager.SOUND_PRE[LuaSoundManager.SOUND_TYPE.SKILL] then
  830. self:addRefCount(ref_tar, abName)
  831. end
  832. if tonumber(AppConst.EnglineVer) >= 89 then
  833. return soundMgr:PlayEffect(abName, resName, is_loop,vol_modulus, custom_speed)
  834. else
  835. return soundMgr:PlayEffect(abName, resName, is_loop,vol_modulus)
  836. end
  837. end
  838. --加载外部二进制文件,目前只有db加载使用,是常驻内存的,不需引用计数
  839. function LuaResManager:loadOutsideTextAsset(ref_tar, respath, callBack)
  840. outsideResMgr:LoadTextAsset(respath, callBack,OutSideFileType.BYTE)
  841. end
  842. --停止一个声音
  843. function LuaResManager:stopSound(ref_tar, abName, effect_id)
  844. if abName ~= LuaSoundManager.SOUND_PRE[LuaSoundManager.SOUND_TYPE.SKILL] then
  845. -- self:reduceRefCount(ref_tar, abName)
  846. end
  847. if effect_id then
  848. local res_id = tonumber(effect_id)
  849. if res_id then
  850. soundMgr:StopEffect(effect_id)
  851. else
  852. logWarn("stopSound error abName = " .. abName)
  853. end
  854. end
  855. end
  856. function LuaResManager:LoadPrefabView(ref_tar, abName, pfName, callBack)
  857. local item = {
  858. DeleteMe=LuaResManager.__DestroyPrefab,
  859. is_loaded = false, gameObject = false, transform = false,
  860. SetPosition = function(item, x, y)
  861. if item.is_loaded then
  862. SetLocalPosition(item.transform, x, y, 0)
  863. else
  864. item.cache_pos = {x=x, y=y}
  865. end
  866. end,
  867. SetVisible = function(item, is_show)
  868. if item.is_loaded then
  869. item.gameObject:SetActive(is_show)
  870. else
  871. item.cache_visible = is_show
  872. end
  873. end,
  874. SetData = function(item, i, v)
  875. if item.is_loaded and item.UpdateView then
  876. item:UpdateView(i, v)
  877. else
  878. item.cache_data = {index=i, data=v}
  879. end
  880. end,
  881. AddUIComponent = UIPartical.AddUIComponent,
  882. RemoveUIComponent = UIPartical.RemoveUIComponent,
  883. }
  884. local on_load_prefab = function ( prefab )
  885. if not prefab or not prefab[0] then return end
  886. if not ref_tar or ref_tar._use_delete_method or item._use_delete_method then return end
  887. local go = newObject(prefab[0])
  888. go.name = pfName
  889. item.gameObject = go
  890. item.transform = go.transform
  891. item.is_loaded = true
  892. UIPartical.BeforeLoad(item)
  893. if callBack then
  894. callBack(item)
  895. end
  896. if item.cache_visible ~= nil then
  897. item.gameObject:SetActive(item.cache_visible)
  898. item.cache_visible = nil
  899. end
  900. if item.cache_pos then
  901. SetLocalPosition(item.transform, item.cache_pos.x, item.cache_pos.y, 0)
  902. item.cache_pos = nil
  903. end
  904. if item.cache_data and item.UpdateView then
  905. item:UpdateView(item.cache_data.index, item.cache_data.data)
  906. item.cache_data = nil
  907. end
  908. end
  909. if not IsUseLocalResource then
  910. LuaResManager:getInstance():loadPrefab(ref_tar,abName,pfName, on_load_prefab, true, ASSETS_LEVEL.HIGHT)
  911. else
  912. --延迟到下一帧再加载,模拟异步加载
  913. local function delay_method( )
  914. local prefab = LuaFramework.PanelMgrEx.GetInstance():LoadPrefabInLocalByFile("Assets/LuaFramework/AssetBundleRes/ui/"..abName.."/prefab/"..pfName..".prefab")
  915. on_load_prefab({[0]=prefab})
  916. end
  917. setTimeout(delay_method, 0.001)
  918. end
  919. return item
  920. end
  921. function LuaResManager.__DestroyPrefab( self )
  922. --回收从LoadPrefabView用的,外部别调用本接口
  923. if self.is_loaded and self.gameObject then
  924. UIPartical.AfterDestroy(self)
  925. if self.destroy_callback then
  926. self.destroy_callback(self)
  927. end
  928. destroy(self.gameObject)
  929. end
  930. self._use_delete_method = true
  931. end