123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 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
- {
- /// <summary>
- /// 场景加载管理类
- /// </summary>
- public class SceneConfMgr : Singleton<SceneConfMgr>
- {
- private TableInterface table;
- private string tableName;
- private bool isInitMap = false;
- private Action InitMapComplete;
- /// <summary>
- /// 场景以及对应的配置文件
- /// </summary>
- private Dictionary<string, List<SceneConf>> 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<SceneConf> 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<SceneConf>();
- InitSceneMap(sceneConfs);
- }
- public void InitSceneMap(List<SceneConf> sceneConfs)
- {
- if (sceneConfs != null)
- {
- scenesObjMap = new Dictionary<string, List<SceneConf>>();
- 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<SceneConf> list = new List<SceneConf>();
- list.Add(conf);
- scenesObjMap.Add(conf.SceneName, list);
- }
- else
- {
- List<SceneConf> list = scenesObjMap[conf.SceneName];
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].Prikey == conf.Prikey)
- {
- return;
- }
- }
- list.Add(conf);
- }
- }
- }
- /// <summary>
- /// 当场景被加载时
- /// </summary>
- /// <param name="arg0"></param>
- /// <param name="arg1"></param>
- 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);
- };
- }
- }
- /// <summary>
- /// 清空场景
- /// </summary>
- /// <param name="sceneName"></param>
- 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<SceneConf>(list[i].Prikey);
- }
- table.Save();
- }
- scenesObjMap.Remove(sceneName);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="scene"></param>
- /// <param name="parent"></param>
- /// <param name="body"></param>
- /// <param name="path"></param>
- 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);
- }
- }
- }
- }
|