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,
}
///
/// 游戏节点管理器
///
public class GameNode : UnitySingleton
{
///
/// 节点map,包含local,server下的所有节点信息
///
private Dictionary> nodeDic;
private string[] nodeRoot = new string[] { "Local", "Server" };
private string[] nodes = new string[] { "UICanvas", "WorldCanvas", "World" };
public Dictionary> NodeDic { get => nodeDic; set => nodeDic = value; }
public string[] NodeRoot { get => nodeRoot; }
public string[] Nodes { get => nodes; }
#if UNITY_EDITOR
public bool isLoadOnEditor = true;
#endif
///
/// 物体id和容器的集合
/// 注意进入房间时开始同步,退出房间时要清除缓存
///
private Dictionary goodsContainerMap;
///
/// 缓存的用户信息
///
private Dictionary userMap;
///
/// 对应页的物体的id
/// 每一个页代表一个节点数据
///
private Dictionary> pageGoodsId;
///
/// 对应同步物体数据的id
/// 方便索引查找
///
private Dictionary 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 GoodsContainerMap { get => goodsContainerMap; set => goodsContainerMap = value; }
public Dictionary UserMap { get => userMap; set => userMap = value; }
public Dictionary PageGoods
{
get
{
if (pageGoods == null)
{
pageGoods = new Dictionary();
}
return pageGoods;
}
}
///
/// 缓存每一页的物体的id集合
/// 当发生翻页的时候,清理当前页的缓存信息,但是此列表数据不移除:但是如果保证删除时的同步功能?
///
public Dictionary> PageGoodsId
{
get
{
if (pageGoodsId == null)
{
pageGoodsId = new Dictionary>();
}
return pageGoodsId;
}
}
public Dictionary PageGoodsIdCount = new Dictionary();
public int DefaultPageIndex { get => defaultPageIndex; set => defaultPageIndex = value; }
protected override void Awake()
{
base.Awake();
if (NodeDic == null)
{
InitNode();
}
}
///
/// 检测某节点是否是系统节点
///
///
///
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;
}
///
/// 初始化节点
///
private void InitNode()
{
if (NodeDic == null)
{
NodeDic = new Dictionary>();
}
for (int i = 0; i < NodeRoot.Length; i++)
{
Dictionary dic = null;
if (NodeDic.ContainsKey(NodeRoot[i]))
{
dic = NodeDic[NodeRoot[i]];
}
else
{
dic = new Dictionary();
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);
// }
//}
}
///
/// 添加一个节点对象
///
public void AddNode(Transform node, string nodeName, int index)
{
if (index >= 0 && index < nodeRoot.Length)
{
Dictionary dic = NodeDic[NodeRoot[index]];
if (!dic.ContainsKey(nodeName))
{
dic.Add(nodeName, node);
}
}
}
///
/// 设置节点
///
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;
}
///
/// 设置节点
///
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);
}
///
/// 设置节点
///
public void SetParent(ObjNode objNode, Transform node, Vector3 position, Vector3 angle, Vector3 scale, bool isLocal = true)
{
SetParent(objNode.ToString(), node, position, angle, scale, isLocal);
}
///
/// 查找一个节点
///
///
///
///
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