AvatarRTMgr.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LuaInterface;
  5. public class AvatarRTMgr : Singleton<AvatarRTMgr>
  6. {
  7. private LuaTable mPreviewLuaTbl;
  8. private List<ActorAvatar> mActors;
  9. private bool bStartLoading = false;
  10. private bool bLoaded = false;
  11. public bool IsLoading
  12. {
  13. get { return bStartLoading && !bLoaded; }
  14. }
  15. public override void Init()
  16. {
  17. base.Init();
  18. mActors = new List<ActorAvatar>();
  19. }
  20. public override void UnInit()
  21. {
  22. base.UnInit();
  23. }
  24. List<ActorData> mNeedLoadActors = new List<ActorData>();
  25. public void LoadPreviewActors(ActorData[] actors, LuaTable luaTbl)
  26. {
  27. if (actors == null || actors.Length == 0) return;
  28. if (bStartLoading) return;
  29. mNeedLoadActors.Clear();
  30. mNeedLoadActors.AddRange(actors);
  31. if (mActors != null && mActors.Count > 0)
  32. {
  33. for(int idx = mActors.Count - 1; idx >= 0 ;idx--)
  34. {
  35. if(!NeedActor(actors,mActors[idx]))
  36. {
  37. mActors[idx].Dispose();
  38. mActors.RemoveAt(idx);
  39. }else
  40. {
  41. mNeedLoadActors.Remove(mActors[idx].Data);
  42. }
  43. }
  44. }
  45. if(mNeedLoadActors.Count == 0)
  46. {
  47. for(int idx =0; idx < mActors.Count;idx++)
  48. {
  49. mActors[idx].IsVisible = true;
  50. }
  51. mPreviewLuaTbl = luaTbl;
  52. mPreviewLuaTbl.CallCS2Lua("OnLoadCompleted");
  53. return;
  54. }
  55. bStartLoading = true;
  56. bLoaded = false;
  57. mPreviewLuaTbl = luaTbl;
  58. for (int idx = 0; idx < mNeedLoadActors.Count; idx++)
  59. {
  60. ActorAvatar avatar = new ActorAvatar(mNeedLoadActors[idx]);
  61. mActors.Add(avatar);
  62. avatar.ParseLoad();
  63. }
  64. EventMgr.AddEventListener<bool>(ECoreEventType.EID_PREPARE_LOAD_OK, OnPrepareLoadOk);
  65. BattlePrepareManager.Instance.StartLoad();
  66. }
  67. private bool NeedActor(ActorData[] actors, ActorAvatar aa)
  68. {
  69. if (actors == null || actors.Length == 0) return false;
  70. for(int idx =0; idx < actors.Length;idx++)
  71. {
  72. if(actors[idx] == aa.Data)
  73. {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. public RenderTexture GetActorRT(long id)
  80. {
  81. if (mActors == null) return null;
  82. for(int idx =0; idx < mActors.Count;idx++)
  83. {
  84. if(mActors[idx].Data.ID == id)
  85. {
  86. return mActors[idx].RT;
  87. }
  88. }
  89. return null;
  90. }
  91. public ActorAvatar FindActorByID(long id)
  92. {
  93. if (mActors == null) return null;
  94. for (int idx = 0; idx < mActors.Count; idx++)
  95. {
  96. if (mActors[idx].Data.ID == id)
  97. {
  98. return mActors[idx];
  99. }
  100. }
  101. return null;
  102. }
  103. public void PlayAnim(long id,string anim)
  104. {
  105. ActorAvatar actor = FindActorByID(id);
  106. if(actor != null)
  107. {
  108. actor.PlayAnim(anim);
  109. }
  110. }
  111. public void Hide()
  112. {
  113. if (mActors != null)
  114. {
  115. for (int idx = 0; idx < mActors.Count; idx++)
  116. {
  117. mActors[idx].IsVisible = false;
  118. }
  119. }
  120. if (!GameMgr.Instance.IsBadDevice)
  121. {
  122. GameMgr.Instance.DisableAntiAliasing();
  123. }
  124. mPreviewLuaTbl = null;
  125. bLoaded = false;
  126. bStartLoading = false;
  127. }
  128. public void Clear()
  129. {
  130. BattlePrepareManager.Instance.Clear();
  131. if (mActors != null)
  132. {
  133. for (int idx = 0; idx < mActors.Count; idx++)
  134. {
  135. mActors[idx].Dispose();
  136. }
  137. mActors.Clear();
  138. }
  139. if (!GameMgr.Instance.IsBadDevice)
  140. {
  141. GameMgr.Instance.DisableAntiAliasing();
  142. }
  143. mPreviewLuaTbl = null;
  144. bLoaded = false;
  145. bStartLoading = false;
  146. }
  147. public void RemoveActor(long id)
  148. {
  149. if (mActors == null) return;
  150. for(int idx = mActors.Count -1;idx>=0;idx--)
  151. {
  152. if(mActors[idx].Data.ID == id)
  153. {
  154. mActors[idx].Dispose();
  155. mActors.RemoveAt(idx);
  156. return;
  157. }
  158. }
  159. }
  160. private void OnPrepareLoadOk(CoreEvent<bool> ce)
  161. {
  162. if (ce.Data)
  163. {
  164. bLoaded = true;
  165. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_PREPARE_LOAD_OK, OnPrepareLoadOk);
  166. CreateActors();
  167. CreateActorsRT();
  168. if(mPreviewLuaTbl!=null)
  169. {
  170. mPreviewLuaTbl.CallCS2Lua("OnLoadCompleted");
  171. }
  172. }
  173. }
  174. private void CreateActors()
  175. {
  176. int initPosX = 1000;
  177. int initPosY = 0;
  178. int initPosZ = 0;
  179. for (int idx = 0; idx < mActors.Count; idx++)
  180. {
  181. mActors[idx].CreateGo(new Vector3(initPosX+idx*50,initPosY,initPosZ),null);
  182. }
  183. }
  184. private void CreateActorsRT()
  185. {
  186. if (!GameMgr.Instance.IsBadDevice)
  187. {
  188. GameMgr.Instance.EnableAntiAliasing();
  189. }
  190. for(int idx =0; idx < mActors.Count;idx++)
  191. {
  192. mActors[idx].GenerateRT();
  193. }
  194. }
  195. }