ModelViewSystem.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. local ModelViewSystem = class("ModelViewSystem")
  2. local MultiTypeAssetLoadSystem = require("MultiTypeAssetLoadSystem")
  3. local AnimatorType = typeof(UnityEngine.Animator)
  4. local _dialogueName = "Dialogue/Dialogue"; --冒泡对话的预设物的名字
  5. local _goPosition = Vector3.zero --实例化出来的模型的默认位置
  6. function ModelViewSystem:ctor()
  7. self.modelPath = nil
  8. self.modelName = nil
  9. self.acPath = nil
  10. self.acName = nil
  11. self.loadModelPath = nil
  12. self.loadModelName = nil
  13. self.loadAcPath = nil
  14. self.loadAcName = nil
  15. self.modelGo = nil
  16. self._dialogueData = nil;
  17. self._dialogueGo = nil;
  18. self:RemoveTimers();
  19. self.ownercb = nil
  20. self.cb = nil
  21. self.loadSystem = MultiTypeAssetLoadSystem:new()
  22. self.loadSystem:SetCompleteCB(self, self.PreloadedAssets)
  23. end
  24. function ModelViewSystem:Dispose()
  25. self:RemoveTimers()
  26. self:Recycle()
  27. self.loadSystem:Dispose()
  28. self.loadSystem = nil
  29. --目前是不重复利用的,释放就删除
  30. CommonUtil.DestroyGOImmediate(self.modelGo);
  31. CommonUtil.DestroyGOImmediate(self._dialogueGo);
  32. end
  33. function ModelViewSystem:Recycle()
  34. --self:CancelCreate()
  35. if not tolua.isnull(self.modelGo) then
  36. local go = self.modelGo
  37. self.modelGo = nil
  38. local animator = go:GetComponent(AnimatorType)
  39. animator.runtimeAnimatorController = nil
  40. ManagerContainer.ResMgr:RecycleGO(self.loadModelPath, self.loadModelName, go)
  41. end
  42. end
  43. --[[
  44. _dialogueData = {
  45. _textStr = "" --要说的话
  46. _showStartTime = 0000, --显示的开始时间
  47. _showEndTime = 0000, --显示的结束时间
  48. _dialogueParent = "path", --冒泡对话的父级节点的查路径
  49. }
  50. ]]--
  51. function ModelViewSystem:RefreshView(modelPath, modelName, acPath, acName, _dialogueData, _position, createdOwner, createdCB)
  52. if _position then
  53. _goPosition = _position;
  54. end
  55. self:CancelCreate()
  56. if self.modelPath ~= modelPath then
  57. self.loadModelPath = modelPath
  58. self.loadModelName = modelName
  59. self:Recycle()
  60. else
  61. if self.modelName ~= modelName then
  62. self.loadModelPath = modelPath
  63. self.loadModelName = modelName
  64. self:Recycle()
  65. end
  66. end
  67. if self.acPath ~= acPath then
  68. self.loadAcPath = acPath
  69. self.loadAcName = acName
  70. else
  71. if self.acName ~= acName then
  72. self.loadAcPath = acPath
  73. self.loadAcName = acName
  74. end
  75. end
  76. if _dialogueData then
  77. self._dialogueData = _dialogueData;
  78. end
  79. self.ownercb = createdOwner;
  80. self.cb = createdCB;
  81. if self.loadModelPath
  82. or self.loadModelName
  83. or self.loadAcPath
  84. or self.loadAcName
  85. or self._dialogueData then
  86. self:RefreshLoadSystem()
  87. self:BeginCreate()
  88. end
  89. end
  90. function ModelViewSystem:RefreshLoadSystem()
  91. self.loadSystem:RemoveLoadAllAsset()
  92. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, self.loadModelPath, self.loadModelName)
  93. self.loadSystem:AddLoadAsset(Enum.ResourceType.AnimatorController, self.loadAcPath, self.loadAcName)
  94. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.ModelPath, _dialogueName)
  95. end
  96. function ModelViewSystem:PreloadedAssets()
  97. self:Create()
  98. end
  99. function ModelViewSystem:BeginCreate()
  100. self.loadSystem:Begin()
  101. end
  102. function ModelViewSystem:CancelCreate()
  103. self.loadModelPath = nil
  104. self.loadModelName = nil
  105. self.loadAcPath = nil
  106. self.loadAcName = nil
  107. self.ownercb = nil
  108. self.cb = nil
  109. self.loadSystem:Cancel()
  110. end
  111. function ModelViewSystem:Create()
  112. local newGo = nil
  113. if self.loadModelPath and self.loadModelName then
  114. newGo = ManagerContainer.ResMgr:GetGoFromPool(self.loadModelPath, self.loadModelName)
  115. self.modelPath = self.loadModelPath
  116. self.modelName = self.loadModelName
  117. end
  118. if self.loadAcPath and self.loadAcName then
  119. self.acPath = self.loadAcPath
  120. self.acName = self.loadAcName
  121. local go = newGo or self.modelGo
  122. if go then
  123. local animatorController = ManagerContainer.ResMgr:GetAsset(self.loadAcPath, self.loadAcName)
  124. local animator = go:GetComponent(Enum.TypeInfo.Animator)
  125. if not animator then
  126. animator = go:AddComponent(Enum.TypeInfo.Animator)
  127. end
  128. animator.runtimeAnimatorController = animatorController
  129. end
  130. end
  131. if newGo then
  132. self.modelGo = newGo
  133. self:SetGoPosition(_goPosition);
  134. self:SetDialogue();
  135. if self.ownercb and self.cb then
  136. self.cb(self.ownercb, newGo, true)
  137. end
  138. else
  139. if self.ownercb and self.cb then
  140. self.cb(self.ownercb, self.modelGo, true)
  141. end
  142. end
  143. self.ownercb = nil
  144. self.cb = nil
  145. end
  146. --冒泡对话
  147. function ModelViewSystem:SetDialogue()
  148. if self._dialogueData then
  149. if not self._dialogueGo then
  150. local _newGo = ManagerContainer.ResMgr:GetGoFromPool(Constants.ModelPath, _dialogueName);
  151. if not _newGo then
  152. LogError("冒泡预设的路径或者名字不对,请检查");
  153. else
  154. self._dialogueGo = _newGo;
  155. end
  156. end
  157. self._dialogueGo.gameObject:SetActive(false);
  158. local _scaleStr = GlobalConfig.Instance:GetConfigStrValue(164)
  159. local _scale = string.split(_scaleStr, ";")
  160. self._dialogueGo.transform.localScale = Vector3.New(tonumber(_scale[1]), tonumber(_scale[2]), tonumber(_scale[3]));
  161. local _parent = self.modelGo.transform:Find(self._dialogueData._dialogueParent);
  162. if not _parent then _parent = self.modelGo; end
  163. self._dialogueGo.transform:SetParent(_parent.transform);
  164. self:RemoveTimers();
  165. local _showDialogueText = function()
  166. self._dialogueGo.transform.localPosition = Vector3.zero;
  167. self._dialogueGo.transform:Find("Dialogue/Dialog/Text"):GetComponent("Text").text = self._dialogueData._textStr;
  168. self._dialogueGo.gameObject:SetActive(true);
  169. end
  170. self._dialogueStartTimer = ManagerContainer.LuaTimerMgr:AddTimer(self._dialogueData._showStartTime, 1, self, _showDialogueText, nil);
  171. local _endDialogueText = function()
  172. --if not ManagerContainer.DataMgr.SignData:GetKeepDialogue() then
  173. self._dialogueGo.gameObject:SetActive(false);
  174. --end
  175. end
  176. self._dialogueEndTimer = ManagerContainer.LuaTimerMgr:AddTimer(self._dialogueData._showEndTime, 1, self, _endDialogueText, nil);
  177. end
  178. end
  179. function ModelViewSystem:GetGameObject()
  180. return self.modelGo
  181. end
  182. function ModelViewSystem:RemoveTimers()
  183. if self._dialogueStartTimer then
  184. ManagerContainer.LuaTimerMgr:RemoveTimer(self._dialogueStartTimer);
  185. self._dialogueStartTimer = nil;
  186. end
  187. if self._dialogueEndTimer then
  188. ManagerContainer.LuaTimerMgr:RemoveTimer(self._dialogueEndTimer);
  189. self._dialogueEndTimer = nil;
  190. end
  191. end
  192. function ModelViewSystem:SetGoPosition(_position)
  193. if _position then
  194. self.modelGo.transform.position = _position;
  195. end
  196. end
  197. return ModelViewSystem