Exemplo n.º 1
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();
                }
            }
        }
Exemplo n.º 2
0
 private static bool TestExportCurrentScene()
 {
     ResExportUtil.ExportCurrentScene(true, false);
     return(true);
 }
Exemplo n.º 3
0
        /// <summary>
        /// 导出
        /// </summary>
        static public void Export(SceneTree[] trees, List <MeshCollider> meshColliders, bool exportRes, bool compress)
        {
            // 收集依赖
            var sb           = new StringBuilder();
            int depsCount    = 0;
            var resCollector = new ResDescCollector();
            var dictRes      = new Dictionary <string, ResExportDesc>();

            // 光照图(不能卸载第一张光照图,否则unity_Lightmap_HDR会置为1)
            var lightMaps = LightmapSettings.lightmaps;

            if (null != lightMaps && lightMaps.Length > 0 && null != lightMaps[0].lightmapColor)
            {
                var resName = ResDepBase.GetResName(lightMaps[0].lightmapColor);
                resCollector.AddLightMap(resName, 0, true);
            }

            // 场景
            for (int i = 0; i < trees.Length; ++i)
            {
                var tree = trees[i];
                if (null != tree && tree.settings.objType == SceneTreeObjType.Renderer)
                {
                    var n = tree.CollectDeps(dictRes);
                    sb.AppendFormat("Tree {0} deps count is {1}\n", tree.settings.name, n);
                    depsCount += n;
                }
            }
            sb.AppendFormat("All tree deps count is {0}\n", depsCount);
            Debug.Log(sb.ToString());

            // 生成节点数据
            var prefabCollection = new ScenePrefabCollection();
            var treeDatas        = new List <SceneTreeData>();

            for (int i = 0; i < trees.Length; ++i)
            {
                var tree = trees[i];
                if (null != tree)
                {
                    var data = tree.BuildData(prefabCollection, dictRes);
                    treeDatas.Add(data);
                }
            }

            // LightMap这里要置空一下,仅清空LightmapSettings.lightmaps仍然有依赖
            Lightmapping.lightingDataAsset = null;

            // 只留一盏主光源
            var   lights    = Light.GetLights(LightType.Directional, 0);
            Light mainLight = null;

            if (lights.Length > 0)
            {
                mainLight = lights[0];
                for (int i = 1; i < lights.Length; ++i)
                {
                    GameObject.DestroyImmediate(lights[i].gameObject);
                }
            }

            // MeshCollider
            var deps = new List <ResDepBase>();

            ResDepBase.CollectColliderDependencies(meshColliders, deps);
            for (int n = 0, cn = deps.Count; n < cn; ++n)
            {
                var dep = deps[n];
                dep.CollectRes(resCollector, dictRes);
                dep.RemoveDependencies();
            }

            // 场景描述
            var       goSceneDesc = new GameObject("scene_desc");
            SceneDesc desc        = goSceneDesc.AddComponent <SceneDesc>();

            desc.mainLight     = mainLight;
            desc.lightMapCount = LightmapSettings.lightmaps.Length;
            desc.resCollection = resCollector.Export();
            desc.trees         = treeDatas.ToArray();
            desc.prefabs       = prefabCollection.Export();

            // 移除编辑脚本
            var cfgs = UnityEngine.Object.FindObjectsOfType <EditorSceneObject>();

            for (int i = 0; i < cfgs.Length; ++i)
            {
                UnityEngine.Object.DestroyImmediate(cfgs[i]);
            }

            // 资源
            if (exportRes)
            {
                var sbExport = new StringBuilder();
                sbExport.AppendFormat("Resources Count[{0}]\n", dictRes.Count);
                sbExport.Append("---------------------------------------------------------------\n");
                foreach (var pair in dictRes)
                {
                    var resDesc = pair.Value;
                    ResExportUtil.ExportRes(resDesc, compress);
                    sbExport.AppendFormat("{0}/{1} - {2}\n", resDesc.resDir, resDesc.resName, resDesc.refCount);
                }
                sbExport.Append("---------------------------------------------------------------\n");
                Debug.Log(sbExport.ToString());
            }
        }