SceneConfMgr.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using BeinLab.Util;
  2. using PublicTools.Unity;
  3. using PublicTools.XMLDataBase;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. using XRTool.Util;
  11. namespace XRTool.Util
  12. {
  13. /// <summary>
  14. /// 场景加载管理类
  15. /// </summary>
  16. public class SceneConfMgr : Singleton<SceneConfMgr>
  17. {
  18. private TableInterface table;
  19. private string tableName;
  20. private bool isInitMap = false;
  21. private Action InitMapComplete;
  22. /// <summary>
  23. /// 场景以及对应的配置文件
  24. /// </summary>
  25. private Dictionary<string, List<SceneConf>> scenesObjMap;
  26. public SceneConfMgr()
  27. {
  28. isInitMap = false;
  29. #if UNITY_EDITOR || !UNITY_ANDROID
  30. tableName = typeof(SceneConf).Name;
  31. table = XSql.Instance.OpenTable(ResourcesManager.LocalPath, tableName, ".xml", true);
  32. OpenTable();
  33. #else
  34. string fullPath = Path.Combine(ResourcesManager.LocalPath, typeof(SceneConf).Name + ".xml");
  35. GameNode.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<SceneConf> sceneConfs) =>
  36. {
  37. InitSceneMap(sceneConfs);
  38. }));
  39. #endif
  40. //SceneManager.sceneLoaded += OnSceneLoaded;
  41. }
  42. public void OpenTable()
  43. {
  44. if (!table.Open())
  45. {
  46. table.Create(tableName);
  47. UnityLog.Instance.Log("create table" + tableName);
  48. }
  49. var sceneConfs = table.FindAllData<SceneConf>();
  50. InitSceneMap(sceneConfs);
  51. }
  52. public void InitSceneMap(List<SceneConf> sceneConfs)
  53. {
  54. if (sceneConfs != null)
  55. {
  56. scenesObjMap = new Dictionary<string, List<SceneConf>>();
  57. for (int i = 0; i < sceneConfs.Count; i++)
  58. {
  59. SceneConf conf = sceneConfs[i];
  60. AddScene(conf);
  61. }
  62. }
  63. isInitMap = true;
  64. InitMapComplete?.Invoke();
  65. }
  66. public void AddScene(SceneConf conf)
  67. {
  68. if (scenesObjMap != null)
  69. {
  70. if (!scenesObjMap.ContainsKey(conf.SceneName))
  71. {
  72. List<SceneConf> list = new List<SceneConf>();
  73. list.Add(conf);
  74. scenesObjMap.Add(conf.SceneName, list);
  75. }
  76. else
  77. {
  78. List<SceneConf> list = scenesObjMap[conf.SceneName];
  79. for (int i = 0; i < list.Count; i++)
  80. {
  81. if (list[i].Prikey == conf.Prikey)
  82. {
  83. return;
  84. }
  85. }
  86. list.Add(conf);
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// 当场景被加载时
  92. /// </summary>
  93. /// <param name="arg0"></param>
  94. /// <param name="arg1"></param>
  95. public void OnSceneLoaded(Scene scene)
  96. {
  97. if (isInitMap)
  98. {
  99. if (scenesObjMap != null && scenesObjMap.ContainsKey(scene.name))
  100. {
  101. var list = scenesObjMap[scene.name];
  102. if (list != null)
  103. {
  104. for (int i = 0; i < list.Count; i++)
  105. {
  106. SceneConf conf = list[i];
  107. var obj = ResourcesManager.Instance.DataLoader.Load(conf.Path);
  108. if (obj)
  109. {
  110. GameObject gameObj = UnityEngine.Object.Instantiate(obj) as GameObject;
  111. gameObj.SetActive(conf.IsActiveOnAwake);
  112. gameObj.name = conf.Name;
  113. GameNode.Instance.SetParent(conf.NodeName, gameObj.transform, conf.Position.Trans(),
  114. conf.Angle.Trans(), conf.Scale.Trans());
  115. }
  116. }
  117. }
  118. }
  119. }
  120. else
  121. {
  122. InitMapComplete+= () =>{
  123. OnSceneLoaded(scene);
  124. };
  125. }
  126. }
  127. /// <summary>
  128. /// 清空场景
  129. /// </summary>
  130. /// <param name="sceneName"></param>
  131. public void ClearSceneObj(string sceneName)
  132. {
  133. if (table != null && scenesObjMap != null && scenesObjMap.ContainsKey(sceneName))
  134. {
  135. var list = scenesObjMap[sceneName];
  136. if (list != null)
  137. {
  138. for (int i = 0; i < list.Count; i++)
  139. {
  140. table.DeleteData<SceneConf>(list[i].Prikey);
  141. }
  142. table.Save();
  143. }
  144. scenesObjMap.Remove(sceneName);
  145. }
  146. }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. /// <param name="scene"></param>
  151. /// <param name="parent"></param>
  152. /// <param name="body"></param>
  153. /// <param name="path"></param>
  154. public void SaveSceens(string scene, Transform parent, Transform body, string path)
  155. {
  156. if (body != null && table != null)
  157. {
  158. SceneConf conf = new SceneConf();
  159. conf.Prikey = scene + "_" + body.name;
  160. conf.SceneName = scene;
  161. conf.Name = body.name;
  162. conf.Path = path;
  163. if (parent)
  164. {
  165. conf.NodeName = (ObjNode)Enum.Parse(typeof(ObjNode), parent.name);
  166. }
  167. else
  168. {
  169. conf.NodeName = ObjNode.Null;
  170. }
  171. conf.Position = new XVector3(body.position);
  172. conf.Angle = new XVector3(body.localEulerAngles);
  173. conf.Scale = new XVector3(body.localScale);
  174. conf.IsActiveOnAwake = body.gameObject.activeSelf;
  175. table.InsertData(conf);
  176. table.Save();
  177. AddScene(conf);
  178. }
  179. }
  180. }
  181. }