12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187 |
- using Newtonsoft.Json;
- using PublicTools.XMLDataBase;
- using ShadowStudio.Tool;
- using Studio.Scripts;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using UnityEngine;
- using XRTool.UI;
- using XRTool.Util;
- namespace ShadowStudio.Model
- {
- /// <summary>
- /// 资源映射表
- /// 读取,获取资源映射表
- /// 按照id和类型建立资源映射。通过artid获取对应的artinfo
- /// 通过资源类型获取对应的artinfo
- /// 创建一个美术资源:
- /// </summary>
- public class ArtInfoMgr : Singleton<ArtInfoMgr>
- {
- private string tableName;
- private TableInterface table;
- private bool isInit = false;
- /// <summary>
- /// 所有的资源列表,此列表当前服务器和本地各有一份。后期读取服务器列表数据
- /// </summary>
- private List<ArtInfo> artInfoList;
- /// <summary>
- /// 资源id对应的资源信息
- /// </summary>
- private Dictionary<string, ArtInfo> artInfoMap;
- private Dictionary<string, ArtInfo> otherArtInfoMap;
- /// <summary>
- /// 资源类型对应的资源列表
- /// </summary>
- private Dictionary<ArtType, List<ArtInfo>> artInfoTypeMap;
- /// <summary>
- /// 组件的集合
- /// </summary>
- private Dictionary<string, ArtComponent> artComponentMap = new Dictionary<string, ArtComponent>();
- /// <summary>
- /// 资源的引用
- /// </summary>
- private Dictionary<string, ArtResConf> resArtMap;
- /// <summary>
- /// 资源对应的工具类
- /// </summary>
- private Dictionary<ArtInfo, ArtHandler> artHandlerMap = new Dictionary<ArtInfo, ArtHandler>();
- /// <summary>
- /// 对应资源类型的容器,例如视频播放器,图片浏览器,模型浏览器
- /// </summary>
- private Dictionary<ArtType, List<ArtContainer>> artContainerMap = new Dictionary<ArtType, List<ArtContainer>>();
- /// <summary>
- /// 数据初始化
- /// </summary>
- public event Action InitComplete;
- /// <summary>
- /// 对应资源存储的最大的id
- /// 本地生成goods的一种辅助方案,此方案即将移除
- /// 此方案已移除,请勿使用2020-09-14
- /// </summary>
- private Dictionary<ArtInfo, int> goodsIdMap = new Dictionary<ArtInfo, int>();
- /// <summary>
- /// 使用变量缓存服务器下发的物体信息数据
- /// </summary>
- private GoodsInfo serverCacheData = new GoodsInfo();
- /// <summary>
- /// 此为上传至服务器的物体的数据缓存,理论上上传多个物体列表信息,实际处理中每次只会上传一条物体的同步信息
- /// </summary>
- private List<GoodsInfo> goodsDataList = new List<GoodsInfo>();
- /// <summary>
- /// 创建容器的事件,某个容器被上传的事件
- /// </summary>
- public event Action<ArtContainer> ContainerCreate;
- /// <summary>
- /// 删除容器,某个容器被删除的事件
- /// </summary>
- public event Action<ArtContainer> ContainerDel;
- /// <summary>
- /// 容器被选中或者容器非选中状态的事件
- /// </summary>
- public static Action<ArtContainer, bool> ContainerSelect;
- /// <summary>
- /// 编辑菜单的名称
- /// </summary>
- private string ModelEditBtnGroup = "ModelEditBtnDlg";
- public ArtEditorContainer ModelEditBtn;
- /// <summary>
- /// 容器缓存
- /// 非即时同步容器缓存
- /// </summary>
- private Dictionary<string, ArtContainer> containerCache = new Dictionary<string, ArtContainer>();
- public event Action<string, bool> UserChange;
- public void AddCacheContainer(string id, ArtContainer container)
- {
- if (!ContainerCache.ContainsKey(id))
- {
- ContainerCache.Add(id, container);
- }
- }
- public void SetContainerSelect(ArtContainer container, bool isSelect)
- {
- ContainerSelect?.Invoke(container, isSelect);
- }
- public bool IsSingleMan()
- {
- if (GameNode.Instance)
- {
- return GameNode.Instance.UserMap == null || GameNode.Instance.UserMap.Count < 2;
- }
- return true;
- }
- public void RemoveArtInfo(List<UnityEngine.Object> resModelList)
- {
- for (int i = 0; i < resModelList.Count; i++)
- {
- string artid = "ArtId_" + resModelList[i].name;
- table.DeleteData<ArtInfo>(artid);
- }
- }
- /// <summary>
- /// 将数据添加至xml
- /// </summary>
- /// <param name="resModelList"></param>
- /// <param name="artType"></param>
- /// <param name="references"></param>
- public void AddArtInfo(List<UnityEngine.Object> resModelList, ArtType artType, InstaceType references, bool isForceUpdate, string url = "")
- {
- if (table == null)
- {
- if (BuildConfigMgr.Instance.IsInit)
- {
- ReadArtInfo(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.artInfoPath));
- }
- }
- if (table == null)
- {
- UnityLog.Instance.Log("Read ArtInfo error");
- }
- if (ArtInfoList == null)
- {
- ArtInfoList = new List<ArtInfo>();
- }
- for (int i = 0; i < resModelList.Count; i++)
- {
- UnityEngine.Object obj = resModelList[i];
- string artid = "ArtId_" + obj.name;
- if (isForceUpdate || !ArtInfoContains(artid))
- {
- ArtInfo info = new ArtInfo();
- info.ArtId = artid;
- info.ArtName = obj.name;
- info.ArtType = artType;
- info.Distance = 1f;
- ///Icon
- info.Icon = "ArtIcon/" + artType + "/" + obj.name;
- info.InstaceType = references;
- info.Size = 1;
- if (artType == ArtType.Prefab)
- {
- info.Distance = (obj as GameObject).transform.localPosition.z;
- info.Size = (obj as GameObject).transform.localScale.x;
- }
- if (references == InstaceType.References)
- {
- info.Url = url;
- }
- else if (references == InstaceType.ResourceLoad)
- {
- #if UNITY_EDITOR
- string path = AssetDatabase.GetAssetPath(obj);
- int index = path.LastIndexOf("Resources");
- path = path.Substring((index += "Resources".Length + 1), path.Length - index - 1);
- path = path.Replace(Path.GetExtension(path), "");
- info.Url = path;
- #endif
- }
- info.Version = Application.version;
- table.InsertData(info);
- if (!ArtInfoContains(artid))
- {
- ArtInfoList.Add(info);
- }
- }
- }
- table.Save();
- }
- public bool ArtInfoContains(string artId)
- {
- for (int i = 0; i < ArtInfoList.Count; i++)
- {
- if (ArtInfoList[i].ArtId == artId)
- {
- return true;
- }
- }
- return false;
- }
- public bool IsInit { get => isInit; set => isInit = value; }
- public List<ArtInfo> ArtInfoList { get => artInfoList; set => artInfoList = value; }
- public Dictionary<string, ArtContainer> ContainerCache { get => containerCache; set => containerCache = value; }
- public Dictionary<string, ArtInfo> OtherArtInfoMap
- {
- get
- {
- if (otherArtInfoMap == null)
- {
- otherArtInfoMap = new Dictionary<string, ArtInfo>();
- }
- return otherArtInfoMap;
- }
- }
- /// <summary>
- /// 房间内物体的映射
- /// 房间id与对应的物体资源映射
- /// </summary>
- public void LoadModelEditBtn()
- {
- if (!ModelEditBtn)
- {
- var tmp = Resources.Load<ArtEditorContainer>(ModelEditBtnGroup);
- if (tmp)
- {
- ModelEditBtn = GameObject.Instantiate(tmp);
- GameNode.Instance.SetParent(ObjNode.WorldCanvas, ModelEditBtn.transform, Vector3.zero, Vector3.zero, Vector3.one * 0.001f, false);
- ModelEditBtn.gameObject.SetActive(false);
- }
- }
- }
- public ArtInfoMgr()
- {
- var artResList = Resources.LoadAll<ArtResConf>("ArtConf");
- resArtMap = new Dictionary<string, ArtResConf>();
- if (artResList != null)
- {
- for (int i = 0; i < artResList.Length; i++)
- {
- resArtMap.Add(artResList[i].name, artResList[i]);
- }
- }
- var containers = Resources.LoadAll<GameObject>("Container");
- if (containers != null)
- {
- for (int i = 0; i < containers.Length; i++)
- {
- var container = containers[i].GetComponent<ArtContainer>();
- if (container != null)
- {
- if (!artContainerMap.ContainsKey(container.GetArtType()))
- {
- List<ArtContainer> list = new List<ArtContainer>();
- list.Add(container);
- artContainerMap.Add(container.GetArtType(), list);
- }
- else
- {
- List<ArtContainer> list = artContainerMap[container.GetArtType()];
- list.Add(container);
- }
- }
- }
- }
- var components = Resources.LoadAll<GameObject>("Container/Component");
- if (components != null)
- {
- for (int i = 0; i < components.Length; i++)
- {
- var component = containers[i].GetComponent<ArtComponent>();
- if (component != null)
- {
- if (!artComponentMap.ContainsKey(containers[i].name))
- {
- artComponentMap.Add(containers[i].name, component);
- }
- }
- }
- }
- //if (BuildConfigMgr.Instance.IsInit)
- //{
- // ReadArtInfo(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.artInfoPath));
- //}
- else
- {
- UnityLog.Instance.LogError("配置文件丢失!");
- }
- WSHandler.Room.OnBroadcastGood += SyncGoods;
- ContainerSelect += OnContainerSelect;
- }
- public void ListenUser()
- {
- if (ShowViewMgr.Instance)
- {
- ShowViewMgr.Instance.OnUserChange -= OnUserChange;
- ShowViewMgr.Instance.OnUserChange += OnUserChange;
- }
- else
- {
- ShowViewMgr.InitComplte += () =>
- {
- if (ShowViewMgr.Instance)
- {
- ShowViewMgr.Instance.OnUserChange -= OnUserChange;
- ShowViewMgr.Instance.OnUserChange += OnUserChange;
- }
- };
- }
- }
- public void ListenPage()
- {
- if (ShowViewMgr.Instance)
- {
- ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
- ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
- }
- else
- {
- ShowViewMgr.InitComplte += () =>
- {
- if (ShowViewMgr.Instance)
- {
- ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
- ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
- }
- };
- }
- }
- private void OnContainerSelect(ArtContainer container, bool isSelect)
- {
- if (container.GetGoodsInfo().art_id == "ArtId_PlayerView")
- {
- return;
- }
- if (!ModelEditBtn)
- {
- LoadModelEditBtn();
- }
- if (ModelEditBtn)
- {
- if (ModelEditBtn.gameObject.activeSelf != isSelect)
- {
- ModelEditBtn.gameObject.SetActive(isSelect);
- }
- if (isSelect)
- {
- ModelEditBtn.SetTarget(container, false);
- }
- }
- if (ArtMoreEditor.Instance)
- {
- if (isSelect)
- {
- ArtMoreEditor.Instance.SetTarget(container, false);
- }
- }
- }
- /// <summary>
- /// 读取美术资源
- /// </summary>
- /// <param name="path"></param>
- private void ReadArtInfo(string path)
- {
- //languageIndex = SystemSettingMgr.Instance.settings.Language;
- tableName = typeof(ArtInfo).Name;
- #if UNITY_EDITOR || !UNITY_ANDROID
- table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
- OpenTable();
- #else
- string fullPath = Path.Combine(path, tableName + ".xml");
- GameNode.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<ArtInfo> packList) =>
- {
- InitPackage(packList);
- }));
- #endif
- }
- public void OpenLocalData()
- {
- ReadArtInfo(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.artInfoPath));
- }
- private void OpenTable()
- {
- if (!table.Open())
- {
- table.Create(tableName);
- UnityLog.Instance.Log("create table" + tableName);
- }
- var list = table.FindAllData<ArtInfo>();
- if (list != null)
- {
- for (int i = 0; i < list.Count; i++)
- {
- //list[i].ArtType = CommonMethod.getArtType((int)list[i].ArtType);
- }
- }
- //table.Save();
- //languagePackList = table.FindAllData<LanguagePackConf>();
- InitPackage(list);
- }
- /// <summary>
- /// 初始化包
- /// </summary>
- /// <param name="lists"></param>
- public void InitPackage(List<ArtInfo> lists)
- {
- #if UNITY_EDITOR
- for (int i = 0; i < lists.Count; i++)
- {
- UnityLog.Instance.Log("InitPackage:" + JsonConvert.SerializeObject(lists[i]));
- }
- //if (DataConfMgr<ArtInfo>.Instance.TableHelper == null)
- //{
- // DataConfMgr<ArtInfo>.Instance.OpenData(Application.persistentDataPath);
- //}
- //if (!DataConfMgr<ArtInfo>.Instance.TableHelper.isInit)
- //{
- // DataConfMgr<ArtInfo>.Instance.OpenTable("ArtId");
- //}
- //for (int i = 0; i < lists.Count; i++)
- //{
- // DataConfMgr<ArtInfo>.Instance.TableHelper.AddData(lists[i],false);
- //}
- //DataConfMgr<ArtInfo>.Instance.TableHelper.SaveData();
- #endif
- ArtInfoList = lists;
- if (ArtInfoList == null || ArtInfoList.Count < 1)
- {
- UnityLog.Instance.LogError("美术资源不存在!");
- #if UNITY_EDITOR
- //AddDefaultArt(ArtType.Model);
- //InitPackage(ArtInfoList);
- #endif
- return;
- }
- else
- {
- if (artInfoMap == null)
- {
- artInfoMap = new Dictionary<string, ArtInfo>();
- }
- if (artInfoTypeMap == null)
- {
- artInfoTypeMap = new Dictionary<ArtType, List<ArtInfo>>();
- }
- for (int i = 0; i < ArtInfoList.Count; i++)
- {
- if (!artInfoMap.ContainsKey(ArtInfoList[i].ArtId))
- {
- artInfoMap.Add(ArtInfoList[i].ArtId, ArtInfoList[i]);
- UnityLog.Instance.Log(ArtInfoList[i].Url, 3);
- }
- if (artInfoTypeMap.ContainsKey(ArtInfoList[i].ArtType))
- {
- List<ArtInfo> list = artInfoTypeMap[ArtInfoList[i].ArtType];
- if (list != null && !list.Contains(ArtInfoList[i]))
- {
- list.Add(ArtInfoList[i]);
- }
- }
- else
- {
- List<ArtInfo> list = new List<ArtInfo>();
- list.Add(ArtInfoList[i]);
- artInfoTypeMap.Add(ArtInfoList[i].ArtType, list);
- }
- }
- }
- IsInit = true;
- InitComplete?.Invoke();
- }
- /// <summary>
- /// 添加Unity基本资源到文本
- /// </summary>
- public void AddDefaultArt(ArtType artType)
- {
- if (ArtInfoList == null)
- {
- ArtInfoList = new List<ArtInfo>();
- }
- if (artType == ArtType.Model)
- {
- foreach (PrimitiveType primitive in Enum.GetValues(typeof(PrimitiveType)))
- {
- ArtInfo info = new ArtInfo();
- info.ArtId = "ArtId_" + (int)primitive;
- info.ArtName = primitive.ToString();
- info.ArtType = ArtType.Model;
- info.Distance = 1f;
- ///Icon
- info.Icon = "PrimitiveType/" + primitive.ToString();
- info.InstaceType = InstaceType.UnityBase;
- info.Size = 1;
- info.Url = "" + (int)primitive;
- info.Version = Application.unityVersion;
- table.InsertData(info);
- ArtInfoList.Add(info);
- }
- }
- else
- {
- ArtInfo info = new ArtInfo();
- info.ArtId = "ArtId_GameObject";
- info.ArtName = "GameObject";
- info.ArtType = ArtType.Prefab;
- info.Distance = 1f;
- ///Icon
- info.Icon = "";
- info.InstaceType = InstaceType.UnityBase;
- info.Size = 1;
- info.Url = "New";
- info.Version = Application.unityVersion;
- table.InsertData(info);
- ArtInfoList.Add(info);
- }
- table.Save();
- }
- /// <summary>
- /// 根据资源id获取对应的资源
- /// </summary>
- /// <param name="artId"></param>
- /// <returns></returns>
- public ArtInfo GetArtInfo(string artId)
- {
- if (IsInit && artInfoMap.ContainsKey(artId))
- {
- return artInfoMap[artId];
- }
- return null;
- }
- /// <summary>
- /// 根据资源id获取对应的资源
- /// </summary>
- /// <param name="artId"></param>
- /// <returns></returns>
- public List<ArtInfo> GetArtInfoList(ArtType artType)
- {
- if (IsInit)//
- {
- if (artInfoTypeMap.ContainsKey(artType))
- {
- return artInfoTypeMap[artType];
- }
- List<ArtInfo> tmp = new List<ArtInfo>();
- foreach (var item in artInfoTypeMap)
- {
- ///包含此key
- if ((item.Key & artType) == item.Key)
- {
- tmp.AddRange(item.Value);
- }
- }
- artInfoTypeMap.Add(artType, tmp);
- return tmp;
- }
- return null;
- }
- ///// <summary>
- ///// 根据资源id获取对应的资源
- ///// </summary>
- ///// <param name="artId"></param>
- ///// <returns></returns>
- //public List<ArtInfo> GetArtInfoList(ArtType[] artType)
- //{
- // if (IsInit)//
- // {
- // List<ArtInfo> ArtInfoList = new List<ArtInfo>();
- // for (int i = 0; i < artType.Length; i++)
- // {
- // if (artInfoTypeMap.ContainsKey(artType[i]))
- // {
- // ArtInfoList.AddRange(artInfoTypeMap[artType[i]]);
- // }
- // }
- // return ArtInfoList;
- // }
- // return null;
- //}
- /// <summary>
- /// 根据资源id获取对应的资源
- /// </summary>
- /// <param name="artId"></param>
- /// <returns></returns>
- public List<ArtInfo> GetArtInfoListSingle(ArtType artType)
- {
- if (IsInit)//
- {
- return artInfoTypeMap[artType];
- }
- return null;
- }
- /// <summary>
- /// 创建一个资源ArtHandler,注意内存回收
- /// </summary>
- /// <returns></returns>
- public ArtHandler CreateArtHandler(ArtInfo art, string containerName = "")
- {
- if (string.IsNullOrEmpty(containerName))
- {
- containerName = art.ContainerName;
- }
- if (artHandlerMap.ContainsKey(art))
- {
- return artHandlerMap[art];
- }
- ArtHandler handler = new ArtHandler();
- handler.SetData(art, containerName);
- artHandlerMap.Add(art, handler);
- return handler;
- }
- /// <summary>
- /// 删除资源帮助类ArtHandler
- /// </summary>
- /// <param name="art"></param>
- public void DestrorArtHandler(ArtInfo art)
- {
- if (artHandlerMap.ContainsKey(art))
- {
- var handler = artHandlerMap[art];
- handler = null;
- artHandlerMap.Remove(art);
- }
- }
- /// <summary>
- /// 实例化组件
- /// </summary>
- /// <param name="componentName"></param>
- /// <returns></returns>
- public ArtComponent InstanceComponent(string componentName)
- {
- if (artComponentMap != null && artComponentMap.ContainsKey(componentName))
- {
- ArtComponent component = artComponentMap[componentName];
- var tmp = GameObject.Instantiate(component.GetInstace());
- return tmp.GetComponent<ArtComponent>();
- }
- return null;
- }
- /// <summary>
- /// 实例化出一个容器
- /// </summary>
- /// <param name="artType"></param>
- /// <returns></returns>
- public ArtContainer CreateContainer(ArtType artType, string containerName = "")
- {
- if (artContainerMap != null && artContainerMap.ContainsKey(artType))
- {
- var containerList = artContainerMap[artType];
- if (containerList != null)
- {
- ArtContainer container = null;
- if (containerList.Count == 1)
- {
- container = containerList[0];
- }
- else
- {
- for (int i = 0; i < containerList.Count; i++)
- {
- if (containerList[i].GetInstace().name == containerName)
- {
- container = containerList[i];
- break;
- }
- }
- }
- if (container != null)
- {
- var tmp = GameObject.Instantiate(container.GetInstace());
- return tmp.GetComponent<ArtContainer>();
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 获取对应的配置文件
- /// </summary>
- /// <param name="resName"></param>
- /// <returns></returns>
- public ArtResConf GetResConf(string resName)
- {
- if (resArtMap.ContainsKey(resName))
- {
- return resArtMap[resName];
- }
- UnityLog.Instance.LogError(resName + " is not at resArtMap");
- return null;
- }
- /// <summary>
- /// 物品的自增id设计
- /// </summary>
- /// <param name="info"></param>
- public int GetGoodsId(ArtInfo info)
- {
- int id = 1;
- if (goodsIdMap.ContainsKey(info))
- {
- id = goodsIdMap[info] + 1;
- goodsIdMap[info] = id;
- }
- else
- {
- goodsIdMap.Add(info, id);
- }
- return id;
- }
- /// <summary>
- /// 同步物体:
- /// 创建物体时,接收到广播消息时的数据同步都调用此方法
- /// </summary>
- /// <param name="rid">用户id,物体的创建者</param>
- /// <param name="art_id">美术资源id</param>
- /// <param name="goods_name">物体名称</param>
- /// <param name="goods_extended">物体额外信息</param>
- /// <param name="id">物体id</param>
- /// <param name="goods_info">物体的姿态信息</param>
- public void SyncGoods(int rid, int status, string art_id, string goods_name, string goods_extended, int id, string goods_info)
- {
- serverCacheData.rid = rid;
- serverCacheData.art_id = art_id;
- serverCacheData.goods_name = goods_name;
- serverCacheData.goods_extended = goods_extended;
- serverCacheData.id = id;
- serverCacheData.goods_info = goods_info;
- serverCacheData.status = status;
- SyncGoods(serverCacheData);
- }
- public void SyncGoods(List<GoodsInfo> goodDataList)
- {
- if (goodDataList != null)
- {
- for (int i = 0; i < goodDataList.Count; i++)
- {
- SyncGoods(goodDataList[i]);
- }
- }
- }
- /// <summary>
- /// 同步物体数据
- /// 服务器同步或者翻页时同步,服务器同步时开始缓存数据
- /// 翻页时同步不添加缓存数据
- /// </summary>
- /// <param name="goodData"></param>
- /// <param name="isServer"></param>
- public void SyncGoods(GoodsInfo goodData, bool isServer = true)
- {
- if (GameNode.Instance.GoodsContainerMap == null)
- {
- GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
- }
- ArtContainer container = null;
- bool isCreate = false;
- if (SynUser(goodData))
- {
- container = GameNode.Instance.UserMap[goodData.art_id].Container;
- }
- else if (goodData.id != 0)
- {
- ///先检查此数据是否在此页面
- ///如果数据不在当前页,则只缓存起来
- ///如果在当前页,则缓存后实例化进行同步
- ///如果不是服务器的数据,仅同步设置,不缓存数据
- if (isServer && goodData.scene_id > 0)
- {
- if (!GameNode.Instance.PageGoods.ContainsKey(goodData.id))
- {
- GameNode.Instance.PageGoods.Add(goodData.id, goodData);
- }
- else
- {
- GameNode.Instance.PageGoods[goodData.id] = goodData;
- }
- if (!GameNode.Instance.PageGoodsId.ContainsKey(goodData.scene_id))
- {
- var list = new List<int>();
- list.Add(goodData.id);
- if (goodData.art_id != "ArtId_GameObject")
- {
- GameNode.Instance.AddGoodData(goodData);
- }
- GameNode.Instance.PageGoodsId.Add(goodData.scene_id, list);
- }
- else
- {
- var list = GameNode.Instance.PageGoodsId[goodData.scene_id];
- if (list == null)
- {
- list = new List<int>();
- }
- if (!list.Contains(goodData.id))
- {
- list.Add(goodData.id);
- if (goodData.art_id != "ArtId_GameObject")
- {
- GameNode.Instance.AddGoodData(goodData);
- }
- }
- }
- if (goodData.scene_id != CommonMethod.currentScene.Id)
- {
- return;
- }
- }
- ///对象已存在,进行同步
- if (GameNode.Instance.GoodsContainerMap.ContainsKey(goodData.id))
- {
- container = GameNode.Instance.GoodsContainerMap[goodData.id];
- }
- else
- {
- //Debug.LogError(" 请求物品信息===:" + goodData.art_id);
- ArtInfo info = GetArtInfo(goodData.art_id);
- if (info == null)
- {
- if (OtherArtInfoMap.ContainsKey(goodData.art_id))
- {
- info = OtherArtInfoMap[goodData.art_id];
- }
- else
- {
- HomeProxy.Instance.GetArtInfoDetailByArtID(goodData.art_id, (artInfo) =>
- {
- //Debug.LogWarning("回包信息 goodData.art_id "+ goodData.art_id+" "+artInfo.AID+"QQQ" + artInfo.ArtId + "UUU" + artInfo.Url + "RRR" + artInfo.ArtType);
- if (goodData.art_id == artInfo.ArtId)
- {
- //Debug.LogWarning("确认回包信息");
- if (!OtherArtInfoMap.ContainsKey(goodData.art_id))
- {
- info = artInfo;
- if (info != null)
- {
- //Debug.LogWarning($"添加数据到字典 key:{goodData.art_id} value:{info.ArtId} {info.ArtName} {info.ArtType}");
- OtherArtInfoMap.Add(goodData.art_id, info);
- SyncGoods(goodData, isServer);
- //UnityLog.Instance.LogError("创建完毕 find artinfo:" + goodData.art_id + "info" + info.ArtId);
- return;
- }
- else
- {
- UnityLog.Instance.LogError("Error find artinfo:" + goodData.art_id);
- }
- }
- }
- });
-
- }
- }
- if (info != null)
- {
- ///容器缓存
- if (ContainerCache.ContainsKey(goodData.art_id))
- {
- container = ContainerCache[goodData.art_id];
- ContainerCache.Remove(goodData.art_id);
- }
- else
- {
- ArtHandler handler = CreateArtHandler(info);
- //Debug.LogError($"创建 info aid {info.AID } artid {info.ArtId} InstaceType {info.InstaceType} handler {handler.Instancer}");
- container = handler.CreateArt();
- }
- GameNode.Instance.GoodsContainerMap.Add(goodData.id, container);
- isCreate = true;
- }
- else
- {
- UnityLog.Instance.LogError(goodData.art_id);
- }
- }
- }
- try
- {
- if (container != null && container.GetInstace())
- {
- container.TransferSyn(goodData, goodData.status == (int)TransferState.Doing);
- if (isCreate)
- {
- ContainerCreate?.Invoke(container);
- }
- }
- }
- catch
- {
- UnityLog.Instance.LogError("SyncGoods Error:" + JsonConvert.SerializeObject(goodData));
- ///某个物体可能被删掉了
- ///此种情况不在处理
- }
- }
- //Dictionary<string, string> form;
- //Dictionary<string, string> header;
- ///// <summary>
- ///// 读取资源信息
- ///// </summary>
- ///// <returns></returns>
- //public IEnumerator ReqOtherArtInfo(string artId, Action<ArtInfo> onReadComplete)
- //{
- // if (header == null)
- // {
- // header = new Dictionary<string, string>();
- // header.Add("Content-Type", "application/x-www-form-urlencoded");
- // }
- // if (form == null)
- // {
- // form = new Dictionary<string, string>();
- // form.Add("action", "");
- // }
- // yield return 0;
- //}
- /// <summary>
- /// 同步用户
- /// </summary>
- /// <param name="goodData"></param>
- /// <returns></returns>
- private bool SynUser(GoodsInfo goodData)
- {
- if (GameNode.Instance.UserMap == null)
- {
- GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
- }
- if (string.IsNullOrEmpty(goodData.art_id))
- {
- return false;
- }
- return GameNode.Instance.UserMap.ContainsKey(goodData.art_id);
- }
- /// <summary>
- /// 删除物体
- /// 可能由用户主动删除,主动删除传参true.删除相关的数据缓存
- /// 翻页时是被动删除。被动删除不移除相关的数据缓存
- /// </summary>
- /// <param name="id">物体的id</param>
- /// <param name="isRealDel">是否真实删除,默认真实删除</param>
- public void GoodsDelSync(int id, bool isRealDel = true)
- {
- if (GameNode.Instance.GoodsContainerMap == null)
- {
- GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
- }
- ArtContainer container = null;
- ///对象已存在,进行同步
- if (GameNode.Instance.GoodsContainerMap.ContainsKey(id))
- {
- container = GameNode.Instance.GoodsContainerMap[id];
- //if (ModelEditBtn)
- //{
- // ModelEditBtn.SetTarget(container, true);
- //}
- if (ArtMoreEditor.Instance)
- {
- ArtMoreEditor.Instance.SetTarget(container, true);
- }
- ContainerDel?.Invoke(container);
- if (container != null)
- {
- container.DestroyArt();
- }
- GameNode.Instance.GoodsContainerMap.Remove(id);
- }
- ///真实删除
- if (isRealDel)
- {
- ///删除物体的id
- ///先查找物体id对应的Goods
- if (GameNode.Instance.PageGoods.ContainsKey(id))
- {
- ///获得Goods
- var good = GameNode.Instance.PageGoods[id];
- ///查找页对应的物体的id
- if (GameNode.Instance.PageGoodsId.ContainsKey(good.scene_id))
- {
- ///获取页对应的id列表,并
- var list = GameNode.Instance.PageGoodsId[good.scene_id];
- if (list != null && list.Contains(id))
- {
- list.Remove(id);
- if (good.art_id != "ArtId_GameObject")
- {
- GameNode.Instance.RemoveGoodData(good);
- }
- }
- }
- GameNode.Instance.PageGoods.Remove(id);
- ContainerDel?.Invoke(null);
- }
- }
- }
- public void SendGoodsTransfer(GoodsInfo info)
- {
- goodsDataList.Add(info);
- WSHandler.Room.BroadcastGood(goodsDataList);
- goodsDataList.Clear();
- }
- public void SendTransfer(GoodsInfo info)
- {
- goodsDataList.Add(info);
- WSHandler.Room.TransmitGood(goodsDataList);
- goodsDataList.Clear();
- }
- /// <summary>
- /// 当新用户进入时的事件
- /// </summary>
- /// <param name="userList"></param>
- public void OnUserChange(List<Peer> userList, bool isJoin)
- {
- if (GameNode.Instance.UserMap == null)
- {
- GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
- }
- if (userList != null)
- {
- for (int i = 0; i < userList.Count; i++)
- {
- if (GameNode.Instance.UserMap.ContainsKey(userList[i].PeerId))
- {
- if (isJoin)
- {
- continue;
- }
- else
- {
- var container = GameNode.Instance.UserMap[userList[i].PeerId];
- container.StopSyn();
- container.DestroyArt();
- UserChange?.Invoke(userList[i].PeerId, false);
- GameNode.Instance.UserMap.Remove(userList[i].PeerId);
- }
- }
- else
- {
- if (isJoin)
- {
- ArtInfo info = GetArtInfo(PlayerContainer.userArtId);
- if (info != null)
- {
- //string cName = info.ContainerName;
- //if (string.IsNullOrEmpty(cName))
- //{
- // cName = PlayerContainer.containerName;
- //}
- //ArtHandler handler = CreateArtHandler(info, PlayerContainer.containerName);
- ArtHandler handler = CreateArtHandler(info);
- var container = handler.CreateArt();
- if (container != null)
- {
- if (container.ArtComponent != null && container.ArtComponent.GetInstace())
- {
- if (container.ArtComponent is PlayerContainer)
- {
- (container.ArtComponent as PlayerContainer).SetPeer(userList[i]);
- GameNode.Instance.UserMap.Add(userList[i].PeerId, container.ArtComponent as PlayerContainer);
- UserChange?.Invoke(userList[i].PeerId, true);
- }
- }
- }
- }
- else
- {
- UnityLog.Instance.LogError(PlayerContainer.userArtId + " cant find art");
- }
- }
- else
- {
- continue;
- }
- }
- }
- }
- }
- /// <summary>
- /// 删除场景的物体
- /// </summary>
- /// <param name="id"></param>
- /// <param name="isOnlyScene"></param>
- public void DelGoodsById(int id)
- {
- bool isCanDel = true;
- if (GameNode.Instance.PageGoods.ContainsKey(id))
- {
- var goods = GameNode.Instance.PageGoods[id];
- if (goods.scene_id == CommonMethod.currentScene.Id)
- {
- isCanDel = true;
- }
- }
- if (isCanDel)
- {
- WSHandler.Room.DeleteGood(new int[] { id });
- }
- }
- public void DelGoodsByIds(List<int> ids)
- {
- List<int> list = new List<int>();
- for (int i = 0; i < ids.Count; i++)
- {
- int id = ids[i];
- if (GameNode.Instance.PageGoods.ContainsKey(id))
- {
- var goods = GameNode.Instance.PageGoods[id];
- if (goods.scene_id == CommonMethod.currentScene.Id)
- {
- list.Add(id);
- }
- }
- }
- if (list.Count > 0)
- {
- WSHandler.Room.DeleteGood(list.ToArray());
- }
- }
- /// <summary>
- /// 翻页的数据处理
- /// 翻页时注意,删除旧的数据
- /// 同步新的数据
- /// </summary>
- /// <param name="all"></param>
- /// <param name="current"></param>
- public void OnChangePage(int all, int current)
- {
- if (GameNode.Instance)
- {
- if (GameNode.Instance.DefaultPageIndex != current)
- {
- if (GameNode.Instance.DefaultPageIndex >= 0)
- {
- if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
- {
- var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
- for (int i = 0; i < list.Count; i++)
- {
- GoodsDelSync(list[i], false);
- }
- }
- }
- if (ModelEditBtn)
- {
- ModelEditBtn.gameObject.SetActive(false);
- }
- GameNode.Instance.DefaultPageIndex = current;
- if (GameNode.Instance.DefaultPageIndex >= 0)
- {
- if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
- {
- var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
- for (int i = 0; i < list.Count; i++)
- {
- if (GameNode.Instance.PageGoods.ContainsKey(list[i]))
- {
- SyncGoods(GameNode.Instance.PageGoods[list[i]], false);
- }
- }
- }
- }
- }
- }
- }
- }
- }
|