123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- using ShadowStudio.Model;
- using ShadowStudio.Util;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- namespace XRTool.Util
- {
- public enum ObjNode
- {
- Null = -1,
- UICanvas = 0,
- WorldCanvas = 1,
- World = 2,
- }
- /// <summary>
- /// 游戏节点管理器
- /// </summary>
- public class GameNode : UnitySingleton<GameNode>
- {
- /// <summary>
- /// 节点map,包含local,server下的所有节点信息
- /// </summary>
- private Dictionary<string, Dictionary<string, Transform>> nodeDic;
- private string[] nodeRoot = new string[] { "Local", "Server" };
- private string[] nodes = new string[] { "UICanvas", "WorldCanvas", "World" };
- public Dictionary<string, Dictionary<string, Transform>> NodeDic { get => nodeDic; set => nodeDic = value; }
- public string[] NodeRoot { get => nodeRoot; }
- public string[] Nodes { get => nodes; }
- #if UNITY_EDITOR
- public bool isLoadOnEditor = true;
- #endif
- /// <summary>
- /// 物体id和容器的集合
- /// 注意进入房间时开始同步,退出房间时要清除缓存
- /// </summary>
- private Dictionary<int, ArtContainer> goodsContainerMap;
- /// <summary>
- /// 缓存的用户信息
- /// </summary>
- private Dictionary<string, PlayerContainer> userMap;
- /// <summary>
- /// 对应页的物体的id
- /// 每一个页代表一个节点数据
- /// </summary>
- private Dictionary<int, List<int>> pageGoodsId;
- /// <summary>
- /// 对应同步物体数据的id
- /// 方便索引查找
- /// </summary>
- private Dictionary<int, GoodsInfo> pageGoods;
- private int defaultPageIndex = 0;
- public Transform LocalUICanvas
- {
- get
- {
- return NodeDic[NodeRoot[0]][nodes[0]];
- }
- }
- public Transform ServerUICanvas
- {
- get
- {
- return NodeDic[NodeRoot[1]][nodes[0]];
- }
- }
- public Transform LocalWorldCanvas
- {
- get
- {
- return NodeDic[NodeRoot[0]][nodes[1]];
- }
- }
- public Transform ServerWorldCanvas
- {
- get
- {
- return NodeDic[NodeRoot[1]][nodes[1]];
- }
- }
- public Transform LocalWorld
- {
- get
- {
- return NodeDic[NodeRoot[0]][nodes[2]];
- }
- }
- public Transform ServerWorld
- {
- get
- {
- return NodeDic[NodeRoot[1]][nodes[2]];
- }
- }
- public Dictionary<int, ArtContainer> GoodsContainerMap { get => goodsContainerMap; set => goodsContainerMap = value; }
- public Dictionary<string, PlayerContainer> UserMap { get => userMap; set => userMap = value; }
- public Dictionary<int, GoodsInfo> PageGoods
- {
- get
- {
- if (pageGoods == null)
- {
- pageGoods = new Dictionary<int, GoodsInfo>();
- }
- return pageGoods;
- }
- }
- /// <summary>
- /// 缓存每一页的物体的id集合
- /// 当发生翻页的时候,清理当前页的缓存信息,但是此列表数据不移除:但是如果保证删除时的同步功能?
- /// </summary>
- public Dictionary<int, List<int>> PageGoodsId
- {
- get
- {
- if (pageGoodsId == null)
- {
- pageGoodsId = new Dictionary<int, List<int>>();
- }
- return pageGoodsId;
- }
- }
- public Dictionary<int, int> PageGoodsIdCount = new Dictionary<int, int>();
- public int DefaultPageIndex { get => defaultPageIndex; set => defaultPageIndex = value; }
- protected override void Awake()
- {
- base.Awake();
- if (NodeDic == null)
- {
- InitNode();
- }
- }
- /// <summary>
- /// 检测某节点是否是系统节点
- /// </summary>
- /// <param name="nodeName"></param>
- /// <returns></returns>
- public bool IsSystemNode(string nodeName)
- {
- foreach (var item in NodeDic)
- {
- if (item.Value.ContainsKey(nodeName))
- {
- Transform parent = item.Value[nodeName];
- if (parent == transform || item.Value.ContainsKey(parent.name))
- {
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 初始化节点
- /// </summary>
- private void InitNode()
- {
- if (NodeDic == null)
- {
- NodeDic = new Dictionary<string, Dictionary<string, Transform>>();
- }
- for (int i = 0; i < NodeRoot.Length; i++)
- {
- Dictionary<string, Transform> dic = null;
- if (NodeDic.ContainsKey(NodeRoot[i]))
- {
- dic = NodeDic[NodeRoot[i]];
- }
- else
- {
- dic = new Dictionary<string, Transform>();
- NodeDic.Add(NodeRoot[i], dic);
- dic.Add(NodeRoot[i], transform.Find(NodeRoot[i]));
- }
- ///
- for (int j = 0; j < Nodes.Length; j++)
- {
- if (!dic.ContainsKey(Nodes[j]))
- {
- dic.Add(Nodes[j], dic[NodeRoot[i]].Find(Nodes[j]));
- }
- }
- }
- //foreach (var item in NodeDic)
- //{
- // foreach (var trans in item.Value)
- // {
- // print(trans.Value.parent.name + "/" + trans.Value.name);
- // }
- //}
- }
- /// <summary>
- /// 添加一个节点对象
- /// </summary>
- public void AddNode(Transform node, string nodeName, int index)
- {
- if (index >= 0 && index < nodeRoot.Length)
- {
- Dictionary<string, Transform> dic = NodeDic[NodeRoot[index]];
- if (!dic.ContainsKey(nodeName))
- {
- dic.Add(nodeName, node);
- }
- }
- }
- /// <summary>
- /// 设置节点
- /// </summary>
- public void SetParent(string parentName, Transform node, Vector3 position, Vector3 angle, Vector3 scale, int index)
- {
- Transform parent = FindNode(parentName, index);
- if (parent)
- {
- node.SetParent(parent);
- node.localPosition = position;
- node.localEulerAngles = angle;
- node.localScale = scale;
- }
- }
- public Vector3 LocalPosition(ObjNode node, Vector3 worldPos, int index = 0)
- {
- Transform parent = FindNode(node.ToString(), index);
- if (parent)
- {
- //print(parent);
- return parent.InverseTransformPoint(worldPos);
- }
- return worldPos;
- }
- /// <summary>
- /// 设置节点
- /// </summary>
- public void SetParent(string parentName, Transform node, Vector3 position, Vector3 angle, Vector3 scale, bool isLocal = true)
- {
- SetParent(parentName, node, position, angle, scale, isLocal ? 0 : 1);
- }
- /// <summary>
- /// 设置节点
- /// </summary>
- public void SetParent(ObjNode objNode, Transform node, Vector3 position, Vector3 angle, Vector3 scale, bool isLocal = true)
- {
- SetParent(objNode.ToString(), node, position, angle, scale, isLocal);
- }
- /// <summary>
- /// 查找一个节点
- /// </summary>
- /// <param name="parentName"></param>
- /// <param name="index"></param>
- /// <returns></returns>
- public Transform FindNode(string parentName, int index)
- {
- if (NodeDic == null)
- {
- InitNode();
- }
- if (index >= 0 && index < nodeRoot.Length && NodeDic[nodeRoot[index]].ContainsKey(parentName))
- {
- return NodeDic[nodeRoot[index]][parentName];
- }
- return null;
- }
- private void Start()
- {
- if (!GameSession.InitSuccess)
- {
- GameSession.Instance.SessionInit += () =>
- {
- LocalWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
- ServerWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
- };
- }
- else
- {
- LocalWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
- ServerWorldCanvas.GetComponent<Canvas>().worldCamera = GameSession.Instance.eventCamera;
- }
- #if UNITY_EDITOR
- if (isLoadOnEditor)
- {
- SceneConfMgr.Instance.OnSceneLoaded(SceneManager.GetActiveScene());
- }
- #else
- SceneConfMgr.Instance.OnSceneLoaded(SceneManager.GetActiveScene());
- #endif
- }
- protected override void OnDestroy()
- {
- base.OnDestroy();
- if (this.NodeDic != null)
- {
- this.NodeDic.Clear();
- }
- if (this.GoodsContainerMap != null)
- {
- this.GoodsContainerMap.Clear();
- }
- this.NodeDic = null;
- this.GoodsContainerMap = null;
- this.UserMap = null;
- }
- /// <summary>
- /// 翻页的算法实现
- /// </summary>
- /// <param name="all"></param>
- /// <param name="current"></param>
- public void OnChangePage(int all, int current)
- {
- }
- public void AddGoodData(GoodsInfo goodData)
- {
- if (!PageGoodsIdCount.ContainsKey(goodData.scene_id))
- {
- PageGoodsIdCount.Add(goodData.scene_id, 0);
- }
- PageGoodsIdCount[goodData.scene_id]++;// = count;
- }
- public void RemoveGoodData(GoodsInfo goodData)
- {
- if (PageGoodsIdCount.ContainsKey(goodData.scene_id))
- {
- PageGoodsIdCount[goodData.scene_id]--;
- }
- }
- }
- }
|