GameNode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using ShadowStudio.Model;
  2. using ShadowStudio.Util;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. namespace XRTool.Util
  9. {
  10. public enum ObjNode
  11. {
  12. Null = -1,
  13. UICanvas = 0,
  14. WorldCanvas = 1,
  15. World = 2,
  16. }
  17. /// <summary>
  18. /// 游戏节点管理器
  19. /// </summary>
  20. public class GameNode : UnitySingleton<GameNode>
  21. {
  22. /// <summary>
  23. /// 节点map,包含local,server下的所有节点信息
  24. /// </summary>
  25. private Dictionary<string, Dictionary<string, Transform>> nodeDic;
  26. private string[] nodeRoot = new string[] { "Local", "Server" };
  27. private string[] nodes = new string[] { "UICanvas", "WorldCanvas", "World" };
  28. public Dictionary<string, Dictionary<string, Transform>> NodeDic { get => nodeDic; set => nodeDic = value; }
  29. public string[] NodeRoot { get => nodeRoot; }
  30. public string[] Nodes { get => nodes; }
  31. #if UNITY_EDITOR
  32. public bool isLoadOnEditor = true;
  33. #endif
  34. /// <summary>
  35. /// 物体id和容器的集合
  36. /// 注意进入房间时开始同步,退出房间时要清除缓存
  37. /// </summary>
  38. private Dictionary<int, ArtContainer> goodsContainerMap;
  39. /// <summary>
  40. /// 缓存的用户信息
  41. /// </summary>
  42. private Dictionary<string, PlayerContainer> userMap;
  43. /// <summary>
  44. /// 对应页的物体的id
  45. /// 每一个页代表一个节点数据
  46. /// </summary>
  47. private Dictionary<int, List<int>> pageGoodsId;
  48. /// <summary>
  49. /// 对应同步物体数据的id
  50. /// 方便索引查找
  51. /// </summary>
  52. private Dictionary<int, GoodsInfo> pageGoods;
  53. private int defaultPageIndex = 0;
  54. public Transform LocalUICanvas
  55. {
  56. get
  57. {
  58. return NodeDic[NodeRoot[0]][nodes[0]];
  59. }
  60. }
  61. public Transform ServerUICanvas
  62. {
  63. get
  64. {
  65. return NodeDic[NodeRoot[1]][nodes[0]];
  66. }
  67. }
  68. public Transform LocalWorldCanvas
  69. {
  70. get
  71. {
  72. return NodeDic[NodeRoot[0]][nodes[1]];
  73. }
  74. }
  75. public Transform ServerWorldCanvas
  76. {
  77. get
  78. {
  79. return NodeDic[NodeRoot[1]][nodes[1]];
  80. }
  81. }
  82. public Transform LocalWorld
  83. {
  84. get
  85. {
  86. return NodeDic[NodeRoot[0]][nodes[2]];
  87. }
  88. }
  89. public Transform ServerWorld
  90. {
  91. get
  92. {
  93. return NodeDic[NodeRoot[1]][nodes[2]];
  94. }
  95. }
  96. public Dictionary<int, ArtContainer> GoodsContainerMap { get => goodsContainerMap; set => goodsContainerMap = value; }
  97. public Dictionary<string, PlayerContainer> UserMap { get => userMap; set => userMap = value; }
  98. public Dictionary<int, GoodsInfo> PageGoods
  99. {
  100. get
  101. {
  102. if (pageGoods == null)
  103. {
  104. pageGoods = new Dictionary<int, GoodsInfo>();
  105. }
  106. return pageGoods;
  107. }
  108. }
  109. /// <summary>
  110. /// 缓存每一页的物体的id集合
  111. /// 当发生翻页的时候,清理当前页的缓存信息,但是此列表数据不移除:但是如果保证删除时的同步功能?
  112. /// </summary>
  113. public Dictionary<int, List<int>> PageGoodsId
  114. {
  115. get
  116. {
  117. if (pageGoodsId == null)
  118. {
  119. pageGoodsId = new Dictionary<int, List<int>>();
  120. }
  121. return pageGoodsId;
  122. }
  123. }
  124. public Dictionary<int, int> PageGoodsIdCount = new Dictionary<int, int>();
  125. public int DefaultPageIndex { get => defaultPageIndex; set => defaultPageIndex = value; }
  126. protected override void Awake()
  127. {
  128. base.Awake();
  129. if (NodeDic == null)
  130. {
  131. InitNode();
  132. }
  133. }
  134. /// <summary>
  135. /// 检测某节点是否是系统节点
  136. /// </summary>
  137. /// <param name="nodeName"></param>
  138. /// <returns></returns>
  139. public bool IsSystemNode(string nodeName)
  140. {
  141. foreach (var item in NodeDic)
  142. {
  143. if (item.Value.ContainsKey(nodeName))
  144. {
  145. Transform parent = item.Value[nodeName];
  146. if (parent == transform || item.Value.ContainsKey(parent.name))
  147. {
  148. return true;
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. /// <summary>
  155. /// 初始化节点
  156. /// </summary>
  157. private void InitNode()
  158. {
  159. if (NodeDic == null)
  160. {
  161. NodeDic = new Dictionary<string, Dictionary<string, Transform>>();
  162. }
  163. for (int i = 0; i < NodeRoot.Length; i++)
  164. {
  165. Dictionary<string, Transform> dic = null;
  166. if (NodeDic.ContainsKey(NodeRoot[i]))
  167. {
  168. dic = NodeDic[NodeRoot[i]];
  169. }
  170. else
  171. {
  172. dic = new Dictionary<string, Transform>();
  173. NodeDic.Add(NodeRoot[i], dic);
  174. dic.Add(NodeRoot[i], transform.Find(NodeRoot[i]));
  175. }
  176. ///
  177. for (int j = 0; j < Nodes.Length; j++)
  178. {
  179. if (!dic.ContainsKey(Nodes[j]))
  180. {
  181. dic.Add(Nodes[j], dic[NodeRoot[i]].Find(Nodes[j]));
  182. }
  183. }
  184. }
  185. //foreach (var item in NodeDic)
  186. //{
  187. // foreach (var trans in item.Value)
  188. // {
  189. // print(trans.Value.parent.name + "/" + trans.Value.name);
  190. // }
  191. //}
  192. }
  193. /// <summary>
  194. /// 添加一个节点对象
  195. /// </summary>
  196. public void AddNode(Transform node, string nodeName, int index)
  197. {
  198. if (index >= 0 && index < nodeRoot.Length)
  199. {
  200. Dictionary<string, Transform> dic = NodeDic[NodeRoot[index]];
  201. if (!dic.ContainsKey(nodeName))
  202. {
  203. dic.Add(nodeName, node);
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// 设置节点
  209. /// </summary>
  210. public void SetParent(string parentName, Transform node, Vector3 position, Vector3 angle, Vector3 scale, int index)
  211. {
  212. Transform parent = FindNode(parentName, index);
  213. if (parent)
  214. {
  215. node.SetParent(parent);
  216. node.localPosition = position;
  217. node.localEulerAngles = angle;
  218. node.localScale = scale;
  219. }
  220. }
  221. public Vector3 LocalPosition(ObjNode node, Vector3 worldPos, int index = 0)
  222. {
  223. Transform parent = FindNode(node.ToString(), index);
  224. if (parent)
  225. {
  226. //print(parent);
  227. return parent.InverseTransformPoint(worldPos);
  228. }
  229. return worldPos;
  230. }
  231. /// <summary>
  232. /// 设置节点
  233. /// </summary>
  234. public void SetParent(string parentName, Transform node, Vector3 position, Vector3 angle, Vector3 scale, bool isLocal = true)
  235. {
  236. SetParent(parentName, node, position, angle, scale, isLocal ? 0 : 1);
  237. }
  238. /// <summary>
  239. /// 设置节点
  240. /// </summary>
  241. public void SetParent(ObjNode objNode, Transform node, Vector3 position, Vector3 angle, Vector3 scale, bool isLocal = true)
  242. {
  243. SetParent(objNode.ToString(), node, position, angle, scale, isLocal);
  244. }
  245. /// <summary>
  246. /// 查找一个节点
  247. /// </summary>
  248. /// <param name="parentName"></param>
  249. /// <param name="index"></param>
  250. /// <returns></returns>
  251. public Transform FindNode(string parentName, int index)
  252. {
  253. if (NodeDic == null)
  254. {
  255. InitNode();
  256. }
  257. if (index >= 0 && index < nodeRoot.Length && NodeDic[nodeRoot[index]].ContainsKey(parentName))
  258. {
  259. return NodeDic[nodeRoot[index]][parentName];
  260. }
  261. return null;
  262. }
  263. private void Start()
  264. {
  265. if (!GameSession.InitSuccess)
  266. {
  267. GameSession.Instance.SessionInit += () =>
  268. {
  269. LocalWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
  270. ServerWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
  271. };
  272. }
  273. else
  274. {
  275. LocalWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
  276. ServerWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
  277. }
  278. #if UNITY_EDITOR
  279. if (isLoadOnEditor)
  280. {
  281. SceneConfMgr.Instance.OnSceneLoaded(SceneManager.GetActiveScene());
  282. }
  283. #else
  284. SceneConfMgr.Instance.OnSceneLoaded(SceneManager.GetActiveScene());
  285. #endif
  286. }
  287. protected override void OnDestroy()
  288. {
  289. base.OnDestroy();
  290. if (this.NodeDic != null)
  291. {
  292. this.NodeDic.Clear();
  293. }
  294. if (this.GoodsContainerMap != null)
  295. {
  296. this.GoodsContainerMap.Clear();
  297. }
  298. this.NodeDic = null;
  299. this.GoodsContainerMap = null;
  300. this.UserMap = null;
  301. }
  302. /// <summary>
  303. /// 翻页的算法实现
  304. /// </summary>
  305. /// <param name="all"></param>
  306. /// <param name="current"></param>
  307. public void OnChangePage(int all, int current)
  308. {
  309. }
  310. public void AddGoodData(GoodsInfo goodData)
  311. {
  312. if (!PageGoodsIdCount.ContainsKey(goodData.scene_id))
  313. {
  314. PageGoodsIdCount.Add(goodData.scene_id, 0);
  315. }
  316. PageGoodsIdCount[goodData.scene_id]++;// = count;
  317. }
  318. public void RemoveGoodData(GoodsInfo goodData)
  319. {
  320. if (PageGoodsIdCount.ContainsKey(goodData.scene_id))
  321. {
  322. PageGoodsIdCount[goodData.scene_id]--;
  323. }
  324. }
  325. }
  326. }