RoleViewSystem.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. --[[
  2. @ 所有的skinDatas格式如下
  3. skinDatas =
  4. {
  5. [Enum.SkinSlotType.HeadTop] = {prefabGo = GameObject, customBindBone = String},
  6. [Enum.SkinSlotType.HeadMiddle] = {prefabGo = GameObject, customBindBone = String},
  7. [Enum.SkinSlotType.HeadBottom] = {prefabGo = GameObject, customBindBone = String},
  8. [Enum.SkinSlotType.Cloth] = {prefabGo = GameObject, customBindBone = String},
  9. [Enum.SkinSlotType.LeftHand] = {prefabGo = GameObject, customBindBone = String},
  10. [Enum.SkinSlotType.RightHand] = {prefabGo = GameObject, customBindBone = String},
  11. [Enum.SkinSlotType.BodyBack] = {prefabGo = GameObject, customBindBone = String},
  12. [Enum.SkinSlotType.HairStyle] = {prefabGo = GameObject, customBindBone = String}
  13. [Enum.SkinSlotType.Pupil] = {prefabGo = GameObject, customBindBone = String},
  14. [Enum.SkinSlotType.HairColor] = {uvOffset = Vector2},
  15. [Enum.SkinSlotType.Face] = {prefabGo = GameObject},
  16. }
  17. prefabGo 为 prefab资源
  18. customBindBone 为自定义绑定骨骼,可为nil, 不为nil是会覆盖默认绑定骨骼节点
  19. @ 英雄 需要在预览和战斗中显示的 内容
  20. viewDatas = {
  21. [Enum.HeroExtGoesShowType.God_Art] = {prefabGo = GameObject, prefabPath = String}
  22. }
  23. ]]
  24. local RoleViewSystem = class("RoleViewSystem")
  25. local SkinSystem = require("SkinSystem")
  26. local ViewSystem = require("ViewSystem")
  27. local MultiTypeAssetLoadSystem = require("MultiTypeAssetLoadSystem")
  28. local AnimatorType = typeof(UnityEngine.Animator)
  29. local AnimControlSkinType = typeof(AnimControlSkin)
  30. local RoleCfgKey = {
  31. [Enum.FashionSlotType.HeadTop] = "HeadTop",
  32. [Enum.FashionSlotType.HeadMiddle] = "HeadMiddle",
  33. [Enum.FashionSlotType.HeadBottom] = "HeadBottom",
  34. [Enum.FashionSlotType.Cloth] = "ClothId",
  35. [Enum.FashionSlotType.BodyBack] = "BodyBackId",
  36. [Enum.FashionSlotType.Weapon] = "WeaponId",
  37. [Enum.FashionSlotType.HairStyle] = "Hair",
  38. [Enum.FashionSlotType.Pupil] = "Eye",
  39. }
  40. local function CombineDiff(map, key, value, diffMap)
  41. if not key then
  42. return false
  43. end
  44. if not map or map[key] ~= value then
  45. diffMap[key] = value
  46. return true
  47. end
  48. return false
  49. end
  50. local function SkinsDiff(roleCfgId, oldSkins, viewData)
  51. local changed = false
  52. local diffSkins = {}
  53. local cfgMgr = ManagerContainer.CfgMgr
  54. local roleCfg = cfgMgr:GetRoleDataById(roleCfgId)
  55. if not roleCfg and not viewData then
  56. return changed, diffSkins
  57. end
  58. local getFashionById = cfgMgr.GetFashionById
  59. local fashionViewData = viewData and viewData.fashionData or nil
  60. local FashionSlotType = Enum.FashionSlotType
  61. local SkinSlotType = Enum.SkinSlotType
  62. --- 找是否有互斥的数据 (目前是头发)
  63. local isHideHair = false
  64. if fashionViewData then
  65. local fashionId = fashionViewData[FashionSlotType.HeadTop]
  66. if fashionId then
  67. local fashionData = getFashionById(cfgMgr, fashionId)
  68. if fashionData then
  69. isHideHair = fashionData.FashionHideHair
  70. end
  71. end
  72. end
  73. local avatarId, skinSlotType, fashionData
  74. skinSlotType = SkinSlotType.Face
  75. if roleCfg.Sex == Enum.SexType.Man then
  76. avatarId = GlobalConfig.Instance:GetConfigIntValue(GlobalConfig.c_man_face_model_path)
  77. else
  78. avatarId = GlobalConfig.Instance:GetConfigIntValue(GlobalConfig.c_woman_face_model_path)
  79. end
  80. changed = CombineDiff(oldSkins, skinSlotType, avatarId, diffSkins)
  81. for _, fashionSlotType in pairs(FashionSlotType) do
  82. avatarId = 0
  83. fashionData = nil
  84. if fashionViewData then
  85. local fashionId = fashionViewData[fashionSlotType]
  86. if fashionId then
  87. fashionData = getFashionById(cfgMgr, fashionId)
  88. end
  89. end
  90. if fashionSlotType == FashionSlotType.Weapon then
  91. if roleCfg.LeftWeapon and roleCfg.LeftWeapon ~= 0 then
  92. if fashionData then avatarId = fashionData.RoleAvatarID end
  93. skinSlotType = SkinSlotType.LeftHand
  94. avatarId = ((avatarId and avatarId ~= 0 ) and avatarId or roleCfg.LeftWeapon)
  95. if not avatarId then avatarId = 0 end
  96. if CombineDiff(oldSkins, skinSlotType, avatarId, diffSkins) then
  97. changed = true
  98. end
  99. end
  100. avatarId = 0
  101. if roleCfg.RightWeapon and roleCfg.RightWeapon ~= 0 then
  102. if fashionData then avatarId = fashionData.ExtRoleAvatarID end
  103. skinSlotType = SkinSlotType.RightHand
  104. avatarId = ((avatarId and avatarId ~= 0 ) and avatarId or roleCfg.RightWeapon)
  105. if not avatarId then avatarId = 0 end
  106. if CombineDiff(oldSkins, skinSlotType, avatarId, diffSkins) then
  107. changed = true
  108. end
  109. end
  110. else
  111. if fashionData then
  112. avatarId = fashionData.ExtRoleAvatarID
  113. if not avatarId or avatarId == 0 then
  114. avatarId = fashionData.RoleAvatarID
  115. end
  116. end
  117. -- 去默认的配置中找
  118. if not avatarId or avatarId == 0 then
  119. local key = RoleCfgKey[fashionSlotType]
  120. if key then
  121. avatarId = roleCfg[key]
  122. end
  123. end
  124. skinSlotType = Enum.FashionSlotToSkinSlot[fashionSlotType]
  125. if skinSlotType == SkinSlotType.HairStyle and isHideHair then
  126. avatarId = 0
  127. end
  128. if not avatarId then avatarId = 0 end
  129. if CombineDiff(oldSkins, skinSlotType, avatarId, diffSkins) then
  130. changed = true
  131. end
  132. end
  133. end
  134. return changed, diffSkins
  135. end
  136. local function GetAnimatorControllerPath(roleCfgId, roleInEnvType)
  137. local roleCfg = ManagerContainer.CfgMgr:GetRoleDataById(roleCfgId)
  138. if not roleCfg then
  139. roleCfg = ManagerContainer.CfgMgr:GetPartnerDataById(roleCfgId)
  140. end
  141. if Enum.RoleInEnvType.CreateRole == roleInEnvType or Enum.RoleInEnvType.FashionView == roleInEnvType then
  142. return Constants.AnimatorPath, roleCfg.CreateShowCtrl
  143. -- elseif Enum.RoleInEnvType.GuildLobby == roleInEnvType then
  144. -- return Constants.AnimatorPath, roleCfg.CityCtrl
  145. elseif Enum.RoleInEnvType.RoleMainView == roleInEnvType then
  146. return Constants.AnimatorPath, roleCfg.RoleShowCtrl or roleCfg.ShowCtrl
  147. elseif Enum.RoleInEnvType.Transfer == roleInEnvType then
  148. return Constants.AnimatorPath, roleCfg.TransferShowCtrl
  149. else
  150. return Constants.AnimatorPath, roleCfg.BattleCtrl
  151. end
  152. end
  153. local function GetSkinDatas(oldSkins, updateDiffSkins)
  154. local skinDatas = nil
  155. if updateDiffSkins then
  156. skinDatas = {}
  157. local cfgMgr = ManagerContainer.CfgMgr
  158. local getRoleAvatarById = cfgMgr.GetRoleAvatarById
  159. local hasValue, prefabGo, avatarPrefab, pos, rot, scale
  160. for skinSlotType,avatarId in pairs(updateDiffSkins) do
  161. hasValue = false
  162. prefabGo = nil
  163. pos = nil
  164. rot = nil
  165. scale = nil
  166. if avatarId and avatarId > 0 then
  167. hasValue = true
  168. local roleAvatarCfg = getRoleAvatarById(cfgMgr, avatarId)
  169. if roleAvatarCfg then
  170. avatarPrefab = roleAvatarCfg.AvatarPrefab
  171. if avatarPrefab and avatarPrefab ~= '' then
  172. prefabGo = ManagerContainer.ResMgr:GetAsset(Constants.ModelPath, avatarPrefab)
  173. end
  174. pos = CommonUtil.TableToVector3(roleAvatarCfg.ModelPosition)
  175. rot = CommonUtil.TableToQuaternion(roleAvatarCfg.ModelRotation)
  176. scale = CommonUtil.TableToVector3(roleAvatarCfg.ModelScale, Vector3.one)
  177. end
  178. end
  179. skinDatas[skinSlotType] = {hasValue = hasValue, prefabGo = prefabGo, pos = pos, rot = rot, scale = scale}
  180. CombineDiff(oldSkins, skinSlotType, avatarId, oldSkins)
  181. end
  182. end
  183. return skinDatas
  184. end
  185. function RoleViewSystem:ctor()
  186. self.skinSystem = nil
  187. self.viewSystem = nil
  188. self.HeroId = nil
  189. -- 实例化的对象,在系统销毁时,需要销毁或回收
  190. self.gameObject = nil
  191. -- 在异步创建中的皮肤数据
  192. self.updateDiffSkins = nil
  193. -- 在异步创建中的扩展皮肤数据
  194. self.updateDiffExtSkins = nil
  195. -- 当前显示的皮肤数据
  196. self.oldSkins = nil
  197. -- 当前显示的扩展皮肤数据
  198. self.oldExtSkins = nil
  199. -- 如果不为空,说明基本骨骼都需要改变,则全部重新创建
  200. self.updateRoleCfgId = nil
  201. self.oldRoleCfgId = nil
  202. -- 在异步创建中的动作控制器
  203. self.updateRoleInEnvType = nil
  204. -- 在异步创建中的额外显示数据
  205. self.updateExtGoesShow = nil
  206. -- 当前额外显示数据
  207. self.oldShowData = nil
  208. -- 当前显示的动作控制器
  209. self.oldRoleInEnvType = nil
  210. self.ownercb = nil
  211. self.cb = nil
  212. self.loadSystem = MultiTypeAssetLoadSystem:new()
  213. self.loadSystem:SetCompleteCB(self, self.PreloadedAssets)
  214. end
  215. function RoleViewSystem:Dispose()
  216. self:Recycle()
  217. self.loadSystem:Dispose()
  218. self.loadSystem = nil
  219. end
  220. function RoleViewSystem:SetHeroId(Id)
  221. self.HeroId = Id
  222. end
  223. function RoleViewSystem:RefreshData(roleInEnvType, roleCfgId, viewData, extGoesShowData, createdOwner, createdCB)
  224. -- 角色模型已经生成成功了
  225. local skinChanged, viewChanged = false, false
  226. if not tolua.isnull(self.gameObject) and self.oldRoleCfgId == roleCfgId then
  227. skinChanged = self:UpdateSkinData(roleInEnvType, roleCfgId, viewData)
  228. viewChanged = self:UpdateHeroExtGoesShowView(extGoesShowData)
  229. else
  230. self:Recycle()
  231. self:UpdateSkinData(roleInEnvType, roleCfgId, viewData)
  232. self:UpdateHeroExtGoesShowView(extGoesShowData)
  233. self.updateRoleCfgId = roleCfgId
  234. skinChanged = true
  235. viewChanged = true
  236. end
  237. if skinChanged or viewChanged then
  238. self.loadSystem:Cancel()
  239. self.ownercb = createdOwner
  240. self.cb = createdCB
  241. self:RefreshLoadSystem()
  242. end
  243. return skinChanged or viewChanged
  244. end
  245. function RoleViewSystem:UpdateSkinData(roleInEnvType, roleCfgId, viewData)
  246. local changed,diffSkins = SkinsDiff(roleCfgId, self.oldSkins, viewData)
  247. changed = (roleInEnvType ~= self.oldRoleInEnvType) or changed
  248. if changed then
  249. -- self:CancelCreate()
  250. self.updateDiffSkins = nil
  251. self.updateRoleInEnvType = nil
  252. end
  253. self.updateDiffSkins = diffSkins
  254. self.updateRoleInEnvType = (roleInEnvType ~= self.oldRoleInEnvType) and roleInEnvType or nil
  255. return changed
  256. end
  257. --- 刷新显示数据,并自动变更形象
  258. function RoleViewSystem:RefreshView(roleInEnvType, roleCfgId, viewData, heroViewData, createdOwner, createdCB)
  259. -- 角色模型已经生成成功了
  260. local changed = self:RefreshData(roleInEnvType, roleCfgId, viewData, heroViewData, createdOwner, createdCB)
  261. if not changed then
  262. if createdOwner and createdCB then
  263. createdCB(createdOwner, self.gameObject,false,self.HeroId)
  264. end
  265. self.ownercb = nil
  266. self.cb = nil
  267. return
  268. end
  269. self:BeginCreate()
  270. end
  271. --- 强制修改当前皮肤
  272. --- changeExtSkins {{[Enum.SkinSlotType.HeadTop] = RoleAvatarId}}
  273. --- RoleAvatarId为 RoleAvatarCfg的Id
  274. function RoleViewSystem:ChangeExtSkins(changeExtSkins)
  275. if not changeExtSkins then return end
  276. local changed = false
  277. local diffExtSkins = {}
  278. for skinSlotType, avatarId in pairs(changeExtSkins) do
  279. if CombineDiff(self.oldExtSkins, skinSlotType, avatarId, diffExtSkins) then
  280. changed = true
  281. end
  282. end
  283. if changed then
  284. self.updateDiffExtSkins = diffExtSkins
  285. self:CancelCreate()
  286. self:RefreshLoadSystem()
  287. self:BeginCreate()
  288. end
  289. end
  290. --- CS代码强制修改当前皮肤
  291. function RoleViewSystem:CSChangeExtSkins(skinSlotTypes, roleAvatarIds)
  292. if not roleAvatarIds or not skinSlotTypes then return end
  293. local len1 = roleAvatarIds.Length
  294. local len2 = skinSlotTypes.Length
  295. local len = Mathf.Max(len1, len2)
  296. if len <= 0 then return end
  297. local changed = false
  298. local diffExtSkins = {}
  299. for i = 0, len - 1 do
  300. local skinSlotType = skinSlotTypes[i]
  301. local avatarId = roleAvatarIds[i]
  302. if CombineDiff(self.oldExtSkins, skinSlotType, avatarId, diffExtSkins) then
  303. changed = true
  304. end
  305. end
  306. if changed then
  307. self.updateDiffExtSkins = diffExtSkins
  308. self:CancelCreate()
  309. self:RefreshLoadSystem()
  310. self:BeginCreate()
  311. end
  312. end
  313. --- 获得预加载的资源列表 table(reousrceType, table(path))
  314. function RoleViewSystem:StatisticsPreloadAssets(multiTypeAssetLoadSystem)
  315. if not multiTypeAssetLoadSystem then multiTypeAssetLoadSystem = MultiTypeAssetLoadSystem:new() end
  316. self.loadSystem:AddToLoadSystem(multiTypeAssetLoadSystem)
  317. end
  318. function RoleViewSystem:RefreshLoadSystem()
  319. self.loadSystem:RemoveLoadAllAsset()
  320. local assetPath = nil
  321. local assetName = nil
  322. local cfgMgr = ManagerContainer.CfgMgr
  323. -- 基本骨骼
  324. if self.updateRoleCfgId then
  325. local roleCfg = cfgMgr:GetRoleDataById(self.updateRoleCfgId)
  326. local baseName
  327. if roleCfg then
  328. if roleCfg.Sex == Enum.SexType.Man then
  329. baseName = GlobalConfig.c_man_base_model_path
  330. else
  331. baseName = GlobalConfig.c_woman_base_model_path
  332. end
  333. assetName = GlobalConfig.Instance:GetConfigStrValue(baseName)
  334. else
  335. roleCfg = ManagerContainer.CfgMgr:GetPartnerDataById(self.updateRoleCfgId)
  336. local avatarData = ManagerContainer.CfgMgr:GetAvatarCfgById(roleCfg.AvatarId)
  337. assetName = avatarData.AvatarPrefab
  338. end
  339. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.ModelPath, assetName)
  340. end
  341. -- 动作控制器
  342. if self.updateRoleInEnvType then
  343. assetPath, assetName = GetAnimatorControllerPath(self.updateRoleCfgId or self.oldRoleCfgId, self.updateRoleInEnvType)
  344. self.loadSystem:AddLoadAsset(Enum.ResourceType.AnimatorController, assetPath, assetName)
  345. end
  346. local getRoleAvatarById = cfgMgr.GetRoleAvatarById
  347. if self.updateDiffSkins then
  348. for skinSlotType,avatarId in pairs(self.updateDiffSkins) do
  349. if avatarId and avatarId > 0 then
  350. local roleAvatarCfg = getRoleAvatarById(cfgMgr, avatarId)
  351. if roleAvatarCfg then
  352. local avatarPrefab = roleAvatarCfg.AvatarPrefab
  353. if avatarPrefab and avatarPrefab ~= '' then
  354. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.ModelPath, avatarPrefab)
  355. end
  356. end
  357. end
  358. end
  359. end
  360. if self.updateDiffExtSkins then
  361. for skinSlotType,avatarId in pairs(self.updateDiffExtSkins) do
  362. if avatarId and avatarId > 0 then
  363. local roleAvatarCfg = getRoleAvatarById(cfgMgr, avatarId)
  364. if roleAvatarCfg then
  365. local avatarPrefab = roleAvatarCfg.AvatarPrefab
  366. if avatarPrefab and avatarPrefab ~= '' then
  367. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.ModelPath, avatarPrefab)
  368. end
  369. end
  370. end
  371. end
  372. end
  373. if self.updateExtGoesShow then
  374. for heroViewType,val in pairs(self.updateExtGoesShow) do
  375. if val and val > 0 then
  376. if heroViewType == Enum.HeroExtGoesShowType.God_Art then
  377. local cfgData = cfgMgr:GetArtifactCfgDataByCfgId(val)
  378. if cfgData then
  379. local avatarPrefab = cfgData.AvatarPrefab
  380. if avatarPrefab and avatarPrefab ~= '' then
  381. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.ModelPath, avatarPrefab)
  382. end
  383. end
  384. elseif heroViewType == Enum.HeroExtGoesShowType.Strength_Halo then
  385. local lv = val
  386. if val >= #Constant.PartnerStrengthEffectPath then
  387. lv = #Constant.PartnerStrengthEffectPath
  388. end
  389. local effectName = Constant.PartnerStrengthEffectPath[lv]
  390. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.EffectPath, effectName)
  391. end
  392. end
  393. end
  394. end
  395. end
  396. ---@generic 预加载的资源已加载完成
  397. function RoleViewSystem:PreloadedAssets()
  398. self:Create()
  399. end
  400. function RoleViewSystem:BeginCreate()
  401. self.loadSystem:Begin()
  402. end
  403. function RoleViewSystem:CancelCreate()
  404. self.loadSystem:Cancel()
  405. self.updateDiffSkins = nil
  406. self.updateExtGoesShow = nil
  407. self.updateRoleCfgId = nil
  408. self.updateRoleInEnvType = nil
  409. end
  410. function RoleViewSystem:Recycle()
  411. self:CancelCreate()
  412. if self.skinSystem then
  413. self.skinSystem:Dispose()
  414. end
  415. self.skinSystem = nil
  416. if self.viewSystem then
  417. self.viewSystem:Dispose()
  418. end
  419. self.viewSystem = nil
  420. if not tolua.isnull(self.gameObject) then
  421. local go = self.gameObject
  422. local animator = go:GetComponent(AnimatorType)
  423. animator.runtimeAnimatorController = nil
  424. local animControlSkin = go:GetComponent(AnimControlSkinType)
  425. if animControlSkin then
  426. animControlSkin:SetBindLuaCallback(nil, nil)
  427. end
  428. local roleCfg = ManagerContainer.CfgMgr:GetRoleDataById(self.oldRoleCfgId)
  429. local baseName, assetName
  430. if roleCfg then
  431. if roleCfg.Sex == Enum.SexType.Man then
  432. baseName = GlobalConfig.c_man_base_model_path
  433. else
  434. baseName = GlobalConfig.c_woman_base_model_path
  435. end
  436. assetName = GlobalConfig.Instance:GetConfigStrValue(baseName)
  437. else
  438. roleCfg = ManagerContainer.CfgMgr:GetPartnerDataById(self.oldRoleCfgId)
  439. local avatarData = ManagerContainer.CfgMgr:GetAvatarCfgById(roleCfg.AvatarId)
  440. assetName = avatarData.AvatarPrefab
  441. end
  442. ManagerContainer.ResMgr:RecycleGO(Constants.ModelPath, assetName, go)
  443. end
  444. self.gameObject = nil
  445. self.oldSkins = nil
  446. self.oldShowData = nil
  447. self.oldExtSkins = nil
  448. self.oldRoleCfgId = nil
  449. self.oldRoleInEnvType = nil
  450. self.ownercb = nil
  451. self.cb = nil
  452. end
  453. -- 需要预先加载好了资源
  454. function RoleViewSystem:Create()
  455. local assetPath = nil
  456. local assetName = nil
  457. local newGO = nil
  458. local cfgMgr = ManagerContainer.CfgMgr
  459. local isLeader = false
  460. -- 基本骨骼
  461. if self.updateRoleCfgId then
  462. local roleCfg = cfgMgr:GetRoleDataById(self.updateRoleCfgId)
  463. local baseName
  464. if roleCfg then
  465. isLeader = true
  466. if roleCfg.Sex == Enum.SexType.Man then
  467. baseName = GlobalConfig.c_man_base_model_path
  468. else
  469. baseName = GlobalConfig.c_woman_base_model_path
  470. end
  471. assetName = GlobalConfig.Instance:GetConfigStrValue(baseName)
  472. else
  473. roleCfg = ManagerContainer.CfgMgr:GetPartnerDataById(self.updateRoleCfgId)
  474. local avatarData = ManagerContainer.CfgMgr:GetAvatarCfgById(roleCfg.AvatarId)
  475. assetName = avatarData.AvatarPrefab
  476. end
  477. newGO = ManagerContainer.ResMgr:GetGoFromPool(Constants.ModelPath, assetName)
  478. if ManagerContainer.LuaModelMgr and ManagerContainer.LuaModelMgr.ModelRoot then
  479. newGO.transform:SetParent(ManagerContainer.LuaModelMgr.ModelRoot.transform)
  480. else
  481. newGO.transform.parent = nil
  482. end
  483. newGO.transform.position = Vector3.zero;
  484. newGO.transform.rotation = Quaternion.identity;
  485. newGO:SetActive(true)
  486. self.oldRoleCfgId = self.updateRoleCfgId
  487. self.gameObject = newGO
  488. else
  489. local roleCfg = cfgMgr:GetRoleDataById(self.oldRoleCfgId)
  490. if roleCfg then
  491. isLeader = true
  492. end
  493. end
  494. -- 其它部位 时装 暂时只有主角有
  495. if isLeader then
  496. local oldSkins = self.oldSkins
  497. if not oldSkins then
  498. oldSkins = {}
  499. self.oldSkins = oldSkins
  500. end
  501. local skinDatas = GetSkinDatas(oldSkins, self.updateDiffSkins)
  502. local oldExtSkins = self.oldExtSkins
  503. if not oldExtSkins then
  504. oldExtSkins = {}
  505. self.oldExtSkins = oldExtSkins
  506. end
  507. local skinExtDatas = GetSkinDatas(oldExtSkins, self.updateDiffExtSkins)
  508. if not self.skinSystem then
  509. self.skinSystem = SkinSystem.new()
  510. end
  511. if not tolua.isnull(newGO) then
  512. -- 新角色生成
  513. self.skinSystem:Init(self.gameObject.transform, skinDatas, skinExtDatas, true)
  514. else
  515. -- 刷新形象
  516. self.skinSystem:SetSkins(skinDatas, skinExtDatas, true)
  517. end
  518. end
  519. -- 动作控制器 (在有BlendShape的SkinnedMeshRenderer之后加,否则Animator不生效BlendShape动画)
  520. if self.updateRoleInEnvType then
  521. assetPath, assetName = GetAnimatorControllerPath(self.updateRoleCfgId or self.oldRoleCfgId, self.updateRoleInEnvType)
  522. local animatorController = ManagerContainer.ResMgr:GetAsset(assetPath, assetName)
  523. local go = self.gameObject
  524. if not tolua.isnull(go) then
  525. local animator = go:GetComponent(AnimatorType)
  526. if tolua.isnull(animator) then
  527. animator = go:AddComponent(AnimatorType)
  528. end
  529. animator.runtimeAnimatorController = animatorController
  530. local animControlSkin = go:GetComponent(AnimControlSkinType)
  531. if tolua.isnull(animControlSkin) then
  532. animControlSkin = go:AddComponent(AnimControlSkinType)
  533. end
  534. animControlSkin:SetBindLuaCallback(self, self.CSChangeExtSkins)
  535. end
  536. self.oldRoleInEnvType = self.updateRoleInEnvType
  537. end
  538. -- 额外显示物件
  539. local oldShowData = self.oldShowData
  540. if not oldShowData then
  541. oldShowData = {}
  542. self.oldShowData = oldShowData
  543. end
  544. if not self.viewSystem then
  545. self.viewSystem = ViewSystem.new()
  546. end
  547. -- 刷新额外显示
  548. if self.updateExtGoesShow then
  549. self.viewSystem:UpdateView(self.gameObject.transform, oldShowData, self.updateExtGoesShow)
  550. end
  551. -- 更新完形象,清空队列中的数据
  552. self.updateDiffSkins = nil
  553. self.updateDiffExtSkins = nil
  554. self.updateRoleCfgId = nil
  555. self.updateRoleInEnvType = nil
  556. self.updateExtGoesShow = nil
  557. -- 回调
  558. if self.ownercb and self.cb then
  559. self.cb(self.ownercb, self.gameObject, (newGO ~= nil),self.HeroId)
  560. end
  561. self.ownercb = nil
  562. self.cb = nil
  563. return self.gameObject
  564. end
  565. function RoleViewSystem:RolePlayAni(go, aniName)
  566. if tolua.isnull(go) then return end
  567. local animator = go:GetComponent(Enum.TypeInfo.Animator)
  568. if tolua.isnull(animator) then return end
  569. animator:Play(aniName)
  570. end
  571. function RoleViewSystem:GetGameObject()
  572. return self.gameObject
  573. end
  574. ----------------------------------------------神器----------------------------------------------
  575. local function ExtGoesShowUpdate(map, key, data, diffMap)
  576. if not key or not data then
  577. return false
  578. end
  579. diffMap[key] = data[key]
  580. if not map or map[key] ~= data[key] then
  581. return true
  582. end
  583. return false
  584. end
  585. local function HeroExtGoesShowViewUpdate(roleInEnvType, oldShowData, curExtGoesShow)
  586. local extGoesShow = {}
  587. local changed = false
  588. if ExtGoesShowUpdate(oldShowData, Enum.HeroExtGoesShowType.God_Art, curExtGoesShow, extGoesShow) then
  589. changed = true
  590. end
  591. if roleInEnvType ~= Enum.RoleInEnvType.Battle then
  592. if ExtGoesShowUpdate(oldShowData, Enum.HeroExtGoesShowType.Strength_Halo, curExtGoesShow, extGoesShow) then
  593. changed = true
  594. end
  595. end
  596. return changed, extGoesShow
  597. end
  598. function RoleViewSystem:UpdateHeroExtGoesShowView(extGoesShow)
  599. local changed, viewUpdate = HeroExtGoesShowViewUpdate(self.updateRoleInEnvType, self.oldShowData, extGoesShow)
  600. if changed then
  601. self.updateExtGoesShow = nil
  602. end
  603. self.updateExtGoesShow = viewUpdate
  604. return changed
  605. end
  606. -------------------------------------------------------------------------------------------------
  607. return RoleViewSystem