ArtInfoMgr.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. using Newtonsoft.Json;
  2. using PublicTools.XMLDataBase;
  3. using SC.XR.Unity;
  4. using ShadowStudio.Tool;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. #if UNITY_EDITOR
  10. using UnityEditor;
  11. #endif
  12. using UnityEngine;
  13. using XRTool.Util;
  14. namespace ShadowStudio.Model
  15. {
  16. /// <summary>
  17. /// 资源映射表
  18. /// 读取,获取资源映射表
  19. /// 按照id和类型建立资源映射。通过artid获取对应的artinfo
  20. /// 通过资源类型获取对应的artinfo
  21. /// 创建一个美术资源:
  22. /// </summary>
  23. public class ArtInfoMgr : SC.XR.Unity.Singleton<ArtInfoMgr>
  24. {
  25. private string tableName;
  26. private TableInterface table;
  27. private bool isInit = false;
  28. /// <summary>
  29. /// 所有的资源列表,此列表当前服务器和本地各有一份。后期读取服务器列表数据
  30. /// </summary>
  31. private List<ArtInfo> artInfoList;
  32. /// <summary>
  33. /// 资源id对应的资源信息
  34. /// </summary>
  35. private Dictionary<string, ArtInfo> artInfoMap;
  36. private Dictionary<string, ArtInfo> otherArtInfoMap;
  37. /// <summary>
  38. /// 资源类型对应的资源列表
  39. /// </summary>
  40. private Dictionary<ArtType, List<ArtInfo>> artInfoTypeMap;
  41. /// <summary>
  42. /// 组件的集合
  43. /// </summary>
  44. private Dictionary<string, ArtComponent> artComponentMap = new Dictionary<string, ArtComponent>();
  45. /// <summary>
  46. /// 资源的引用
  47. /// </summary>
  48. private Dictionary<string, ArtResConf> resArtMap;
  49. /// <summary>
  50. /// 资源对应的工具类
  51. /// </summary>
  52. private Dictionary<ArtInfo, ArtHandler> artHandlerMap = new Dictionary<ArtInfo, ArtHandler>();
  53. /// <summary>
  54. /// 对应资源类型的容器,例如视频播放器,图片浏览器,模型浏览器
  55. /// </summary>
  56. private Dictionary<ArtType, List<ArtContainer>> artContainerMap = new Dictionary<ArtType, List<ArtContainer>>();
  57. /// <summary>
  58. /// 数据初始化
  59. /// </summary>
  60. public event Action InitComplete;
  61. /// <summary>
  62. /// 对应资源存储的最大的id
  63. /// 本地生成goods的一种辅助方案,此方案即将移除
  64. /// 此方案已移除,请勿使用2020-09-14
  65. /// </summary>
  66. private Dictionary<ArtInfo, int> goodsIdMap = new Dictionary<ArtInfo, int>();
  67. /// <summary>
  68. /// 使用变量缓存服务器下发的物体信息数据
  69. /// </summary>
  70. private GoodsInfo serverCacheData = new GoodsInfo();
  71. /// <summary>
  72. /// 此为上传至服务器的物体的数据缓存,理论上上传多个物体列表信息,实际处理中每次只会上传一条物体的同步信息
  73. /// </summary>
  74. private List<GoodsInfo> goodsDataList = new List<GoodsInfo>();
  75. /// <summary>
  76. /// 创建容器的事件,某个容器被上传的事件
  77. /// </summary>
  78. public event Action<ArtContainer> ContainerCreate;
  79. /// <summary>
  80. /// 删除容器,某个容器被删除的事件
  81. /// </summary>
  82. public event Action<ArtContainer> ContainerDel;
  83. /// <summary>
  84. /// 容器被选中或者容器非选中状态的事件
  85. /// </summary>
  86. public static event Action<ArtContainer, bool> ContainerSelect;
  87. /// <summary>
  88. /// 编辑菜单的名称
  89. /// </summary>
  90. private string ModelEditBtnGroup = "ModelEditBtnDlg";
  91. //public ArtEditorContainer ModelEditBtn;
  92. /// <summary>
  93. /// 容器缓存
  94. /// 非即时同步容器缓存
  95. /// </summary>
  96. private Dictionary<string, ArtContainer> containerCache = new Dictionary<string, ArtContainer>();
  97. public event Action<string, bool> UserChange;
  98. public void AddCacheContainer(string id, ArtContainer container)
  99. {
  100. if (!ContainerCache.ContainsKey(id))
  101. {
  102. ContainerCache.Add(id, container);
  103. }
  104. }
  105. public void SetContainerSelect(ArtContainer container, bool isSelect)
  106. {
  107. ContainerSelect?.Invoke(container, isSelect);
  108. }
  109. public bool IsSingleMan()
  110. {
  111. //if (GameNode.Instance)
  112. //{
  113. // return GameNode.Instance.UserMap == null || GameNode.Instance.UserMap.Count < 2;
  114. //}
  115. return true;
  116. }
  117. public void RemoveArtInfo(List<UnityEngine.Object> resModelList)
  118. {
  119. for (int i = 0; i < resModelList.Count; i++)
  120. {
  121. string artid = "ArtId_" + resModelList[i].name;
  122. table.DeleteData<ArtInfo>(artid);
  123. }
  124. }
  125. /// <summary>
  126. /// 将数据添加至xml
  127. /// </summary>
  128. /// <param name="resModelList"></param>
  129. /// <param name="artType"></param>
  130. /// <param name="references"></param>
  131. public void AddArtInfo(List<UnityEngine.Object> resModelList, ArtType artType, InstaceType references, bool isForceUpdate, string url = "")
  132. {
  133. if (ArtInfoList == null)
  134. {
  135. ArtInfoList = new List<ArtInfo>();
  136. }
  137. for (int i = 0; i < resModelList.Count; i++)
  138. {
  139. UnityEngine.Object obj = resModelList[i];
  140. string artid = "ArtId_" + obj.name;
  141. if (isForceUpdate || !ArtInfoContains(artid))
  142. {
  143. ArtInfo info = new ArtInfo();
  144. info.ArtId = artid;
  145. info.ArtName = obj.name;
  146. info.ArtType = artType;
  147. info.Distance = 1f;
  148. ///Icon
  149. info.Icon = "ArtIcon/" + artType + "/" + obj.name;
  150. info.InstaceType = references;
  151. info.Size = 1;
  152. if (artType == ArtType.Prefab)
  153. {
  154. info.Distance = (obj as GameObject).transform.localPosition.z;
  155. info.Size = (obj as GameObject).transform.localScale.x;
  156. }
  157. if (references == InstaceType.References)
  158. {
  159. info.Url = url;
  160. }
  161. else if (references == InstaceType.ResourceLoad)
  162. {
  163. #if UNITY_EDITOR
  164. string path = AssetDatabase.GetAssetPath(obj);
  165. int index = path.LastIndexOf("Resources");
  166. path = path.Substring((index += "Resources".Length + 1), path.Length - index - 1);
  167. path = path.Replace(Path.GetExtension(path), "");
  168. info.Url = path;
  169. #endif
  170. }
  171. info.Version = Application.version;
  172. table.InsertData(info);
  173. if (!ArtInfoContains(artid))
  174. {
  175. ArtInfoList.Add(info);
  176. }
  177. }
  178. }
  179. table.Save();
  180. }
  181. public bool ArtInfoContains(string artId)
  182. {
  183. for (int i = 0; i < ArtInfoList.Count; i++)
  184. {
  185. if (ArtInfoList[i].ArtId == artId)
  186. {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. public bool IsInit { get => isInit; set => isInit = value; }
  193. public List<ArtInfo> ArtInfoList { get => artInfoList; set => artInfoList = value; }
  194. public Dictionary<string, ArtContainer> ContainerCache { get => containerCache; set => containerCache = value; }
  195. public Dictionary<string, ArtInfo> OtherArtInfoMap
  196. {
  197. get
  198. {
  199. if (otherArtInfoMap == null)
  200. {
  201. otherArtInfoMap = new Dictionary<string, ArtInfo>();
  202. }
  203. return otherArtInfoMap;
  204. }
  205. }
  206. /// <summary>
  207. /// 房间内物体的映射
  208. /// 房间id与对应的物体资源映射
  209. /// </summary>
  210. public void LoadModelEditBtn()
  211. {
  212. //if (!ModelEditBtn)
  213. //{
  214. // var tmp = Resources.Load<ArtEditorContainer>(ModelEditBtnGroup);
  215. // if (tmp)
  216. // {
  217. // ModelEditBtn = GameObject.Instantiate(tmp);
  218. // GameNode.Instance.SetParent(ObjNode.WorldCanvas, ModelEditBtn.transform, Vector3.zero, Vector3.zero, Vector3.one * 0.001f, false);
  219. // ModelEditBtn.gameObject.SetActive(false);
  220. // }
  221. //}
  222. }
  223. public ArtInfoMgr()
  224. {
  225. var artResList = Resources.LoadAll<ArtResConf>("ArtConf");
  226. resArtMap = new Dictionary<string, ArtResConf>();
  227. if (artResList != null)
  228. {
  229. for (int i = 0; i < artResList.Length; i++)
  230. {
  231. resArtMap.Add(artResList[i].name, artResList[i]);
  232. }
  233. }
  234. var containers = Resources.LoadAll<GameObject>("Container");
  235. if (containers != null)
  236. {
  237. for (int i = 0; i < containers.Length; i++)
  238. {
  239. var container = containers[i].GetComponent<ArtContainer>();
  240. if (container != null)
  241. {
  242. if (!artContainerMap.ContainsKey(container.GetArtType()))
  243. {
  244. List<ArtContainer> list = new List<ArtContainer>();
  245. list.Add(container);
  246. artContainerMap.Add(container.GetArtType(), list);
  247. }
  248. else
  249. {
  250. List<ArtContainer> list = artContainerMap[container.GetArtType()];
  251. list.Add(container);
  252. }
  253. }
  254. }
  255. }
  256. var components = Resources.LoadAll<GameObject>("Container/Component");
  257. if (components != null)
  258. {
  259. for (int i = 0; i < components.Length; i++)
  260. {
  261. var component = containers[i].GetComponent<ArtComponent>();
  262. if (component != null)
  263. {
  264. if (!artComponentMap.ContainsKey(containers[i].name))
  265. {
  266. artComponentMap.Add(containers[i].name, component);
  267. }
  268. }
  269. }
  270. }
  271. if (BuildConfigMgr.Instance.IsInit)
  272. {
  273. //ReadArtInfo(Path.Combine(BuildConfig.Instance.LocalPath, BuildConfig.Instance.artInfoPath));
  274. }
  275. else
  276. {
  277. UnityLog.LogError("配置文件丢失!");
  278. }
  279. //WSHandler.Room.OnBroadcastGood += SyncGoods;
  280. ContainerSelect += OnContainerSelect;
  281. }
  282. public void ListenUser()
  283. {
  284. //if (ShowViewMgr.Instance)
  285. //{
  286. // ShowViewMgr.Instance.OnUserChange -= OnUserChange;
  287. // ShowViewMgr.Instance.OnUserChange += OnUserChange;
  288. //}
  289. //else
  290. //{
  291. // ShowViewMgr.InitComplte += () =>
  292. // {
  293. // if (ShowViewMgr.Instance)
  294. // {
  295. // ShowViewMgr.Instance.OnUserChange -= OnUserChange;
  296. // ShowViewMgr.Instance.OnUserChange += OnUserChange;
  297. // }
  298. // };
  299. //}
  300. }
  301. public void ListenPage()
  302. {
  303. //if (ShowViewMgr.Instance)
  304. //{
  305. // ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
  306. // ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
  307. //}
  308. //else
  309. //{
  310. // ShowViewMgr.InitComplte += () =>
  311. // {
  312. // if (ShowViewMgr.Instance)
  313. // {
  314. // ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
  315. // ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
  316. // }
  317. // };
  318. //}
  319. }
  320. private void OnContainerSelect(ArtContainer container, bool isSelect)
  321. {
  322. if (container.GetGoodsInfo().art_id == "ArtId_PlayerView")
  323. {
  324. return;
  325. }
  326. //if (!ModelEditBtn)
  327. //{
  328. // LoadModelEditBtn();
  329. //}
  330. //if (ModelEditBtn)
  331. //{
  332. // if (ModelEditBtn.gameObject.activeSelf != isSelect)
  333. // {
  334. // ModelEditBtn.gameObject.SetActive(isSelect);
  335. // }
  336. // if (isSelect)
  337. // {
  338. // ModelEditBtn.SetTarget(container, false);
  339. // }
  340. //}
  341. //if (ArtMoreEditor.Instance)
  342. //{
  343. // if (isSelect)
  344. // {
  345. // ArtMoreEditor.Instance.SetTarget(container, false);
  346. // }
  347. //}
  348. }
  349. /// <summary>
  350. /// 读取美术资源
  351. /// </summary>
  352. /// <param name="path"></param>
  353. private void ReadArtInfo(string path)
  354. {
  355. //languageIndex = SystemSettingMgr.Instance.settings.Language;
  356. tableName = typeof(ArtInfo).Name;
  357. #if UNITY_EDITOR || !UNITY_ANDROID
  358. table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
  359. OpenTable();
  360. #else
  361. string fullPath = Path.Combine(path, tableName + ".xml");
  362. TimerMgr.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<ArtInfo> packList) =>
  363. {
  364. InitPackage(packList);
  365. }));
  366. #endif
  367. }
  368. public void OpenLocalData()
  369. {
  370. //ReadArtInfo(Path.Combine(BuildConfig.Instance.LocalPath, BuildConfig.Instance.artInfoPath));
  371. }
  372. private void OpenTable()
  373. {
  374. if (!table.Open())
  375. {
  376. table.Create(tableName);
  377. UnityLog.Log("create table" + tableName);
  378. }
  379. var list = table.FindAllData<ArtInfo>();
  380. for (int i = 0; i < list.Count; i++)
  381. {
  382. list[i].ArtType = ArtInfo.GetArtType((int)list[i].ArtType);
  383. }
  384. //table.Save();
  385. //languagePackList = table.FindAllData<LanguagePackConf>();
  386. InitPackage(list);
  387. }
  388. /// <summary>
  389. /// 初始化包
  390. /// </summary>
  391. /// <param name="lists"></param>
  392. public void InitPackage(List<ArtInfo> lists)
  393. {
  394. ArtInfoList = lists;
  395. if (ArtInfoList == null || ArtInfoList.Count < 1)
  396. {
  397. UnityLog.LogError("美术资源不存在!");
  398. #if UNITY_EDITOR
  399. AddDefaultArt(ArtType.Model);
  400. InitPackage(ArtInfoList);
  401. #endif
  402. return;
  403. }
  404. else
  405. {
  406. if (artInfoMap == null)
  407. {
  408. artInfoMap = new Dictionary<string, ArtInfo>();
  409. }
  410. if (artInfoTypeMap == null)
  411. {
  412. artInfoTypeMap = new Dictionary<ArtType, List<ArtInfo>>();
  413. }
  414. for (int i = 0; i < ArtInfoList.Count; i++)
  415. {
  416. if (!artInfoMap.ContainsKey(ArtInfoList[i].ArtId))
  417. {
  418. artInfoMap.Add(ArtInfoList[i].ArtId, ArtInfoList[i]);
  419. UnityLog.Log(ArtInfoList[i].Url, 3);
  420. }
  421. if (artInfoTypeMap.ContainsKey(ArtInfoList[i].ArtType))
  422. {
  423. List<ArtInfo> list = artInfoTypeMap[ArtInfoList[i].ArtType];
  424. if (list != null && !list.Contains(ArtInfoList[i]))
  425. {
  426. list.Add(ArtInfoList[i]);
  427. }
  428. }
  429. else
  430. {
  431. List<ArtInfo> list = new List<ArtInfo>();
  432. list.Add(ArtInfoList[i]);
  433. artInfoTypeMap.Add(ArtInfoList[i].ArtType, list);
  434. }
  435. }
  436. }
  437. IsInit = true;
  438. InitComplete?.Invoke();
  439. }
  440. /// <summary>
  441. /// 添加Unity基本资源到文本
  442. /// </summary>
  443. public void AddDefaultArt(ArtType artType)
  444. {
  445. if (ArtInfoList == null)
  446. {
  447. ArtInfoList = new List<ArtInfo>();
  448. }
  449. if (artType == ArtType.Model)
  450. {
  451. foreach (PrimitiveType primitive in Enum.GetValues(typeof(PrimitiveType)))
  452. {
  453. ArtInfo info = new ArtInfo();
  454. info.ArtId = "ArtId_" + (int)primitive;
  455. info.ArtName = primitive.ToString();
  456. info.ArtType = ArtType.Model;
  457. info.Distance = 1f;
  458. ///Icon
  459. info.Icon = "PrimitiveType/" + primitive.ToString();
  460. info.InstaceType = InstaceType.UnityBase;
  461. info.Size = 1;
  462. info.Url = "" + (int)primitive;
  463. info.Version = Application.unityVersion;
  464. table.InsertData(info);
  465. ArtInfoList.Add(info);
  466. }
  467. }
  468. else
  469. {
  470. ArtInfo info = new ArtInfo();
  471. info.ArtId = "ArtId_GameObject";
  472. info.ArtName = "GameObject";
  473. info.ArtType = ArtType.Prefab;
  474. info.Distance = 1f;
  475. ///Icon
  476. info.Icon = "";
  477. info.InstaceType = InstaceType.UnityBase;
  478. info.Size = 1;
  479. info.Url = "New";
  480. info.Version = Application.unityVersion;
  481. table.InsertData(info);
  482. ArtInfoList.Add(info);
  483. }
  484. table.Save();
  485. }
  486. /// <summary>
  487. /// 根据资源id获取对应的资源
  488. /// </summary>
  489. /// <param name="artId"></param>
  490. /// <returns></returns>
  491. public ArtInfo GetArtInfo(string artId)
  492. {
  493. if (IsInit && artInfoMap.ContainsKey(artId))
  494. {
  495. return artInfoMap[artId];
  496. }
  497. return null;
  498. }
  499. /// <summary>
  500. /// 根据资源id获取对应的资源
  501. /// </summary>
  502. /// <param name="artId"></param>
  503. /// <returns></returns>
  504. public List<ArtInfo> GetArtInfoList(ArtType artType)
  505. {
  506. if (IsInit)//
  507. {
  508. if (artInfoTypeMap.ContainsKey(artType))
  509. {
  510. return artInfoTypeMap[artType];
  511. }
  512. List<ArtInfo> tmp = new List<ArtInfo>();
  513. foreach (var item in artInfoTypeMap)
  514. {
  515. ///包含此key
  516. if ((item.Key & artType) == item.Key)
  517. {
  518. tmp.AddRange(item.Value);
  519. }
  520. }
  521. artInfoTypeMap.Add(artType, tmp);
  522. return tmp;
  523. }
  524. return null;
  525. }
  526. ///// <summary>
  527. ///// 根据资源id获取对应的资源
  528. ///// </summary>
  529. ///// <param name="artId"></param>
  530. ///// <returns></returns>
  531. //public List<ArtInfo> GetArtInfoList(ArtType[] artType)
  532. //{
  533. // if (IsInit)//
  534. // {
  535. // List<ArtInfo> ArtInfoList = new List<ArtInfo>();
  536. // for (int i = 0; i < artType.Length; i++)
  537. // {
  538. // if (artInfoTypeMap.ContainsKey(artType[i]))
  539. // {
  540. // ArtInfoList.AddRange(artInfoTypeMap[artType[i]]);
  541. // }
  542. // }
  543. // return ArtInfoList;
  544. // }
  545. // return null;
  546. //}
  547. /// <summary>
  548. /// 根据资源id获取对应的资源
  549. /// </summary>
  550. /// <param name="artId"></param>
  551. /// <returns></returns>
  552. public List<ArtInfo> GetArtInfoListSingle(ArtType artType)
  553. {
  554. if (IsInit)//
  555. {
  556. return artInfoTypeMap[artType];
  557. }
  558. return null;
  559. }
  560. /// <summary>
  561. /// 创建一个资源ArtHandler,注意内存回收
  562. /// </summary>
  563. /// <returns></returns>
  564. public ArtHandler CreateArtHandler(ArtInfo art, string containerName = "")
  565. {
  566. if (string.IsNullOrEmpty(containerName))
  567. {
  568. containerName = art.ContainerName;
  569. }
  570. if (artHandlerMap.ContainsKey(art))
  571. {
  572. return artHandlerMap[art];
  573. }
  574. ArtHandler handler = new ArtHandler();
  575. handler.SetData(art, containerName);
  576. artHandlerMap.Add(art, handler);
  577. return handler;
  578. }
  579. /// <summary>
  580. /// 删除资源帮助类ArtHandler
  581. /// </summary>
  582. /// <param name="art"></param>
  583. public void DestrorArtHandler(ArtInfo art)
  584. {
  585. if (artHandlerMap.ContainsKey(art))
  586. {
  587. var handler = artHandlerMap[art];
  588. handler = null;
  589. artHandlerMap.Remove(art);
  590. }
  591. }
  592. /// <summary>
  593. /// 实例化组件
  594. /// </summary>
  595. /// <param name="componentName"></param>
  596. /// <returns></returns>
  597. public ArtComponent InstanceComponent(string componentName)
  598. {
  599. if (artComponentMap != null && artComponentMap.ContainsKey(componentName))
  600. {
  601. ArtComponent component = artComponentMap[componentName];
  602. var tmp = GameObject.Instantiate(component.GetInstace());
  603. return tmp.GetComponent<ArtComponent>();
  604. }
  605. return null;
  606. }
  607. /// <summary>
  608. /// 实例化出一个容器
  609. /// </summary>
  610. /// <param name="artType"></param>
  611. /// <returns></returns>
  612. public ArtContainer CreateContainer(ArtType artType, string containerName = "")
  613. {
  614. if (artContainerMap != null && artContainerMap.ContainsKey(artType))
  615. {
  616. var containerList = artContainerMap[artType];
  617. if (containerList != null)
  618. {
  619. ArtContainer container = null;
  620. if (containerList.Count == 1)
  621. {
  622. container = containerList[0];
  623. }
  624. else
  625. {
  626. for (int i = 0; i < containerList.Count; i++)
  627. {
  628. if (containerList[i].GetInstace().name == containerName)
  629. {
  630. container = containerList[i];
  631. break;
  632. }
  633. }
  634. }
  635. if (container != null)
  636. {
  637. var tmp = GameObject.Instantiate(container.GetInstace());
  638. return tmp.GetComponent<ArtContainer>();
  639. }
  640. }
  641. }
  642. return null;
  643. }
  644. /// <summary>
  645. /// 获取对应的配置文件
  646. /// </summary>
  647. /// <param name="resName"></param>
  648. /// <returns></returns>
  649. public ArtResConf GetResConf(string resName)
  650. {
  651. if (resArtMap.ContainsKey(resName))
  652. {
  653. return resArtMap[resName];
  654. }
  655. UnityLog.LogError(resName + " is not at resArtMap");
  656. return null;
  657. }
  658. /// <summary>
  659. /// 物品的自增id设计
  660. /// </summary>
  661. /// <param name="info"></param>
  662. public int GetGoodsId(ArtInfo info)
  663. {
  664. int id = 1;
  665. if (goodsIdMap.ContainsKey(info))
  666. {
  667. id = goodsIdMap[info] + 1;
  668. goodsIdMap[info] = id;
  669. }
  670. else
  671. {
  672. goodsIdMap.Add(info, id);
  673. }
  674. return id;
  675. }
  676. /// <summary>
  677. /// 同步物体:
  678. /// 创建物体时,接收到广播消息时的数据同步都调用此方法
  679. /// </summary>
  680. /// <param name="rid">用户id,物体的创建者</param>
  681. /// <param name="art_id">美术资源id</param>
  682. /// <param name="goods_name">物体名称</param>
  683. /// <param name="goods_extended">物体额外信息</param>
  684. /// <param name="id">物体id</param>
  685. /// <param name="goods_info">物体的姿态信息</param>
  686. public void SyncGoods(int rid, int status, string art_id, string goods_name, string goods_extended, int id, string goods_info)
  687. {
  688. serverCacheData.rid = rid;
  689. serverCacheData.art_id = art_id;
  690. serverCacheData.goods_name = goods_name;
  691. serverCacheData.goods_extended = goods_extended;
  692. serverCacheData.id = id;
  693. serverCacheData.goods_info = goods_info;
  694. serverCacheData.status = status;
  695. SyncGoods(serverCacheData);
  696. }
  697. public void SyncGoods(List<GoodsInfo> goodDataList)
  698. {
  699. if (goodDataList != null)
  700. {
  701. for (int i = 0; i < goodDataList.Count; i++)
  702. {
  703. SyncGoods(goodDataList[i]);
  704. }
  705. }
  706. }
  707. /// <summary>
  708. /// 同步物体数据
  709. /// 服务器同步或者翻页时同步,服务器同步时开始缓存数据
  710. /// 翻页时同步不添加缓存数据
  711. /// </summary>
  712. /// <param name="goodData"></param>
  713. /// <param name="isServer"></param>
  714. public void SyncGoods(GoodsInfo goodData, bool isServer = true)
  715. {
  716. //if (GameNode.Instance.GoodsContainerMap == null)
  717. //{
  718. // GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
  719. //}
  720. //ArtContainer container = null;
  721. //bool isCreate = false;
  722. //if (SynUser(goodData))
  723. //{
  724. // container = GameNode.Instance.UserMap[goodData.art_id].Container;
  725. //}
  726. //else if (goodData.id != 0)
  727. //{
  728. // ///先检查此数据是否在此页面
  729. // ///如果数据不在当前页,则只缓存起来
  730. // ///如果在当前页,则缓存后实例化进行同步
  731. // ///如果不是服务器的数据,仅同步设置,不缓存数据
  732. // if (isServer && goodData.scene_id > 0)
  733. // {
  734. // if (!GameNode.Instance.PageGoods.ContainsKey(goodData.id))
  735. // {
  736. // GameNode.Instance.PageGoods.Add(goodData.id, goodData);
  737. // }
  738. // else
  739. // {
  740. // GameNode.Instance.PageGoods[goodData.id] = goodData;
  741. // }
  742. // if (!GameNode.Instance.PageGoodsId.ContainsKey(goodData.scene_id))
  743. // {
  744. // var list = new List<int>();
  745. // list.Add(goodData.id);
  746. // GameNode.Instance.PageGoodsId.Add(goodData.scene_id, list);
  747. // }
  748. // else
  749. // {
  750. // var list = GameNode.Instance.PageGoodsId[goodData.scene_id];
  751. // if (list == null)
  752. // {
  753. // list = new List<int>();
  754. // }
  755. // if (!list.Contains(goodData.id))
  756. // {
  757. // list.Add(goodData.id);
  758. // }
  759. // }
  760. // if (goodData.scene_id != CommonMethod.currentScene)
  761. // {
  762. // return;
  763. // }
  764. // }
  765. // ///对象已存在,进行同步
  766. // if (GameNode.Instance.GoodsContainerMap.ContainsKey(goodData.id))
  767. // {
  768. // container = GameNode.Instance.GoodsContainerMap[goodData.id];
  769. // }
  770. // else
  771. // {
  772. // ArtInfo info = GetArtInfo(goodData.art_id);
  773. // if (info == null)
  774. // {
  775. // if (OtherArtInfoMap.ContainsKey(goodData.art_id))
  776. // {
  777. // info = OtherArtInfoMap[goodData.art_id];
  778. // }
  779. // else
  780. // {
  781. // NetWorkHeaders.RequestArtInfo(goodData.art_id, (jsonData) =>
  782. // {
  783. // if (!OtherArtInfoMap.ContainsKey(goodData.art_id))
  784. // {
  785. // info = JsonConvert.DeserializeObject<ArtInfo>(jsonData["data"].ToJson());
  786. // if (info != null)
  787. // {
  788. // info.ArtType = CommonMethod.getArtType(int.Parse(jsonData["data"]["art_type"].ToString()));
  789. // info.ImmediateSyn = jsonData["data"]["immediate_syn"].ToString() == "0" ? false : true;
  790. // if (string.IsNullOrEmpty(info.Component))
  791. // {
  792. // if (info.ArtType == ArtType.Image)
  793. // {
  794. // info.Component = "PictureComponent";
  795. // }
  796. // else if (info.ArtType == ArtType.Movies)
  797. // {
  798. // info.Component = "VideoComponent";
  799. // }
  800. // }
  801. // OtherArtInfoMap.Add(goodData.art_id, info);
  802. // SyncGoods(goodData, isServer);
  803. // UnityLog.Instance.LogError("find artinfo:" + goodData.art_id);
  804. // return;
  805. // }
  806. // else
  807. // {
  808. // UnityLog.Instance.LogError("Error find artinfo:" + goodData.art_id);
  809. // }
  810. // }
  811. // });
  812. // }
  813. // }
  814. // if (info != null)
  815. // {
  816. // ///容器缓存
  817. // if (ContainerCache.ContainsKey(goodData.art_id))
  818. // {
  819. // container = ContainerCache[goodData.art_id];
  820. // ContainerCache.Remove(goodData.art_id);
  821. // }
  822. // else
  823. // {
  824. // ArtHandler handler = CreateArtHandler(info);
  825. // container = handler.CreateArt();
  826. // }
  827. // GameNode.Instance.GoodsContainerMap.Add(goodData.id, container);
  828. // isCreate = true;
  829. // }
  830. // else
  831. // {
  832. // UnityLog.Instance.LogError(goodData.art_id);
  833. // }
  834. // }
  835. //}
  836. //try
  837. //{
  838. // if (container != null && container.GetInstace())
  839. // {
  840. // container.TransferSyn(goodData, goodData.status == (int)TransferState.Doing);
  841. // if (isCreate)
  842. // {
  843. // ContainerCreate?.Invoke(container);
  844. // }
  845. // }
  846. //}
  847. //catch
  848. //{
  849. // ///某个物体可能被删掉了
  850. // ///此种情况不在处理
  851. //}
  852. }
  853. //Dictionary<string, string> form;
  854. //Dictionary<string, string> header;
  855. ///// <summary>
  856. ///// 读取资源信息
  857. ///// </summary>
  858. ///// <returns></returns>
  859. //public IEnumerator ReqOtherArtInfo(string artId, Action<ArtInfo> onReadComplete)
  860. //{
  861. // if (header == null)
  862. // {
  863. // header = new Dictionary<string, string>();
  864. // header.Add("Content-Type", "application/x-www-form-urlencoded");
  865. // }
  866. // if (form == null)
  867. // {
  868. // form = new Dictionary<string, string>();
  869. // form.Add("action", "");
  870. // }
  871. // yield return 0;
  872. //}
  873. /// <summary>
  874. /// 同步用户
  875. /// </summary>
  876. /// <param name="goodData"></param>
  877. /// <returns></returns>
  878. private bool SynUser(GoodsInfo goodData)
  879. {
  880. //if (GameNode.Instance.UserMap == null)
  881. //{
  882. // GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
  883. //}
  884. //if (string.IsNullOrEmpty(goodData.art_id))
  885. //{
  886. // return false;
  887. //}
  888. //return GameNode.Instance.UserMap.ContainsKey(goodData.art_id);
  889. return false;
  890. }
  891. /// <summary>
  892. /// 删除物体
  893. /// 可能由用户主动删除,主动删除传参true.删除相关的数据缓存
  894. /// 翻页时是被动删除。被动删除不移除相关的数据缓存
  895. /// </summary>
  896. /// <param name="id">物体的id</param>
  897. /// <param name="isRealDel">是否真实删除,默认真实删除</param>
  898. public void GoodsDelSync(int id, bool isRealDel = true)
  899. {
  900. //if (GameNode.Instance.GoodsContainerMap == null)
  901. //{
  902. // GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
  903. //}
  904. //ArtContainer container = null;
  905. /////对象已存在,进行同步
  906. //if (GameNode.Instance.GoodsContainerMap.ContainsKey(id))
  907. //{
  908. // container = GameNode.Instance.GoodsContainerMap[id];
  909. // //if (ModelEditBtn)
  910. // //{
  911. // // ModelEditBtn.SetTarget(container, true);
  912. // //}
  913. // if (ArtMoreEditor.Instance)
  914. // {
  915. // ArtMoreEditor.Instance.SetTarget(container, true);
  916. // }
  917. // ContainerDel?.Invoke(container);
  918. // if (container != null)
  919. // {
  920. // container.DestroyArt();
  921. // }
  922. // GameNode.Instance.GoodsContainerMap.Remove(id);
  923. //}
  924. /////真实删除
  925. //if (isRealDel)
  926. //{
  927. // ///删除物体的id
  928. // ///先查找物体id对应的Goods
  929. // if (GameNode.Instance.PageGoods.ContainsKey(id))
  930. // {
  931. // ///获得Goods
  932. // var good = GameNode.Instance.PageGoods[id];
  933. // ///查找页对应的物体的id
  934. // if (GameNode.Instance.PageGoodsId.ContainsKey(good.scene_id))
  935. // {
  936. // ///获取页对应的id列表,并
  937. // var list = GameNode.Instance.PageGoodsId[good.scene_id];
  938. // if (list != null && list.Contains(id))
  939. // {
  940. // list.Remove(id);
  941. // }
  942. // }
  943. // GameNode.Instance.PageGoods.Remove(id);
  944. // ContainerDel?.Invoke(null);
  945. // }
  946. //}
  947. }
  948. public void SendGoodsTransfer(GoodsInfo info)
  949. {
  950. //goodsDataList.Add(info);
  951. ////WSHandler.Room.BroadcastGood(goodsDataList);
  952. //goodsDataList.Clear();
  953. }
  954. public void SendTransfer(GoodsInfo info)
  955. {
  956. //goodsDataList.Add(info);
  957. ////WSHandler.Room.TransmitGood(goodsDataList);
  958. //goodsDataList.Clear();
  959. }
  960. /// <summary>
  961. /// 当新用户进入时的事件
  962. /// </summary>
  963. /// <param name="userList"></param>
  964. //public void OnUserChange(List<Peer> userList, bool isJoin)
  965. //{
  966. // if (GameNode.Instance.UserMap == null)
  967. // {
  968. // GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
  969. // }
  970. // if (userList != null)
  971. // {
  972. // for (int i = 0; i < userList.Count; i++)
  973. // {
  974. // if (GameNode.Instance.UserMap.ContainsKey(userList[i].PeerId))
  975. // {
  976. // if (isJoin)
  977. // {
  978. // continue;
  979. // }
  980. // else
  981. // {
  982. // var container = GameNode.Instance.UserMap[userList[i].PeerId];
  983. // container.StopSyn();
  984. // container.DestroyArt();
  985. // UserChange?.Invoke(userList[i].PeerId, false);
  986. // GameNode.Instance.UserMap.Remove(userList[i].PeerId);
  987. // }
  988. // }
  989. // else
  990. // {
  991. // if (isJoin)
  992. // {
  993. // ArtInfo info = GetArtInfo(PlayerContainer.userArtId);
  994. // if (info != null)
  995. // {
  996. // //string cName = info.ContainerName;
  997. // //if (string.IsNullOrEmpty(cName))
  998. // //{
  999. // // cName = PlayerContainer.containerName;
  1000. // //}
  1001. // //ArtHandler handler = CreateArtHandler(info, PlayerContainer.containerName);
  1002. // ArtHandler handler = CreateArtHandler(info);
  1003. // var container = handler.CreateArt();
  1004. // if (container != null)
  1005. // {
  1006. // if (container.ArtComponent != null && container.ArtComponent.GetInstace())
  1007. // {
  1008. // if (container.ArtComponent is PlayerContainer)
  1009. // {
  1010. // (container.ArtComponent as PlayerContainer).SetPeer(userList[i]);
  1011. // GameNode.Instance.UserMap.Add(userList[i].PeerId, container.ArtComponent as PlayerContainer);
  1012. // UserChange?.Invoke(userList[i].PeerId, true);
  1013. // }
  1014. // }
  1015. // }
  1016. // }
  1017. // else
  1018. // {
  1019. // UnityLog.Instance.LogError(PlayerContainer.userArtId + " cant find art");
  1020. // }
  1021. // }
  1022. // else
  1023. // {
  1024. // continue;
  1025. // }
  1026. // }
  1027. // }
  1028. // }
  1029. //}
  1030. /// <summary>
  1031. /// 删除场景的物体
  1032. /// </summary>
  1033. /// <param name="id"></param>
  1034. /// <param name="isOnlyScene"></param>
  1035. public void DelGoodsById(int id)
  1036. {
  1037. //bool isCanDel = true;
  1038. //if (GameNode.Instance.PageGoods.ContainsKey(id))
  1039. //{
  1040. // var goods = GameNode.Instance.PageGoods[id];
  1041. // if (goods.scene_id == CommonMethod.currentScene)
  1042. // {
  1043. // isCanDel = true;
  1044. // }
  1045. //}
  1046. //if (isCanDel)
  1047. //{
  1048. // WSHandler.Room.DeleteGood(new int[] { id });
  1049. //}
  1050. }
  1051. public void DelGoodsByIds(List<int> ids)
  1052. {
  1053. //List<int> list = new List<int>();
  1054. //for (int i = 0; i < ids.Count; i++)
  1055. //{
  1056. // int id = ids[i];
  1057. // if (GameNode.Instance.PageGoods.ContainsKey(id))
  1058. // {
  1059. // var goods = GameNode.Instance.PageGoods[id];
  1060. // if (goods.scene_id == CommonMethod.currentScene)
  1061. // {
  1062. // list.Add(id);
  1063. // }
  1064. // }
  1065. //}
  1066. //if (list.Count > 0)
  1067. //{
  1068. // WSHandler.Room.DeleteGood(list.ToArray());
  1069. //}
  1070. }
  1071. /// <summary>
  1072. /// 翻页的数据处理
  1073. /// 翻页时注意,删除旧的数据
  1074. /// 同步新的数据
  1075. /// </summary>
  1076. /// <param name="all"></param>
  1077. /// <param name="current"></param>
  1078. public void OnChangePage(int all, int current)
  1079. {
  1080. //if (GameNode.Instance)
  1081. //{
  1082. // if (GameNode.Instance.DefaultPageIndex != current)
  1083. // {
  1084. // if (GameNode.Instance.DefaultPageIndex >= 0)
  1085. // {
  1086. // if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
  1087. // {
  1088. // var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
  1089. // for (int i = 0; i < list.Count; i++)
  1090. // {
  1091. // GoodsDelSync(list[i], false);
  1092. // }
  1093. // }
  1094. // }
  1095. // if (ModelEditBtn)
  1096. // {
  1097. // ModelEditBtn.gameObject.SetActive(false);
  1098. // }
  1099. // GameNode.Instance.DefaultPageIndex = current;
  1100. // if (GameNode.Instance.DefaultPageIndex >= 0)
  1101. // {
  1102. // if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
  1103. // {
  1104. // var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
  1105. // for (int i = 0; i < list.Count; i++)
  1106. // {
  1107. // if (GameNode.Instance.PageGoods.ContainsKey(list[i]))
  1108. // {
  1109. // SyncGoods(GameNode.Instance.PageGoods[list[i]], false);
  1110. // }
  1111. // }
  1112. // }
  1113. // }
  1114. // }
  1115. //}
  1116. }
  1117. }
  1118. }