using BeinLab.Util;
using PublicTools.Unity;
using PublicTools.XMLDataBase;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using XRTool.Util;
namespace XRTool.Util
{
///
/// 场景加载管理类
///
public class SceneConfMgr : Singleton
{
private TableInterface table;
private string tableName;
private bool isInitMap = false;
private Action InitMapComplete;
///
/// 场景以及对应的配置文件
///
private Dictionary> scenesObjMap;
public SceneConfMgr()
{
isInitMap = false;
#if UNITY_EDITOR || !UNITY_ANDROID
tableName = typeof(SceneConf).Name;
table = XSql.Instance.OpenTable(ResourcesManager.LocalPath, tableName, ".xml", true);
OpenTable();
#else
string fullPath = Path.Combine(ResourcesManager.LocalPath, typeof(SceneConf).Name + ".xml");
GameNode.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List sceneConfs) =>
{
InitSceneMap(sceneConfs);
}));
#endif
//SceneManager.sceneLoaded += OnSceneLoaded;
}
public void OpenTable()
{
if (!table.Open())
{
table.Create(tableName);
UnityLog.Instance.Log("create table" + tableName);
}
var sceneConfs = table.FindAllData();
InitSceneMap(sceneConfs);
}
public void InitSceneMap(List sceneConfs)
{
if (sceneConfs != null)
{
scenesObjMap = new Dictionary>();
for (int i = 0; i < sceneConfs.Count; i++)
{
SceneConf conf = sceneConfs[i];
AddScene(conf);
}
}
isInitMap = true;
InitMapComplete?.Invoke();
}
public void AddScene(SceneConf conf)
{
if (scenesObjMap != null)
{
if (!scenesObjMap.ContainsKey(conf.SceneName))
{
List list = new List();
list.Add(conf);
scenesObjMap.Add(conf.SceneName, list);
}
else
{
List list = scenesObjMap[conf.SceneName];
for (int i = 0; i < list.Count; i++)
{
if (list[i].Prikey == conf.Prikey)
{
return;
}
}
list.Add(conf);
}
}
}
///
/// 当场景被加载时
///
///
///
public void OnSceneLoaded(Scene scene)
{
if (isInitMap)
{
if (scenesObjMap != null && scenesObjMap.ContainsKey(scene.name))
{
var list = scenesObjMap[scene.name];
if (list != null)
{
for (int i = 0; i < list.Count; i++)
{
SceneConf conf = list[i];
var obj = ResourcesManager.Instance.DataLoader.Load(conf.Path);
if (obj)
{
GameObject gameObj = UnityEngine.Object.Instantiate(obj) as GameObject;
gameObj.SetActive(conf.IsActiveOnAwake);
gameObj.name = conf.Name;
GameNode.Instance.SetParent(conf.NodeName, gameObj.transform, conf.Position.Trans(),
conf.Angle.Trans(), conf.Scale.Trans());
}
}
}
}
}
else
{
InitMapComplete+= () =>{
OnSceneLoaded(scene);
};
}
}
///
/// 清空场景
///
///
public void ClearSceneObj(string sceneName)
{
if (table != null && scenesObjMap != null && scenesObjMap.ContainsKey(sceneName))
{
var list = scenesObjMap[sceneName];
if (list != null)
{
for (int i = 0; i < list.Count; i++)
{
table.DeleteData(list[i].Prikey);
}
table.Save();
}
scenesObjMap.Remove(sceneName);
}
}
///
///
///
///
///
///
///
public void SaveSceens(string scene, Transform parent, Transform body, string path)
{
if (body != null && table != null)
{
SceneConf conf = new SceneConf();
conf.Prikey = scene + "_" + body.name;
conf.SceneName = scene;
conf.Name = body.name;
conf.Path = path;
if (parent)
{
conf.NodeName = (ObjNode)Enum.Parse(typeof(ObjNode), parent.name);
}
else
{
conf.NodeName = ObjNode.Null;
}
conf.Position = new XVector3(body.position);
conf.Angle = new XVector3(body.localEulerAngles);
conf.Scale = new XVector3(body.localScale);
conf.IsActiveOnAwake = body.gameObject.activeSelf;
table.InsertData(conf);
table.Save();
AddScene(conf);
}
}
}
}