/// <summary> /// 读取场景配置文件,生成场景对象 /// </summary> static void GenerateSceneObject(string scene_name) { if (!AssetBundleManager.Instance.IsSceneExist(scene_name)) { return; } var scene_desc = AssetBundleManager.Instance.ResourcesManifest.FindScene(scene_name); if (scene_desc == null) { return; } TextAsset text_asset = AssetBundleManager.Instance.LoadAsset <TextAsset>(scene_desc.SceneConfigPath); if (text_asset == null) { return; } SceneConfig config = new SceneConfig(); config.LoadFromString(text_asset.text); for (int i = 0; i < config.Data.SceneObjects.Count; ++i) { var obj = config.Data.SceneObjects[i]; var go = ResourcesManager.Load <GameObject>(obj.AssetName); var parent = GameObject.Find(obj.ParentName); var instance = GameObject.Instantiate <GameObject>(go); instance.transform.parent = parent != null ? parent.transform : null; instance.transform.position = obj.Position; instance.transform.localScale = obj.Scale; instance.transform.rotation = obj.Rotation; } }
/// <summary> /// 删除配置文件 /// </summary> public static void DeleteSceneConfig(string scene_path) { var file_name = SceneConfig.GetSceneConfigPath(scene_path); if (File.Exists(file_name)) { File.Delete(file_name); } }
static void SaveAll() { UnityEngine.SceneManagement.Scene sc = UnityEngine.SceneManagement.SceneManager.GetActiveScene(); if (!sc.IsValid()) { return; } SceneConfig data = new SceneConfig(); data.Data.LevelName = sc.name; GameObject[] array = GameObject.FindGameObjectsWithTag(SERIALIZE_SCENE_OBJECT_TAG); if (array == null) { return; } for (int i = 0; i < array.Length; ++i) { UnityEngine.Object parentObject = PrefabUtility.GetPrefabParent(array[i]); string path = AssetDatabase.GetAssetPath(parentObject); if (string.IsNullOrEmpty(path)) { continue; } var transform = array[i].transform; var parent = transform.parent; //写入数据 var obj = new SceneConfigData.SceneObject(); obj.AssetName = path; obj.Position = transform.position; obj.Scale = transform.lossyScale; obj.Rotation = transform.rotation; obj.ParentName = parent != null? Common.CalcTransformHierarchyPath(parent) : ""; data.Data.SceneObjects.Add(obj); } data.Save(SceneConfig.GetSceneConfigPath(sc.path)); AssetDatabase.Refresh(); }
/// <summary> /// 生成所有场景配置文件 /// </summary> public static bool GenerateAllSceneConfig(AssetBundleBuildData.SceneBuild scene_rules) { RecordDefaultOpenScene(); bool cancel = false; float total = (float)scene_rules.Scenes.Count; float current = 0; for (int i = 0; i < scene_rules.Scenes.Count; ++i) { var scene = scene_rules.Scenes[i].ScenePath; if (scene_rules.Scenes[i].IsBuild) { CopySceneToBackup(scene); GenerateSceneConfig(scene); AssetBundleNameTool.SetAssetBundleName(SceneConfig.GetSceneConfigPath(scene)); AssetBundleNameTool.SetAssetBundleName(scene); } else { DeleteSceneConfig(scene); AssetBundleNameTool.ClearAssetBundleName(scene); } current += 1.0f; float progress = current / total; if (EditorUtility.DisplayCancelableProgressBar("正在生成场景配置数据", "Change " + scene, progress)) { cancel = true; break; } } EditorUtility.ClearProgressBar(); return(!cancel); }
/// <summary> /// 根据AssetBundle导出ResourcesManifest文件 /// </summary> public static void ExportResourcesManifestFile(AssetBundleManifest manifest) { ResourcesManifest info = new ResourcesManifest(); //读取所有AssetBundle string root_dir = EditorCommon.BUILD_PATH + "/"; List <string> scenes = new List <string>(); if (manifest != null) { //读取主AssetBundle ResourcesManifestData.AssetBundle desc = new ResourcesManifestData.AssetBundle(); desc.AssetBundleName = Common.MAIN_MANIFEST_FILE_NAME; desc.Size = XFramework.FileHelper.GetFileSize(root_dir + Common.MAIN_MANIFEST_FILE_NAME); info.Data.AssetBundles.Add(Common.MAIN_MANIFEST_FILE_NAME, desc); //读取其它AssetBundle foreach (var name in manifest.GetAllAssetBundles()) { desc = new ResourcesManifestData.AssetBundle(); desc.AssetBundleName = name; desc.Size = XFramework.FileHelper.GetFileSize(root_dir + name); AssetBundle ab = AssetBundle.LoadFromFile(root_dir + name); foreach (var asset in ab.GetAllAssetNames()) { desc.Assets.Add(asset); } foreach (var scene in ab.GetAllScenePaths()) { desc.Scenes.Add(scene); scenes.Add(scene); } ab.Unload(false); info.Data.AssetBundles.Add(name, desc); } } //读取所有Scene信息 for (int i = 0; i < scenes.Count; ++i) { ResourcesManifestData.Scene scene_desc = new ResourcesManifestData.Scene(); scene_desc.SceneLevelName = Path.GetFileNameWithoutExtension(scenes[i]); scene_desc.ScenePath = scenes[i]; scene_desc.SceneConfigPath = SceneConfig.GetSceneConfigPath(scenes[i]); info.Data.Scenes.Add(scene_desc.SceneLevelName, scene_desc); } //读取旧的ResourcesInfo,同步其它额外的数据 ResourcesManifest old_info = new ResourcesManifest(); old_info.Load(EditorCommon.RESOURCES_MANIFEST_FILE_PATH); if (old_info.Data != null && old_info.Data.AssetBundles.Count > 0) { foreach (var desc in old_info.Data.AssetBundles.Values) { if (info.Data.AssetBundles.ContainsKey(desc.AssetBundleName)) { info.Data.AssetBundles[desc.AssetBundleName].IsNative = desc.IsNative; info.Data.AssetBundles[desc.AssetBundleName].IsPermanent = desc.IsPermanent; } } } //保存ResourcesInfo string resources_manifest_file = EditorCommon.BUILD_PATH + "/" + Common.RESOURCES_MANIFEST_FILE_NAME; info.Save(resources_manifest_file); AssetDatabase.Refresh(); }