示例#1
0
        /// <summary>
        /// 导出场景
        /// </summary>
        public static bool ExportCurrentScene(bool useSceneFolder, bool compress)
        {
            var    sceneName = UtilityTools.GetFileName(EditorApplication.currentScene);
            string pathName;

            if (useSceneFolder)
            {
                pathName = UtilityTools.GenResExportPath(string.Format("scene/{0}", sceneName), sceneName);
            }
            else
            {
                pathName = UtilityTools.GenResExportPath("scene", sceneName);
            }

            if (string.IsNullOrEmpty(pathName))
            {
                return(false);
            }

            var options = BuildOptions.None;

            if (!compress)
            {
                options |= BuildOptions.UncompressedAssetBundle;
            }

            // 导出
            var levels = new string[] { EditorApplication.currentScene };

            BuildPipeline.PushAssetDependencies();
            string error = BuildPipeline.BuildStreamedSceneAssetBundle(levels, pathName, EditorUserBuildSettings.activeBuildTarget, options);

            if (!string.IsNullOrEmpty(error))
            {
                EndExportAsset();
                Debug.LogErrorFormat("Export Scene \"{0}\" failed! error: {1}", sceneName, error);
                return(false);
            }

            EndExportAsset();
            return(true);
        }
示例#2
0
        /// <summary>
        /// 导出资源包
        /// </summary>
        public static bool ExportRes(ResExportDesc rd, bool compress)
        {
            // 导出
            string pathName = UtilityTools.GenResExportPath(rd.resDir, rd.resName);
            var    names    = new string[] { rd.resName };
            var    assets   = new UnityEngine.Object[] { rd.asset };
            var    options  = BuildAssetBundleOptions.DeterministicAssetBundle;

            if (!compress)
            {
                options |= BuildAssetBundleOptions.UncompressedAssetBundle;
            }
            if (!BuildPipeline.BuildAssetBundleExplicitAssetNames(assets, names, pathName, options, EditorUserBuildSettings.activeBuildTarget))
            {
                Debug.LogErrorFormat("ExportAssetBundle {0} failed!", pathName);
                return(false);
            }

            return(true);
        }
示例#3
0
        /// <summary>
        /// 生成场景
        /// </summary>
        void BuildScene(bool exportScene, bool exportRes, bool compressScene, bool compressRes)
        {
            // 先保存场景
            EditorApplication.SaveScene();

            // 场景名字
            var    scenePath = EditorApplication.currentScene;
            string sceneName = UtilityTools.GetFileName(scenePath);

            // 导出目录
            var exportPath = UtilityTools.GenResExportPath(string.Format("scene/{0}", sceneName), sceneName);
            var exportDir  = Path.GetDirectoryName(exportPath);

            // 删除目录
            if (exportRes && exportScene)
            {
                Directory.Delete(exportDir, true);
            }

            // 创建目录
            if (!Directory.Exists(exportDir))
            {
                Directory.CreateDirectory(exportDir);
            }

            /// 开始处理数据
            try
            {
                do
                {
                    AssetsBackuper.inst.Clear();
                    if (exportScene)
                    {
                        var outputDir = EditorUtil.GetSceneOutputPath(null);
                        AssetDatabase.DeleteAsset(outputDir);
                        var outputPath = EditorUtil.GetSceneOutputPath(string.Format("{0}.unity", sceneName));
                        EditorApplication.SaveScene(outputPath);
                    }

                    // 树结构
                    var meshColliders = new List <MeshCollider>();
                    var trees         = SceneTree.Build(_settings.trees, meshColliders);
                    if (null == trees)
                    {
                        break;
                    }
                    SaveSettings();
                    SceneTree.Export(trees, meshColliders, exportRes, compressRes);

                    // 准备导出
                    EditorApplication.SaveScene();
                    AssetDatabase.SaveAssets();

                    if (exportScene)
                    {
                        ResExportUtil.ExportCurrentScene(true, compressScene);
                    }
                }while(false);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                AssetsBackuper.inst.Recover();
                AssetDatabase.SaveAssets();
            }

            finally
            {
                // 还原修改过的资源和场景
                if (_recovertScene)
                {
                    AssetsBackuper.inst.Recover();
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    EditorApplication.OpenScene(scenePath);
                    Resources.UnloadUnusedAssets();
                }
            }
        }