源战役客户端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

1023 строки
33 KiB

--lua端的资源管理器
LuaResManager = LuaResManager or BaseClass()
local LuaResManager = LuaResManager
local soundMgr = soundMgr
local resMgr = resMgr
local Array_New = Array.New
local Time = Time
LuaResManager.RES_TYPE = {
PREFAB = 1,
SPRITE = 2,
RENDERTEXTURE = 3,
TEXTASSET = 4,
}
function LuaResManager:__init()
LuaResManager.Instance = self
--assetbundle包资源引用列表
self.ref_count_list = {}
--延迟unload的资源列表
self.delay_unload_list = {}
--界面显示模型对象列表
self.role_mode_list = {}
--3dUIRt对象列表
self.ui_rt_mode_list = {}
--每隔多长时间执行一次内存清理
self.clear_memory_time = 10
if SystemRuntimePlatform.IsIphone() then
self.clear_memory_time = 5
elseif SystemRuntimePlatform.IsAndroid() then
if SystemMemoryLevel.Cur == SystemMemoryLevel.Low then
self.clear_memory_time = 15
end
end
--定时清理资源函数
local function onCheckToClearObjPool()
self:CheckToClearObjPool()
end
GlobalTimerQuest:AddPeriodQuest(onCheckToClearObjPool, self.clear_memory_time, -1)
--资源对象池
self.obj_pool_list = {}
--对象池总ab包对象数量
self.obj_pool_ab_count = 0
--静态的缓存对象名字列表
self.static_obj_name_list = {}
--静态的缓存对象列表
self.static_obj_pool_list = {}
self.static_obj_pool_container = GameObject.New("static_obj_pool_container").transform
self.static_obj_pool_container:SetParent(panelMgr:GetParent("SceneObjContainer"))
self.static_obj_pool_container.gameObject:SetActive(false)
self.canvas_root = GameObject.Find("root/Canvas").transform
--外部资源使用记录,处理请求未返回时触发的二次请求
self.outsize_list = {}
local callfunc = function(module_name,func)
if module_name == "res" and func == "objlist" then
local data = {}
for k,v in pairs(self.static_obj_pool_list) do
print("---static----",k,v)
data["static_" .. k] = true
end
for k,v in pairs(self.obj_pool_list) do
for resname,obj in pairs(v["res_name_list"]) do
print("---obj----",resname,obj)
if not data["obj_" .. resname] then
data["obj_" .. resname] = 0
end
data["obj_" .. resname] = data["obj_" .. resname] + 1
end
end
GlobalEventSystem:Fire(EventName.SENT_MODULE_DEBUG_DATA,"res", data)
end
end
GlobalEventSystem:Bind(EventName.DO_MODULE_DEBUG_FUNC,callfunc)
local func = function()
callfunc("res","objlist")
end
GlobalEventSystem:Bind(EventName.CHECK_MODULE_DEBUG_DATA,func)
end
function LuaResManager:getInstance()
if LuaResManager.Instance == nil then
LuaResManager.Instance = LuaResManager.New()
end
return LuaResManager.Instance
end
----------------------------资源引用计数-------------------------------------------
--增加引用数
function LuaResManager:addRefCount(ref_tar,abName,count)
count = count or 1
if count > 0 and abName then
local ref_info = self.ref_count_list[ref_tar] or {}
local ref_count = ref_info[abName] and ref_info[abName] + count or count
ref_info[abName] = ref_count
self.ref_count_list[ref_tar] = ref_info
--发现资源在待卸载列表,更新其使用时间
local delay_info = self.delay_unload_list[abName]
if delay_info then
delay_info.use_time = Time.time
end
end
end
--减少引用数
function LuaResManager:reduceRefCount(ref_tar,abName)
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
self.ref_count_list[ref_tar][abName] = self.ref_count_list[ref_tar][abName] - 1
end
end
--清除关联对象的所有资源关联信息
function LuaResManager:clearReference(ref_tar)
self:clearRefCount(ref_tar)
self:clearRoleMode(ref_tar)
end
--清除该对象的所有资源引用
function LuaResManager:clearRefCount(ref_tar)
local ref_count_obj = self.ref_count_list[ref_tar]
if ref_count_obj then
for abName,ref_count in pairs(ref_count_obj) do
if ref_count and ref_count > 0 then
local delay_info = self.delay_unload_list[abName]
if delay_info == nil then
delay_info = {
use_time = Time.time,
ref_count = ref_count,
}
self.delay_unload_list[abName] = delay_info
else
delay_info.use_time = Time.time
delay_info.ref_count = delay_info.ref_count + ref_count
end
end
end
self.ref_count_list[ref_tar] = nil
end
end
--清除该对象的所有角色模型的引用
function LuaResManager:clearRoleMode(ref_tar)
local ref_count_obj = self.role_mode_list[ref_tar]
if ref_count_obj then
for p,uiModelClass in pairs(ref_count_obj) do
uiModelClass:DeleteMe()
end
self.role_mode_list[ref_tar] = nil
end
end
--清除该对象的所有3d的引用
function LuaResManager:clearRtUIMode(ref_tar)
local ref_count_obj = self.ui_rt_mode_list[ref_tar]
if ref_count_obj then
for p,uiRtModelClass in pairs(ref_count_obj) do
uiRtModelClass:DeleteMe()
end
self.ui_rt_mode_list[ref_tar] = nil
end
end
-------------------------------------------对象缓存池管理-----------------------------------------------
--添加游戏对象列表进入缓存池 通过loadPrefabs加载的资源对象列表
function LuaResManager:AddObjListToPool(ref_tar, abName, resName_list, gameObject_list)
if gameObject_list then
self:CreateABObjPool(abName)
local resName = "resName_list" .. table.concat(resName_list, "_")
local res_list = self.obj_pool_list[abName]["res_name_list"][resName]
if res_list == nil then
res_list = Array_New()
self.obj_pool_list[abName]["res_name_list"][resName] = res_list
end
if res_list:GetSize() > self:GetMaxChildPoolObjCount() then
for i, go in ipairs(gameObject_list) do
destroy(go)
end
else
self:reduceRefCount(ref_tar, abName)
for i, go in ipairs(gameObject_list) do
go.transform:SetParent(self.obj_pool_list[abName].container)
end
res_list:PushBack(gameObject_list)
self.obj_pool_list[abName].last_change_time = Time.time
end
end
end
--添加游戏对象进入缓存池 通过loadPrefab加载的资源对象
function LuaResManager:AddObjToPool(ref_tar, abName, resName, gameObject, alpha, is_default_color)
if gameObject then
--非静态缓存对象
if abName and (not self.static_obj_name_list[abName] or self.static_obj_pool_list[abName]) then
self:CreateABObjPool(abName)
local res_list = self.obj_pool_list[abName]["res_name_list"][resName]
if res_list == nil then
res_list = Array_New()
self.obj_pool_list[abName]["res_name_list"][resName] = res_list
end
if res_list:GetSize() > self:GetMaxChildPoolObjCount() then
--大于限制的相同对象缓存数量,不放入缓存
destroy(gameObject)
else
if not IsNull(gameObject) then
--放入缓存后减少引用计数
self:reduceRefCount(ref_tar, abName)
gameObject.transform:SetParent(self.obj_pool_list[abName].container)
res_list:PushBack(gameObject)
self.obj_pool_list[abName].last_change_time = Time.time
end
end
else
--静态缓存对象
if not abName or self.static_obj_pool_list[abName] then
--只缓存一个相同的静态对象
destroy(gameObject)
else
--重置shader
-- if (alpha and alpha < 1) or is_default_color == false then
-- local skinned_mesh_renderer = gameObject:GetComponentInChildren(typeof(UnityEngine.SkinnedMeshRenderer))
-- if skinned_mesh_renderer then
-- local sd = SceneManager:getInstance():GetTextureShader()
-- if sd then
-- skinned_mesh_renderer.material.shader = sd
-- end
-- end
-- end
self:reduceRefCount(ref_tar, abName)
self.static_obj_pool_list[abName] = gameObject
gameObject.transform:SetParent(self.static_obj_pool_container)
--设置引用计数10000,避免静态缓存对象异常情况引用归零被销毁
self:addRefCount(self.static_obj_pool_list, abName, 10000)
end
end
end
end
--添加缓存对象时初始化缓存信息
function LuaResManager:CreateABObjPool(abName)
if not self.obj_pool_container then
self.obj_pool_container = UiFactory.createChild(panelMgr:GetParent("SceneObjContainer"), UIType.EmptyObject, "obj_pool_container").transform
self.obj_pool_container.gameObject:SetActive(false)
end
if self.obj_pool_list[abName] == nil then
--缓存AB包超过最大数量时,强制清理最久未使用的那个资源
if self.obj_pool_ab_count >= self:GetMaxPoolObjCount() then
local min_change_time = 9999999
local min_abName = nil
for pool_abName, ab_info in pairs(self.obj_pool_list) do
if ab_info.last_change_time < min_change_time and ab_info then
min_change_time = ab_info.last_change_time
min_abName = pool_abName
end
end
if min_abName then
self:ClearObjPool(min_abName)
end
end
self.obj_pool_ab_count = self.obj_pool_ab_count + 1
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}
end
end
--从缓存池中获取游戏对象列表
function LuaResManager:GetObjListFormPool(ref_tar, abName, resName_list)
local resName = "resName_list" .. table.concat(resName_list, "_")
if self.obj_pool_list[abName] and self.obj_pool_list[abName]["res_name_list"][resName] then
local gameObject_list = self.obj_pool_list[abName]["res_name_list"][resName]:PopBack()
if gameObject_list then
self.obj_pool_list[abName].last_change_time = Time.time
self:addRefCount(ref_tar, abName)
return gameObject_list
end
end
end
--从缓存池中获取游戏对象
function LuaResManager:GetObjFormPool(ref_tar, abName, resName)
if self.static_obj_name_list[abName] then
local go = self.static_obj_pool_list[abName]
if go then
self.static_obj_pool_list[abName] = nil
self:addRefCount(ref_tar, abName)
return go
end
end
if self.obj_pool_list[abName] and self.obj_pool_list[abName]["res_name_list"][resName] then
local gameObject = self.obj_pool_list[abName]["res_name_list"][resName]:PopBack()
if gameObject then
self.obj_pool_list[abName].last_change_time = Time.time
self:addRefCount(ref_tar, abName)
return gameObject
end
end
end
--清除单个ab包的缓存游戏对象
function LuaResManager:ClearObjPool(abName, ab_info)
ab_info = ab_info or self.obj_pool_list[abName]
if ab_info then
local all_ref_count = 0
for resName, res_list in pairs(ab_info.res_name_list) do
if res_list and res_list:GetSize() > 0 then
all_ref_count = all_ref_count + res_list:GetSize()
end
end
self:addRefCount(self.obj_pool_container, abName, all_ref_count)
self:clearReference(self.obj_pool_container)
destroy(ab_info.container.gameObject)
self.obj_pool_list[abName] = nil
self.obj_pool_ab_count = self.obj_pool_ab_count - 1
end
end
--清除所有缓存池游戏对象,切换场景时调用
function LuaResManager:ClearAllObjPool()
local obj = nil
if self.obj_pool_container then
local all_ref_count = 0
for abName, ab_info in pairs(self.obj_pool_list) do
all_ref_count = 0
for resName, res_list in pairs(ab_info.res_name_list) do
if res_list and res_list:GetSize() > 0 then
all_ref_count = all_ref_count + res_list:GetSize()
end
end
self:addRefCount(self.obj_pool_container, abName, all_ref_count)
end
self:clearReference(self.obj_pool_container)
destroy(self.obj_pool_container.gameObject, true)
self.obj_pool_container = nil
end
self.obj_pool_list = {}
self.obj_pool_ab_count = 0
self:CheckToClearObjPool(true)
end
--有10秒定时器检测,切换场景时自动调用一次
function LuaResManager:CheckToClearObjPool(force_unload)
local unload_count = 0
for abName,vo in pairs(self.delay_unload_list) do
if force_unload or Time.time - vo.use_time >= self.clear_memory_time then --延迟一定时间unload ab包
unload_count = unload_count + 1
if not force_unload and unload_count > 10 then --自动unload的时候 避免一次性交互过多
return
end
--print("-------UnloadAssetBundle---------",abName)
resMgr:UnloadAssetBundle(abName,true,vo.ref_count)
self.delay_unload_list[abName] = nil
end
end
end
--获得最大的缓存AB包数量
function LuaResManager:GetMaxPoolObjCount()
if SystemMemoryLevel.Cur == SystemMemoryLevel.Low and SystemRuntimePlatform.IsIphone() then
return 10
else
if SystemRuntimePlatform.IsIphone() then
return 50
else
return 100
end
end
end
--获得最大的缓存子对象数量
function LuaResManager:GetMaxChildPoolObjCount()
if SystemMemoryLevel.Cur == SystemMemoryLevel.Low and SystemRuntimePlatform.IsIphone() then
return 3
else
if SystemRuntimePlatform.IsIphone() then
return 10
else
return 10
end
end
end
---------------------------------静态对象缓存------------------------------------
--res_name 静态资源名字
--res_type 资源类型 0默认 永不删除的类型
function LuaResManager:AppendStaticObjName(res_name, res_type)
if res_name and res_name ~= "" then
self.static_obj_name_list[res_name] = res_type or 0
end
end
--删除制定类型的静态资源
function LuaResManager:RemoveStaticObj(remove_res_type)
for res_name, res_type in pairs(self.static_obj_name_list) do
if remove_res_type == res_type then
self.static_obj_name_list[res_name] = nil
local go = self.static_obj_pool_list[res_name]
if go then
self.static_obj_pool_list[res_name] = nil
destroy(go)
local delay_info = self.delay_unload_list[res_name]
if delay_info == nil then
delay_info = {
use_time = Time.time,
ref_count = 10000,
}
self.delay_unload_list[res_name] = delay_info
else
delay_info.use_time = Time.time
delay_info.ref_count = delay_info.ref_count + 10000
end
end
end
end
end
---------------------------------------------界面显示模型使用-----------------------------------------------
function LuaResManager:GetPartModel(ref_tar, parent)
local ref_info = self.role_mode_list[ref_tar] or {}
local curr_roleModel = ref_info[parent]
return curr_roleModel
end
--[[
UI直接挂接模型的接口,与rt区分
ref_tar:必须是继承baseclass的对象
parent:父容器
career:职业
sex:性别
clothe_res_id:衣服模型id
weapon_res_id:武器模型id
weapon_clothe_id: 武器时装
type:类型
layer_name:层级
rotate:旋转度数
action_name_list:动作集
can_rotate:是否支持左右旋转
scale:缩放
position:位置
fashion_model_id:时装模型id
texture_id:时装贴图id
action_delta:动作间隔
free_param,
ignore_param,
skill_id:技能id
callBack:加载完成的回调,返回modelClass
wing_id:翅膀id
image_id,
head_wear_id,
head_clothe_id,
footmark_id:足迹id
layout_file:是否支持配置控制模型大小,位置,角度参数
--]]
function LuaResManager:SetRoleModel(ref_tar, parent, model_data)
if parent and not ref_tar._use_delete_method then
local ref_info = self.role_mode_list[ref_tar] or {}
local curr_roleModel = ref_info[parent]
if curr_roleModel then
curr_roleModel:DeleteMe()
ref_info[parent] = nil
end
model_data = self:ModelDataDefineVar(model_data)
ref_info[parent] = UIModelClass.New(nil,parent, model_data)
self.role_mode_list[ref_tar] = ref_info
end
end
--[[
使用设置摄像机的targetTexture模式
ref_tar必须是继承baseclass的对象
parent:父容器
model_data = {
career:职业
clothe_res_id:衣服模型id
weapon_res_id:武器模型id
weapon_clothe_id: 武器时装
role_type:类型
size:大小
rotate:读书
action_name_list:动作集
can_rotate:是否支持左右旋转
scale:缩放
position:位置
fashion_model_id:时装模型id
texture_id:时装贴图id
action_delta:
renderSize:自定义RenderTexture尺寸
lCallBack:加载完成的回调,返回modelClass
wing_id:翅膀id
partner_id:
skill_id:
head_wear_id
image_id
footmark_id
use_bloom:使用泛光效果
}
]]
function LuaResManager:SetRoleModelByRT(ref_tar, parent, model_data)
if ClientConfig.is_use_model_render then
self:SetBackRoleModel(ref_tar, parent, model_data)
else
local function loadCameraCallBack(objs)
if parent and not ref_tar._use_delete_method and objs and objs[0] then
local go = newObject(objs[0])
local ref_info = self.role_mode_list[ref_tar] or {}
local curr_roleModel = ref_info[parent]
if curr_roleModel then
curr_roleModel:DeleteMe()
ref_info[parent] = nil
end
model_data = self:ModelDataDefineVar(model_data)
ref_info[parent] = UIModelClassByRT.New(go, parent, model_data)
self.role_mode_list[ref_tar] = ref_info
go.transform:SetParent(self.canvas_root)
end
end
-- self:loadPrefab(ref_tar,"common","RoleMode", loadCameraCallBack)
self:loadPrefab(ref_tar,"uiComponent","UIRoleMode", loadCameraCallBack)
end
end
function LuaResManager:SetRtUIMode(ref_tar, parent, root, model_data)
local function loadRtUImodelCallBack(objs)
if parent and not ref_tar._use_delete_method and objs and objs[0] then
local go = newObject(objs[0])
local ref_info = self.ui_rt_mode_list[ref_tar] or {}
local cur_rt_model = ref_info[parent]
if cur_rt_model then
cur_rt_model:DeleteMe()
ref_info[parent] = nil
end
ref_info[parent] = UIRtModelClass.New(go, parent, root, model_data)
self.ui_rt_mode_list[ref_tar] = ref_info
go.transform:SetParent(self.canvas_root)
end
end
self:loadPrefab(ref_tar,"uiComponent","UIRtMode", loadRtUImodelCallBack)
end
function LuaResManager:SetBackRoleModel(ref_tar, parent, model_data)
if parent and not ref_tar._use_delete_method then
local ref_info = self.role_mode_list[ref_tar] or {}
local curr_roleModel = ref_info[parent]
if curr_roleModel then
curr_roleModel:DeleteMe()
ref_info[parent] = nil
end
model_data = self:ModelDataDefineVar(model_data)
ref_info[parent] = UIBackModelClass.New(nil,parent, model_data)
self.role_mode_list[ref_tar] = ref_info
end
end
function LuaResManager:ModelDataDefineVar(model_data)
model_data = model_data or {}
model_data.career = model_data.career
model_data.clothe_res_id = model_data.clothe_res_id
model_data.weapon_res_id = model_data.weapon_res_id
model_data.weapon_clothe_id = model_data.weapon_clothe_id or ""
model_data.role_type = model_data.type or model_data.role_type
model_data.size = model_data.size
model_data.rotate = model_data.rotate
model_data.action_name_list = model_data.action_name_list
model_data.can_rotate = model_data.can_rotate
model_data.scale = model_data.scale
model_data.position = model_data.position
model_data.fashion_model_id = model_data.fashion_model_id
model_data.texture_id = model_data.texture_id
model_data.action_delta = model_data.action_delta
model_data.renderSize = model_data.renderSize
model_data.partner_id = model_data.partner_id
model_data.skill_id = model_data.skill_id
model_data.lCallBack = model_data.callBack
model_data.wing_id = model_data.wing_id
model_data.image_id = model_data.image_id
model_data.head_wear_id = model_data.head_wear_id
model_data.head_clothe_id = model_data.head_clothe_id
model_data.footmark_id = model_data.footmark_id
model_data.raycastParent = model_data.raycastParent
model_data.show_shadow = model_data.show_shadow
model_data.use_bloom = model_data.use_bloom
model_data.skill_preview = model_data.skill_preview
model_data.skill_preview_callback = model_data.skill_preview_callback
model_data.camera_args = model_data.camera_args
model_data.using_material = model_data.using_material
model_data.using_sprite_bg = model_data.using_sprite_bg
model_data.role_position_offset = model_data.role_position_offset
model_data.light_id = model_data.light_id
model_data.talisman_id = model_data.talisman_id
model_data.layer_name = model_data.layer_name
model_data.pearl_vo = model_data.pearl_vo
model_data.pearl_pos = model_data.pearl_pos
model_data.shadow2_plane_pos = model_data.shadow2_plane_pos
model_data.shadow2_light_pos = model_data.shadow2_light_pos
model_data.hat_wear_id = model_data.hat_wear_id
model_data.dynamic_bone = model_data.dynamic_bone
model_data.attach_game_root = model_data.attach_game_root
model_data.use_green_screen = model_data.use_green_screen
model_data.green_screen_camera_type = model_data.green_screen_camera_type
model_data.model_rotation = model_data.model_rotation
model_data.use_light_data = model_data.use_light_data
model_data.psionic_effect = model_data.psionic_effect -- 圣物光环特效
model_data.pokemon_diamond_effect = model_data.pokemon_diamond_effect -- 宠物宝石光环特效
model_data.show_baby_attack_effect = model_data.show_baby_attack_effect -- 宝宝攻击特效
model_data.show_jarvis_all_figure_data = model_data.show_jarvis_all_figure_data -- ai娘模型展示包括翅膀磁炮幻甲全套装备
model_data.effect_obj_type = model_data.effect_obj_type --特效主体类型
model_data.load_dance_pose = model_data.load_dance_pose --是否加载舞姿
model_data.role_action_sound = model_data.role_action_sound --动作音效
model_data.do_not_main_role_cloth_res_change = model_data.do_not_main_role_cloth_res_change --时装更改广播之后,不要刷新uimodelcommon的改变
return model_data
end
--------------------------------------动态设置纹理使用------------------------------------------------------------
--设置Sprite的图片:图片资源在外部icon或iconjpg目录的情况
function LuaResManager:setOutsideSpriteRender(ref_tar, sp_render, respath,call_back)
local function loadedCallBack(sp)
if sp_render and not ref_tar._use_delete_method then
if sp then
sp_render.sprite = sp[0]
end
if call_back then
call_back()
end
end
end
local abName, res_name = GameResPath.GetOutSideResAbName(respath)
self:addRefCount(ref_tar,abName)
resMgr:LoadSprites(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
end
--设置image的图片:图片资源在外部icon或iconjpg目录的情况
function LuaResManager:setOutsideImageSprite(ref_tar, image, respath, setNativeSize, call_back, force_sprite)
if image then
self.outsize_list[image] = self.outsize_list[image] or {}
local list = {ref_tar=ref_tar,image=image,respath=respath,setNativeSize=setNativeSize,call_back=call_back}
table.insert(self.outsize_list[image], list)
--资源已在请求中
if #self.outsize_list[image] > 1 then return end
end
local function loadedCallBack(sp)
if image then
table.remove(self.outsize_list[image], 1)
end
if not image or #self.outsize_list[image]==0 then
if image and not image:IsDestroyed() then
if sp ~= nil and sp[0] then
local sprite = sp[0]
if force_sprite then
-- if force_sprite or sprite:GetType().FullName ~= "UnityEngine.Sprite" then
sprite = Util.TextureToSprite(sprite)
end
image.sprite = sprite
if setNativeSize then
image:SetNativeSize()
end
end
end
if not ref_tar._use_delete_method and call_back then
call_back(sp)
end
else
--已有新的加载请求
local list = image and self.outsize_list[image][1]
if list then
local abName, res_name = GameResPath.GetOutSideResAbName(list.respath)
self:addRefCount(ref_tar,abName)
resMgr:LoadSprites(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
end
end
end
local abName, res_name = GameResPath.GetOutSideResAbName(respath)
self:addRefCount(ref_tar,abName)
resMgr:LoadSprites(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
end
--设置rawimage的图片:图片资源在外部icon或iconjpg目录的情况
function LuaResManager:setOutsideRawImage(ref_tar, image, respath, setNativeSize, call_back, force_texture)
local function loadedCallBack(texture)
if image and not image:IsDestroyed() then
if texture and texture[0] then
local now_res = texture[0]
if force_texture then
-- if force_texture or now_res:GetType().FullName ~= "UnityEngine.Texture2D" then
now_res = now_res.texture
end
image.texture = now_res
if setNativeSize then
image:SetNativeSize()
end
else
logWarn("资源"..respath.."缺失!!")
end
if not ref_tar._use_delete_method and call_back then
call_back()
end
end
end
local abName, res_name = GameResPath.GetOutSideResAbName(respath)
self:addRefCount(ref_tar,abName)
resMgr:LoadTexture(abName, {res_name}, loadedCallBack, ASSETS_LEVEL.HIGHT)
end
--设置image的图片:图片资源在UI预制中的情况
--ref_tar必须是继承baseclass的对象
--not_insert 不是外部设置新的图片,不用插入队列中
function LuaResManager:setImageSprite(ref_tar,image,abName,resName,setNativeSize, call_back, load_level, not_insert)
if not not_insert then
if image then
local list = {ref_tar=ref_tar,image=image,abName=abName,resName=resName,setNativeSize=setNativeSize,call_back=call_back,load_level=load_level}
self.image_list = self.image_list or {}
self.image_list[image] = self.image_list[image] or {}
table.insert(self.image_list[image], list)
if self.image_list[image] and #self.image_list[image] > 1 then return end
end
end
load_level = load_level or ASSETS_LEVEL.HIGHT
local function loadedCallBack(objs)
if not ref_tar._use_delete_method and image and not image:IsDestroyed() then
if objs and objs[0] then
image.sprite = objs[0]
if setNativeSize then
image:SetNativeSize()
end
end
if not ref_tar._use_delete_method and call_back then
call_back(objs)
end
end
if image and self.image_list[image] then
if #self.image_list[image] > 0 then
table.remove(self.image_list[image], 1)
end
if #self.image_list[image] > 0 then
local next_image = self.image_list[image][1]
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)
end
end
end
self:loadSprite(ref_tar, abName, resName, loadedCallBack, load_level)
end
--设置rawimage的图片:图片资源在UI预制中的情况
--ref_tar必须是继承baseclass的对象
function LuaResManager:setRawImage(ref_tar,image,abName,resName,setNativeSize, call_back)
local function loadedCallBack(objs)
if not ref_tar._use_delete_method and image and not image:IsDestroyed() then
if objs and objs[0] then
image.texture = objs[0]
if setNativeSize then
image:SetNativeSize()
end
end
if not ref_tar._use_delete_method and call_back then
call_back()
end
end
end
self:loadTexture(ref_tar,abName,{resName},loadedCallBack, ASSETS_LEVEL.HIGHT)
end
-----------------------------------------对象加载----------------------------------------------------------
--目前只有baseview使用
function LuaResManager:LoadRes(ref_tar, res_type, abName, pfNameList, callBack)
if res_type == nil or res_type == LuaResManager.RES_TYPE.PREFAB then
self:loadPrefabs(ref_tar, abName, pfNameList, callBack, nil, ASSETS_LEVEL.HIGHT)
elseif res_type == LuaResManager.RES_TYPE.SPRITE then
self:loadSprites(ref_tar, abName, pfNameList, callBack, ASSETS_LEVEL.HIGHT)
end
end
--加载一个预设
function LuaResManager:loadPrefab(ref_tar,abName,pfName,callBack, ignore_pool, load_level)
load_level = load_level or ASSETS_LEVEL.NORMAL
if not ref_tar or ref_tar._use_delete_method then return end
local reload_prefab = true
if not ignore_pool then
local obj_pool = self:GetObjFormPool(ref_tar, abName, pfName)
if obj_pool and not IsNull(obj_pool) then
reload_prefab = false
if callBack then
--print("--------LoadPool-------",abName)
callBack({[0] = obj_pool}, true)
-- print("---------loadPrefab from pool -----",abName,pfName)
end
end
end
if reload_prefab then
self:addRefCount(ref_tar,abName)
-- print("--------LoadPrefab-------",abName)
resMgr:LoadPrefab(abName, {pfName}, callBack, load_level)
-- print("---------loadPrefab from create -----",abName,pfName)
-- PrintCallStack()
end
end
--加载多个预设
function LuaResManager:loadPrefabs(ref_tar,abName,pfNameList,callBack, ignore_pool, load_level)
load_level = load_level or ASSETS_LEVEL.NORMAL
if not ref_tar or ref_tar._use_delete_method then return end
if not ignore_pool then
local obj_pool = self:GetObjListFormPool(ref_tar, abName, pfNameList)
if obj_pool then
if callBack then
callBack(obj_pool, true)
end
else
self:addRefCount(ref_tar,abName)
resMgr:LoadPrefab(abName, pfNameList, callBack, load_level)
end
else
self:addRefCount(ref_tar,abName)
resMgr:LoadPrefab(abName, pfNameList, callBack, load_level)
end
end
--加载一个对象
function LuaResManager:loadObject(ref_tar,abName,pfName,callBack, ignore_pool, load_level)
load_level = load_level or ASSETS_LEVEL.NORMAL
if not ref_tar or ref_tar._use_delete_method then return end
local reload_prefab = true
if not ignore_pool then
local obj_pool = self:GetObjFormPool(ref_tar, abName, pfName)
if obj_pool then
reload_prefab = false
if callBack then
callBack({[0] = obj_pool}, true)
end
end
end
if reload_prefab then
self:addRefCount(ref_tar,abName)
resMgr:LoadObject(abName, pfName, callBack, load_level)
end
end
--加载一个材质
function LuaResManager:loadMateaial(ref_tar,abName,pfName,callBack, load_level)
load_level = load_level or ASSETS_LEVEL.NORMAL
if not ref_tar or ref_tar._use_delete_method then return end
self:addRefCount(ref_tar,abName)
resMgr:LoadMaterial(abName, pfName, callBack, load_level)
end
--加载一个精灵
function LuaResManager:loadSprite(ref_tar,abName,pfName,callBack,load_level)
load_level = load_level or ASSETS_LEVEL.HIGHT
self:addRefCount(ref_tar,abName)
resMgr:LoadSprite(abName, pfName, callBack, load_level)
end
--加载多个精灵
function LuaResManager:loadSprites(ref_tar,abName,pfNameList,callBack,load_level)
load_level = load_level or ASSETS_LEVEL.HIGHT
self:addRefCount(ref_tar,abName)
resMgr:LoadSprites(abName, pfNameList, callBack, load_level)
end
--加载一张纹理
function LuaResManager:loadTexture(ref_tar,abName,pfNameList,callBack,load_level)
load_level = load_level or ASSETS_LEVEL.HIGHT
self:addRefCount(ref_tar,abName)
resMgr:LoadTexture(abName, pfNameList, callBack, load_level)
end
--加载二进制数据
function LuaResManager:loadTextAsset(ref_tar,abName,pfName,callBack,load_level)
load_level= load_level or ASSETS_LEVEL.NORMAL
self:addRefCount(ref_tar,abName)
resMgr:LoadTextAsset(abName, pfName, callBack,load_level)
end
--加载并播放声音
function LuaResManager:loadSound(ref_tar, abName, resName, is_loop,vol_modulus, speed)
local custom_speed = speed or 1
if abName ~= LuaSoundManager.SOUND_PRE[LuaSoundManager.SOUND_TYPE.SKILL] then
self:addRefCount(ref_tar, abName)
end
if tonumber(AppConst.EnglineVer) >= 89 then
return soundMgr:PlayEffect(abName, resName, is_loop,vol_modulus, custom_speed)
else
return soundMgr:PlayEffect(abName, resName, is_loop,vol_modulus)
end
end
--加载外部二进制文件,目前只有db加载使用,是常驻内存的,不需引用计数
function LuaResManager:loadOutsideTextAsset(ref_tar, respath, callBack)
outsideResMgr:LoadTextAsset(respath, callBack,OutSideFileType.BYTE)
end
--停止一个声音
function LuaResManager:stopSound(ref_tar, abName, effect_id)
if abName ~= LuaSoundManager.SOUND_PRE[LuaSoundManager.SOUND_TYPE.SKILL] then
-- self:reduceRefCount(ref_tar, abName)
end
if effect_id then
local res_id = tonumber(effect_id)
if res_id then
soundMgr:StopEffect(effect_id)
else
logWarn("stopSound error abName = " .. abName)
end
end
end
function LuaResManager:LoadPrefabView(ref_tar, abName, pfName, callBack)
local item = {
DeleteMe=LuaResManager.__DestroyPrefab,
is_loaded = false, gameObject = false, transform = false,
SetPosition = function(item, x, y)
if item.is_loaded then
SetLocalPosition(item.transform, x, y, 0)
else
item.cache_pos = {x=x, y=y}
end
end,
SetVisible = function(item, is_show)
if item.is_loaded then
item.gameObject:SetActive(is_show)
else
item.cache_visible = is_show
end
end,
SetData = function(item, i, v)
if item.is_loaded and item.UpdateView then
item:UpdateView(i, v)
else
item.cache_data = {index=i, data=v}
end
end,
AddUIComponent = UIPartical.AddUIComponent,
RemoveUIComponent = UIPartical.RemoveUIComponent,
}
local on_load_prefab = function ( prefab )
if not prefab or not prefab[0] then return end
if not ref_tar or ref_tar._use_delete_method or item._use_delete_method then return end
local go = newObject(prefab[0])
go.name = pfName
item.gameObject = go
item.transform = go.transform
item.is_loaded = true
UIPartical.BeforeLoad(item)
if callBack then
callBack(item)
end
if item.cache_visible ~= nil then
item.gameObject:SetActive(item.cache_visible)
item.cache_visible = nil
end
if item.cache_pos then
SetLocalPosition(item.transform, item.cache_pos.x, item.cache_pos.y, 0)
item.cache_pos = nil
end
if item.cache_data and item.UpdateView then
item:UpdateView(item.cache_data.index, item.cache_data.data)
item.cache_data = nil
end
end
if not IsUseLocalResource then
LuaResManager:getInstance():loadPrefab(ref_tar,abName,pfName, on_load_prefab, true, ASSETS_LEVEL.HIGHT)
else
--延迟到下一帧再加载,模拟异步加载
local function delay_method( )
local prefab = LuaFramework.PanelMgrEx.GetInstance():LoadPrefabInLocalByFile("Assets/LuaFramework/AssetBundleRes/ui/"..abName.."/prefab/"..pfName..".prefab")
on_load_prefab({[0]=prefab})
end
setTimeout(delay_method, 0.001)
end
return item
end
function LuaResManager.__DestroyPrefab( self )
--回收从LoadPrefabView用的,外部别调用本接口
if self.is_loaded and self.gameObject then
UIPartical.AfterDestroy(self)
if self.destroy_callback then
self.destroy_callback(self)
end
destroy(self.gameObject)
end
self._use_delete_method = true
end