ArtInfoMgr.cs 43 KB

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