/// <summary> /// 实例化单个GO /// </summary> /// <param name="loadItem"></param> private void InstantiateSingleObj(LoadItemData loadItem) { GameObject go = GameObject.Instantiate(cacheObjs[loadItem.prefabABName]) as GameObject; go.name = NameTool.GetRealName(loadItem.prefabABName); EntityInfo info = cacheObjs[loadItem.organizeDataABName] as EntityInfo; go.GetComponent <EntityConfig>().InitSelf(info); RegistGameObject(go); if (go.GetComponent <ModuleBase>() != null) { go.GetComponent <ModuleBase>().Regist(); } }
/// <summary> /// 加载场景初始所需的所有prefab /// </summary> private void LoadAllScenePrefabs() { loadDic = GetScenePrefabNames(); loadedObjRecord.Clear(); cacheObjs.Clear(); for (int i = 0; i < loadDic.Count; i++) { LoadItemData loadItem = loadDic[i]; //1.记录请求加载的资源 loadedObjRecord.Add(loadDic[i].prefabABName, false); loadedObjRecord.Add(loadDic[i].organizeDataABName, false); //2.加载prefab本身 LoadRequest(loadItem.prefabABName); //3.加载prefab对应的organize data(Scriptable Object) LoadRequest(loadItem.organizeDataABName); } cacheObjs.Clear(); }
/// <summary> /// 读取记录文件,得到场景所需的prefab信息 /// </summary> /// <returns></returns> private SortedDictionary <int, LoadItemData> GetScenePrefabNames() { string filePath = new StringBuilder(PathTool.GetAssetBundlePath()).Append('/').Append(SceneManager.GetActiveScene().name).Append("_loadRecord.xml").ToString(); SortedDictionary <int, LoadItemData> prefabs = new SortedDictionary <int, LoadItemData>(); XmlReader reader = XmlReader.Create(filePath); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name.Equals("LoadItem")) { XElement ele = XElement.ReadFrom(reader) as XElement; LoadItemData loadItem = new LoadItemData(); loadItem.initialOrder = int.Parse(ele.Attribute("InitialOrder").Value); loadItem.isActive = bool.Parse(ele.Attribute("IsActive").Value); loadItem.prefabABName = ele.Attribute("PrefabABPath").Value; loadItem.organizeDataABName = ele.Attribute("OrganizeDataABPath").Value; prefabs.Add(loadItem.initialOrder, loadItem); } } reader.Close(); return(prefabs); }