--ui挂接模型处理, 单一模型展示,比如只显示武器,不显示人物模型 新新新新新新新新新新新新 NewUIPartModelClass = NewUIPartModelClass or BaseClass(UIPartical) --当前正在使用的rendertexture列表 function NewUIPartModelClass:__init(parent, res_id, texture_id, modelPartPos,type,raycastParent,texture_size,scale, layer_name,position, action_name_list, action_delta,can_rotate,rotate) self.parent = parent self.particle_list = {} self.pos_x = 0 self.custom_speed = 1 self:SetData(parent, res_id, texture_id, modelPartPos, type,texture_size,scale, layer_name,position,action_name_list, action_delta,can_rotate,rotate) self:AddEvents() end function NewUIPartModelClass:__delete() self:ClearActionTimerList() self:ResetUIDepth() if self.clothe then self.clothe:DeleteMe() self.clothe = nil end if self.gameObject then destroy(self.gameObject) self.gameObject = nil end end --UIPartModelConfig 模型位置,大小等的配置,由策划自己去调整 function NewUIPartModelClass:SetData(parent, res_id, texture_id, modelPartPos, role_type, texture_size,scale, layer_name,position, action_name_list, action_delta,can_rotate,rotate) -- local cfg = Config.UIPartModelConfig[res_id] -- local size = nil -- local rotate = nil -- local can_rotate = nil -- if cfg then -- size = cfg.size -- rotate = cfg.rotate -- can_rotate = cfg.can_rotate -- position = cfg.position or position -- scale = cfg.scale or scale -- end self.layer_name = layer_name or "UI" self.res_id = res_id self.texture_id = texture_id self.type = role_type or nil self.size = texture_size or size or {x = 600,y = 360} self.rotate = rotate or 0 self.can_rotate = can_rotate == nil or can_rotate == true self.position = position -- action_name_list = {"show"} -- action_delta = 0 self.action_name_list = action_name_list or {"show"} self.action_delta = action_delta or 0 print("=====action_delta",action_delta) local role_con = parent local anName = ModelPartName[role_type] .. res_id local resName = ModelPartName[role_type] .. res_id if modelPartPos == FashionPartPos.Weapon then anName = "model_weapon_" .. res_id resName = ModelPartName[role_type] .. res_id else end local function loadRoleCallback(objs) if self._use_delete_method then return end if not objs or not objs[0] then return end if self.gameObject then destroy(self.gameObject) end self.gameObject = newObject(objs[0]) self.transform = self.gameObject.transform local custom_speed = Config.ConfigModelSpeed.ModelActionSpeed[tonumber(res_id)] if custom_speed then local animator = self.gameObject:GetComponent("Animator") if animator then animator.speed = custom_speed self.custom_speed = custom_speed end end self:CheckMat(self.gameObject) self.gameObject:GetComponent("Animator").cullingMode = UnityEngine.AnimatorCullingMode.AlwaysAnimate self.transform:SetParent(role_con) self.transform.localRotation = Quaternion.identity self:SetUILayer(self.transform) self:SetParticleDepth(self.gameObject) if modelPartPos == FashionPartPos.Weapon then --加载武器配置特效 local weapon_pvo = {} --Config.SceneObjectParticle.Weapon[res_id] if weapon_pvo and weapon_pvo.always then for bone_name, particle_vo in pairs(weapon_pvo.always) do local bone = FindBone(self.transform, bone_name) if bone then local function load_finish_func(go) if go then self:SetUILayer(go.transform) go.transform.localScale = Vector3.one go.transform.localRotation = Vector3.zero self:CheckMat(go) end end self:AddUIEffect(particle_vo.name, bone, self.layer_name, nil, -1, false, -1, nil, nil,load_finish_func, nil, nil, true) end end end else local body_pvo = {} --Config.SceneObjectParticle.Body[res_id] if body_pvo and body_pvo.always then for bone_name, particle_vo in pairs(body_pvo.always) do local bone = FindBone(self.transform, bone_name) if bone then local function load_finish_func(go) if go then self:SetUILayer(go.transform) go.transform.localScale = Vector3.one go.transform.localRotation = Quaternion.Euler(Vector3.zero) self:CheckMat(go) end end self:AddUIEffect(particle_vo.name, bone, self.layer_name, nil, -1, false, -1, nil, nil,load_finish_func, nil, nil, true) end end end end local z = 0 if self.layer_name and self.layer_name == "Main" then z = UIZDepth.STEP_VALUE end if position then SetLocalPosition(self.transform, position.x, position.y,UIZDepth.MODEL_Z_VALUE + z) -- self.transform.localPosition = Vector3(position.x, position.y,-UIZDepth.STEP_VALUE + 300 + z) else SetLocalPosition(self.transform, 0, 0,UIZDepth.MODEL_Z_VALUE + z) -- self.transform.localPosition = Vector3(0,0,-UIZDepth.STEP_VALUE + 300 + z) end if scale then self.transform.localScale = Vector3(scale,scale,scale) else self.transform.localScale = Vector3(200,200,200) end local rotate_type = type(rotate) if rotate_type == "number" then self.transform:Rotate(Vector3(0, 180 + (rotate), 0)) self.rotate = rotate elseif rotate_type == "table" then --self.transform:Rotate(Vector3(rotate.x, rotate.y, rotate.z)) self.transform.localRotation = Quaternion.Euler(rotate.x, rotate.y, rotate.z) self.rotate = rotate.y else self.transform:Rotate(Vector3(0, 180, 0)) self.rotate = 0 end -- self.transform:Rotate(Vector3(0, 180 + (self.rotate), 0)) -- local animator = self.gameObject:GetComponent("Animator") -- if animator then -- animator:CrossFade( "idle",0, 0.0 ) -- animator:CrossFade( "show",0, 0.0 ) -- print("============NewUIPartModelClass CrossFade show ") -- end if texture_id then self:CreateClothTexture(res_id, texture_id) end if not IsTableEmpty(action_name_list) then local action_name = action_delta and action_name_list[1] or table.remove(action_name_list, 1) if action_name then local animator = self.gameObject:GetComponent("Animator") if string.find(action_name, "casual") then animator.speed = 1 else animator.speed = self.custom_speed end animator:CrossFade( action_name,0, 0.0 ) if not IsTableEmpty(action_name_list) then self.delay_play_action_timer_list = self.delay_play_action_timer_list or {} if action_delta then self:DelayPlayNextActionLoop(animator, action_name_list, action_delta) else self:DelayPlayNextAction(animator, action_name_list) end end end else local animator = self.gameObject:GetComponent("Animator") if animator then animator:CrossFade( "show",0, 0.0 ) end end end parent.sizeDelta = Vector2(self.size.x, self.size.y) LuaResManager:getInstance():loadPrefab(self,anName,resName, loadRoleCallback, true) end function NewUIPartModelClass:ClearActionTimerList() if self.delay_play_action_timer_list then for i, id in ipairs(self.delay_play_action_timer_list) do TimerQuest.CancelQuest(GlobalTimerQuest, id) end self.delay_play_action_timer_list = {} end end --action_name_list.idle_name 默认待机动作 function NewUIPartModelClass:DelayPlayNextActionLoop(animator, action_name_list, action_delta) if IsTableEmpty(action_name_list) or animator == nil or not self.gameObject.activeInHierarchy then return end animator:Update(0) local cur_state = animator:GetCurrentAnimatorStateInfo(0) local action_time = 0 if not cur_state then return end action_time = cur_state.length local idle_action = action_name_list.idle_name or "idle2" local delay_play = function () if animator then local action_name = action_name_list[ math.random(1,#action_name_list)] if string.find(action_name, "casual") then animator.speed = 1 else animator.speed = self.custom_speed end animator:CrossFade(action_name, 0.2, 0.0) self:DelayPlayNextActionLoop(animator, action_name_list, action_delta) end end local play_idle = function () if not IsNull(animator) then if string.find(idle_action, "casual") then animator.speed = 1 else animator.speed = self.custom_speed end animator:CrossFade(idle_action, 0.3, 0.0 ) end end --清理计时器列表 if #self.delay_play_action_timer_list >= 20 then self:ClearActionTimerList() end action_delta = action_delta < action_time and action_time + 1 or action_delta table.insert(self.delay_play_action_timer_list, TimerQuest.AddDelayQuest(GlobalTimerQuest, play_idle,action_time)) table.insert(self.delay_play_action_timer_list, TimerQuest.AddDelayQuest(GlobalTimerQuest, delay_play,action_delta)) end --延迟播放下一个动作 function NewUIPartModelClass:DelayPlayNextAction(animator, action_name_list) if not IsTableEmpty(action_name_list) then animator:Update(0) local cur_state = animator:GetCurrentAnimatorStateInfo(0) local action_time = 0 if cur_state then action_time = cur_state.length local delay_play = function () local action_name = table.remove(action_name_list, 1) if action_name then if string.find(action_name, "casual") then animator.speed = 1 else animator.speed = self.custom_speed end animator:CrossFade(action_name,0, 0.0 ) if not IsTableEmpty(action_name_list) then self:DelayPlayNextAction(animator, action_name_list) end else return end end table.insert(self.delay_play_action_timer_list, TimerQuest.AddDelayQuest(GlobalTimerQuest, delay_play,action_time)) else return end end return end --贴图 function NewUIPartModelClass:CreateClothTexture(fashion_model_id, texture_id) self.fashion_model_id = fashion_model_id self.texture_id = texture_id if fashion_model_id and texture_id and tonumber(texture_id) ~= 0 and self.gameObject then --使用贴图 if self.clothe then self.clothe:DeleteMe() self.clothe = nil end local texture_res = fashion_model_id .. "_" .. texture_id self.skin_mesh_renderer = self.gameObject:GetComponentInChildren(typeof(UnityEngine.SkinnedMeshRenderer)) if self.skin_mesh_renderer and self.skin_mesh_renderer.material and texture_res then self.clothe = Clothe.New(self.skin_mesh_renderer.material,texture_res) end end end function NewUIPartModelClass:AddEvents() SetAlpha(self.parent:GetComponent("RawImage"),0) if self.can_rotate then local function touch_begin(target,pos_x, pos_y) self.pos_x = pos_x end local function draging(target,pos_x, pos_y) self:OnDragging(pos_x, pos_y) end local function touch_end(target,pos_x, pos_y) end local rtGo = self.parent.gameObject -- AddDownEvent(self.parent.gameObject, touch_begin) self.parent:GetComponent("RawImage").raycastTarget = true AddDragBeginEvent(rtGo, touch_begin) AddDragEvent(rtGo, draging) AddDragEndEvent(rtGo, touch_end) -- AddUpEvent(self.parent.gameObject, touch_end) else self.parent:GetComponent("RawImage").raycastTarget = false end end function NewUIPartModelClass:OnDragging(pos_x, pos_y) if pos_x and self.pos_x then local delta_x = pos_x - self.pos_x if self.gameObject then if delta_x > 0 then self.rotate = self.rotate - 10 -- self.transform:Rotate(Vector3(0, -10, 0)) self.transform:Rotate(Vector3.up, -10, UnityEngine.Space.World) else self.rotate = self.rotate + 10 -- self.transform:Rotate(Vector3(0, 10, 0)) self.transform:Rotate(Vector3.up, 10, UnityEngine.Space.World) end self.pos_x = pos_x end else print("---NewUIPartModelClass--OnDragging-pos_x-:", pos_x) print("---NewUIPartModelClass--OnDragging-pos_y-:", pos_y) end end function NewUIPartModelClass:PlayIdelAnim() if self.gameObject then local animator = self.gameObject:GetComponent("Animator") if animator then animator.speed = self.custom_speed animator:CrossFade( "idle2",0, 0.0 ) end end end function NewUIPartModelClass:PlayShowAnim() if self.gameObject then local animator = self.gameObject:GetComponent("Animator") if animator then animator:CrossFade( "show",0, 0.0 ) end end end function NewUIPartModelClass:CheckMat(gameObject) local check_func = function(renders) if renders then for j = 0, renders.Length - 1 do local mesh = renders[j] local materials = mesh.materials if materials.Length > 0 then for j = 0, materials.Length - 1 do local material = materials[j] if material.shader.name == "Custom/mb_flowalphablendmask_xray" then material.shader = ShaderTools.GetShader( "mb_flowalphablendmaskui" ) elseif material.shader.name == "ApcShader/XRayEffect" then material.shader = SceneManager.Instance:GetTextureShader() elseif material.shader.name == "Mobile/Particles/Additive" then material.shader = ShaderTools.GetShader( "MobileParticleAddNoFog" ) elseif material.shader.name == "Mobile/Particles/Alpha Blended" then material.shader = ShaderTools.GetShader( "MobileParticleAlphaNoFog" ) elseif material.shader.name == "Particles/Alpha Blended" then material.shader = ShaderTools.GetShader( "ParticleAlphaBlendNoFog" ) elseif material.shader.name == "Particles/Additive" then material.shader = ShaderTools.GetShader( "Particle Add_NoFog" ) end end end end end end local renders = gameObject:GetComponentsInChildren(typeof(UnityEngine.SkinnedMeshRenderer)) check_func(renders) renders = gameObject:GetComponentsInChildren(typeof(UnityEngine.MeshRenderer)) check_func(renders) renders = gameObject:GetComponentsInChildren(typeof(UnityEngine.ParticleSystemRenderer)) check_func(renders) end