源战役客户端
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

403 líneas
14 KiB

  1. --ui挂接模型处理, 单一模型展示,比如只显示武器,不显示人物模型 新新新新新新新新新新新新
  2. NewUIPartModelClass = NewUIPartModelClass or BaseClass(UIPartical)
  3. --当前正在使用的rendertexture列表
  4. 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)
  5. self.parent = parent
  6. self.particle_list = {}
  7. self.pos_x = 0
  8. self.custom_speed = 1
  9. self:SetData(parent, res_id, texture_id, modelPartPos, type,texture_size,scale, layer_name,position,action_name_list, action_delta,can_rotate,rotate)
  10. self:AddEvents()
  11. end
  12. function NewUIPartModelClass:__delete()
  13. self:ClearActionTimerList()
  14. self:ResetUIDepth()
  15. if self.clothe then
  16. self.clothe:DeleteMe()
  17. self.clothe = nil
  18. end
  19. if self.gameObject then
  20. destroy(self.gameObject)
  21. self.gameObject = nil
  22. end
  23. end
  24. --UIPartModelConfig 模型位置,大小等的配置,由策划自己去调整
  25. 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)
  26. -- local cfg = Config.UIPartModelConfig[res_id]
  27. -- local size = nil
  28. -- local rotate = nil
  29. -- local can_rotate = nil
  30. -- if cfg then
  31. -- size = cfg.size
  32. -- rotate = cfg.rotate
  33. -- can_rotate = cfg.can_rotate
  34. -- position = cfg.position or position
  35. -- scale = cfg.scale or scale
  36. -- end
  37. self.layer_name = layer_name or "UI"
  38. self.res_id = res_id
  39. self.texture_id = texture_id
  40. self.type = role_type or nil
  41. self.size = texture_size or size or {x = 600,y = 360}
  42. self.rotate = rotate or 0
  43. self.can_rotate = can_rotate == nil or can_rotate == true
  44. self.position = position
  45. -- action_name_list = {"show"}
  46. -- action_delta = 0
  47. self.action_name_list = action_name_list or {"show"}
  48. self.action_delta = action_delta or 0
  49. print("=====action_delta",action_delta)
  50. local role_con = parent
  51. local anName = ModelPartName[role_type] .. res_id
  52. local resName = ModelPartName[role_type] .. res_id
  53. if modelPartPos == FashionPartPos.Weapon then
  54. anName = "model_weapon_" .. res_id
  55. resName = ModelPartName[role_type] .. res_id
  56. else
  57. end
  58. local function loadRoleCallback(objs)
  59. if self._use_delete_method then
  60. return
  61. end
  62. if not objs or not objs[0] then
  63. return
  64. end
  65. if self.gameObject then
  66. destroy(self.gameObject)
  67. end
  68. self.gameObject = newObject(objs[0])
  69. self.transform = self.gameObject.transform
  70. local custom_speed = Config.ConfigModelSpeed.ModelActionSpeed[tonumber(res_id)]
  71. if custom_speed then
  72. local animator = self.gameObject:GetComponent("Animator")
  73. if animator then
  74. animator.speed = custom_speed
  75. self.custom_speed = custom_speed
  76. end
  77. end
  78. self:CheckMat(self.gameObject)
  79. self.gameObject:GetComponent("Animator").cullingMode = UnityEngine.AnimatorCullingMode.AlwaysAnimate
  80. self.transform:SetParent(role_con)
  81. self.transform.localRotation = Quaternion.identity
  82. self:SetUILayer(self.transform)
  83. self:SetParticleDepth(self.gameObject)
  84. if modelPartPos == FashionPartPos.Weapon then
  85. --加载武器配置特效
  86. local weapon_pvo = {} --Config.SceneObjectParticle.Weapon[res_id]
  87. if weapon_pvo and weapon_pvo.always then
  88. for bone_name, particle_vo in pairs(weapon_pvo.always) do
  89. local bone = FindBone(self.transform, bone_name)
  90. if bone then
  91. local function load_finish_func(go)
  92. if go then
  93. self:SetUILayer(go.transform)
  94. go.transform.localScale = Vector3.one
  95. go.transform.localRotation = Vector3.zero
  96. self:CheckMat(go)
  97. end
  98. end
  99. self:AddUIEffect(particle_vo.name, bone, self.layer_name, nil, -1, false, -1, nil, nil,load_finish_func, nil, nil, true)
  100. end
  101. end
  102. end
  103. else
  104. local body_pvo = {} --Config.SceneObjectParticle.Body[res_id]
  105. if body_pvo and body_pvo.always then
  106. for bone_name, particle_vo in pairs(body_pvo.always) do
  107. local bone = FindBone(self.transform, bone_name)
  108. if bone then
  109. local function load_finish_func(go)
  110. if go then
  111. self:SetUILayer(go.transform)
  112. go.transform.localScale = Vector3.one
  113. go.transform.localRotation = Quaternion.Euler(Vector3.zero)
  114. self:CheckMat(go)
  115. end
  116. end
  117. self:AddUIEffect(particle_vo.name, bone, self.layer_name, nil, -1, false, -1, nil, nil,load_finish_func, nil, nil, true)
  118. end
  119. end
  120. end
  121. end
  122. local z = 0
  123. if self.layer_name and self.layer_name == "Main" then
  124. z = UIZDepth.STEP_VALUE
  125. end
  126. if position then
  127. SetLocalPosition(self.transform, position.x, position.y,UIZDepth.MODEL_Z_VALUE + z)
  128. -- self.transform.localPosition = Vector3(position.x, position.y,-UIZDepth.STEP_VALUE + 300 + z)
  129. else
  130. SetLocalPosition(self.transform, 0, 0,UIZDepth.MODEL_Z_VALUE + z)
  131. -- self.transform.localPosition = Vector3(0,0,-UIZDepth.STEP_VALUE + 300 + z)
  132. end
  133. if scale then
  134. self.transform.localScale = Vector3(scale,scale,scale)
  135. else
  136. self.transform.localScale = Vector3(200,200,200)
  137. end
  138. local rotate_type = type(rotate)
  139. if rotate_type == "number" then
  140. self.transform:Rotate(Vector3(0, 180 + (rotate), 0))
  141. self.rotate = rotate
  142. elseif rotate_type == "table" then
  143. --self.transform:Rotate(Vector3(rotate.x, rotate.y, rotate.z))
  144. self.transform.localRotation = Quaternion.Euler(rotate.x, rotate.y, rotate.z)
  145. self.rotate = rotate.y
  146. else
  147. self.transform:Rotate(Vector3(0, 180, 0))
  148. self.rotate = 0
  149. end
  150. -- self.transform:Rotate(Vector3(0, 180 + (self.rotate), 0))
  151. -- local animator = self.gameObject:GetComponent("Animator")
  152. -- if animator then
  153. -- animator:CrossFade( "idle",0, 0.0 )
  154. -- animator:CrossFade( "show",0, 0.0 )
  155. -- print("============NewUIPartModelClass CrossFade show ")
  156. -- end
  157. if texture_id then
  158. self:CreateClothTexture(res_id, texture_id)
  159. end
  160. if not IsTableEmpty(action_name_list) then
  161. local action_name = action_delta and action_name_list[1] or table.remove(action_name_list, 1)
  162. if action_name then
  163. local animator = self.gameObject:GetComponent("Animator")
  164. if string.find(action_name, "casual") then
  165. animator.speed = 1
  166. else
  167. animator.speed = self.custom_speed
  168. end
  169. animator:CrossFade( action_name,0, 0.0 )
  170. if not IsTableEmpty(action_name_list) then
  171. self.delay_play_action_timer_list = self.delay_play_action_timer_list or {}
  172. if action_delta then
  173. self:DelayPlayNextActionLoop(animator, action_name_list, action_delta)
  174. else
  175. self:DelayPlayNextAction(animator, action_name_list)
  176. end
  177. end
  178. end
  179. else
  180. local animator = self.gameObject:GetComponent("Animator")
  181. if animator then
  182. animator:CrossFade( "show",0, 0.0 )
  183. end
  184. end
  185. end
  186. parent.sizeDelta = Vector2(self.size.x, self.size.y)
  187. LuaResManager:getInstance():loadPrefab(self,anName,resName, loadRoleCallback, true)
  188. end
  189. function NewUIPartModelClass:ClearActionTimerList()
  190. if self.delay_play_action_timer_list then
  191. for i, id in ipairs(self.delay_play_action_timer_list) do
  192. TimerQuest.CancelQuest(GlobalTimerQuest, id)
  193. end
  194. self.delay_play_action_timer_list = {}
  195. end
  196. end
  197. --action_name_list.idle_name 默认待机动作
  198. function NewUIPartModelClass:DelayPlayNextActionLoop(animator, action_name_list, action_delta)
  199. if IsTableEmpty(action_name_list) or animator == nil or not self.gameObject.activeInHierarchy then return end
  200. animator:Update(0)
  201. local cur_state = animator:GetCurrentAnimatorStateInfo(0)
  202. local action_time = 0
  203. if not cur_state then return end
  204. action_time = cur_state.length
  205. local idle_action = action_name_list.idle_name or "idle2"
  206. local delay_play = function ()
  207. if animator then
  208. local action_name = action_name_list[ math.random(1,#action_name_list)]
  209. if string.find(action_name, "casual") then
  210. animator.speed = 1
  211. else
  212. animator.speed = self.custom_speed
  213. end
  214. animator:CrossFade(action_name, 0.2, 0.0)
  215. self:DelayPlayNextActionLoop(animator, action_name_list, action_delta)
  216. end
  217. end
  218. local play_idle = function ()
  219. if not IsNull(animator) then
  220. if string.find(idle_action, "casual") then
  221. animator.speed = 1
  222. else
  223. animator.speed = self.custom_speed
  224. end
  225. animator:CrossFade(idle_action, 0.3, 0.0 )
  226. end
  227. end
  228. --清理计时器列表
  229. if #self.delay_play_action_timer_list >= 20 then
  230. self:ClearActionTimerList()
  231. end
  232. action_delta = action_delta < action_time and action_time + 1 or action_delta
  233. table.insert(self.delay_play_action_timer_list, TimerQuest.AddDelayQuest(GlobalTimerQuest, play_idle,action_time))
  234. table.insert(self.delay_play_action_timer_list, TimerQuest.AddDelayQuest(GlobalTimerQuest, delay_play,action_delta))
  235. end
  236. --延迟播放下一个动作
  237. function NewUIPartModelClass:DelayPlayNextAction(animator, action_name_list)
  238. if not IsTableEmpty(action_name_list) then
  239. animator:Update(0)
  240. local cur_state = animator:GetCurrentAnimatorStateInfo(0)
  241. local action_time = 0
  242. if cur_state then
  243. action_time = cur_state.length
  244. local delay_play = function ()
  245. local action_name = table.remove(action_name_list, 1)
  246. if action_name then
  247. if string.find(action_name, "casual") then
  248. animator.speed = 1
  249. else
  250. animator.speed = self.custom_speed
  251. end
  252. animator:CrossFade(action_name,0, 0.0 )
  253. if not IsTableEmpty(action_name_list) then
  254. self:DelayPlayNextAction(animator, action_name_list)
  255. end
  256. else
  257. return
  258. end
  259. end
  260. table.insert(self.delay_play_action_timer_list, TimerQuest.AddDelayQuest(GlobalTimerQuest, delay_play,action_time))
  261. else
  262. return
  263. end
  264. end
  265. return
  266. end
  267. --贴图
  268. function NewUIPartModelClass:CreateClothTexture(fashion_model_id, texture_id)
  269. self.fashion_model_id = fashion_model_id
  270. self.texture_id = texture_id
  271. if fashion_model_id and texture_id and tonumber(texture_id) ~= 0 and self.gameObject then --使用贴图
  272. if self.clothe then
  273. self.clothe:DeleteMe()
  274. self.clothe = nil
  275. end
  276. local texture_res = fashion_model_id .. "_" .. texture_id
  277. self.skin_mesh_renderer = self.gameObject:GetComponentInChildren(typeof(UnityEngine.SkinnedMeshRenderer))
  278. if self.skin_mesh_renderer and self.skin_mesh_renderer.material and texture_res then
  279. self.clothe = Clothe.New(self.skin_mesh_renderer.material,texture_res)
  280. end
  281. end
  282. end
  283. function NewUIPartModelClass:AddEvents()
  284. SetAlpha(self.parent:GetComponent("RawImage"),0)
  285. if self.can_rotate then
  286. local function touch_begin(target,pos_x, pos_y)
  287. self.pos_x = pos_x
  288. end
  289. local function draging(target,pos_x, pos_y)
  290. self:OnDragging(pos_x, pos_y)
  291. end
  292. local function touch_end(target,pos_x, pos_y)
  293. end
  294. local rtGo = self.parent.gameObject
  295. -- AddDownEvent(self.parent.gameObject, touch_begin)
  296. self.parent:GetComponent("RawImage").raycastTarget = true
  297. AddDragBeginEvent(rtGo, touch_begin)
  298. AddDragEvent(rtGo, draging)
  299. AddDragEndEvent(rtGo, touch_end)
  300. -- AddUpEvent(self.parent.gameObject, touch_end)
  301. else
  302. self.parent:GetComponent("RawImage").raycastTarget = false
  303. end
  304. end
  305. function NewUIPartModelClass:OnDragging(pos_x, pos_y)
  306. if pos_x and self.pos_x then
  307. local delta_x = pos_x - self.pos_x
  308. if self.gameObject then
  309. if delta_x > 0 then
  310. self.rotate = self.rotate - 10
  311. -- self.transform:Rotate(Vector3(0, -10, 0))
  312. self.transform:Rotate(Vector3.up, -10, UnityEngine.Space.World)
  313. else
  314. self.rotate = self.rotate + 10
  315. -- self.transform:Rotate(Vector3(0, 10, 0))
  316. self.transform:Rotate(Vector3.up, 10, UnityEngine.Space.World)
  317. end
  318. self.pos_x = pos_x
  319. end
  320. else
  321. print("---NewUIPartModelClass--OnDragging-pos_x-:", pos_x)
  322. print("---NewUIPartModelClass--OnDragging-pos_y-:", pos_y)
  323. end
  324. end
  325. function NewUIPartModelClass:PlayIdelAnim()
  326. if self.gameObject then
  327. local animator = self.gameObject:GetComponent("Animator")
  328. if animator then
  329. animator.speed = self.custom_speed
  330. animator:CrossFade( "idle2",0, 0.0 )
  331. end
  332. end
  333. end
  334. function NewUIPartModelClass:PlayShowAnim()
  335. if self.gameObject then
  336. local animator = self.gameObject:GetComponent("Animator")
  337. if animator then
  338. animator:CrossFade( "show",0, 0.0 )
  339. end
  340. end
  341. end
  342. function NewUIPartModelClass:CheckMat(gameObject)
  343. local check_func = function(renders)
  344. if renders then
  345. for j = 0, renders.Length - 1 do
  346. local mesh = renders[j]
  347. local materials = mesh.materials
  348. if materials.Length > 0 then
  349. for j = 0, materials.Length - 1 do
  350. local material = materials[j]
  351. if material.shader.name == "Custom/mb_flowalphablendmask_xray" then
  352. material.shader = ShaderTools.GetShader( "mb_flowalphablendmaskui" )
  353. elseif material.shader.name == "ApcShader/XRayEffect" then
  354. material.shader = SceneManager.Instance:GetTextureShader()
  355. elseif material.shader.name == "Mobile/Particles/Additive" then
  356. material.shader = ShaderTools.GetShader( "MobileParticleAddNoFog" )
  357. elseif material.shader.name == "Mobile/Particles/Alpha Blended" then
  358. material.shader = ShaderTools.GetShader( "MobileParticleAlphaNoFog" )
  359. elseif material.shader.name == "Particles/Alpha Blended" then
  360. material.shader = ShaderTools.GetShader( "ParticleAlphaBlendNoFog" )
  361. elseif material.shader.name == "Particles/Additive" then
  362. material.shader = ShaderTools.GetShader( "Particle Add_NoFog" )
  363. end
  364. end
  365. end
  366. end
  367. end
  368. end
  369. local renders = gameObject:GetComponentsInChildren(typeof(UnityEngine.SkinnedMeshRenderer))
  370. check_func(renders)
  371. renders = gameObject:GetComponentsInChildren(typeof(UnityEngine.MeshRenderer))
  372. check_func(renders)
  373. renders = gameObject:GetComponentsInChildren(typeof(UnityEngine.ParticleSystemRenderer))
  374. check_func(renders)
  375. end