示例#1
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());
            }
        }
示例#2
0
        /// <summary>
        /// 收集依赖
        /// </summary>
        public int CollectDeps(Dictionary <string, ResExportDesc> dictRes)
        {
            // 没有对象
            if (_items.Length < 1)
            {
                return(0);
            }

            // 收集对象中的依赖
            int nDepCount = 0;

            for (int i = 0; i < _items.Length; ++i)
            {
                _deps.Clear();
                var item = _items[i];

                // 检查游戏对象, 收集go对象所依赖对象
                ResDepBase.CollectRendererDependencies(item.go, false, _deps, null);

                // 没有依赖
                if (_deps.Count <= 0)
                {
                    continue;
                }

                nDepCount += _deps.Count;

                // LOD0
                var resDep = new TreeNodeObjectDep();
                resDep.lods.Add(new TreeNodeObjectLodDep {
                    go = item.go, deps = new List <ResDepBase>(_deps)
                });

                // 只有地形才导出Lightmap
                var isTerrain = item.go.tag.Equals("Terrain");
                if (null != item.cfg)
                {
                    if (null != item.cfg.lods)
                    {
                        for (int n = 0; n < item.cfg.lods.Length; n++)
                        {
                            _deps.Clear();

                            var lod = item.cfg.lods[n];
                            if (lod == null)
                            {
                                continue;
                            }

                            ResDepBase.CollectRendererDependencies(lod.go, false, _deps, item.go, isTerrain);
                            if (_deps.Count <= 0)
                            {
                                continue;
                            }
                            resDep.lods.Add(new TreeNodeObjectLodDep {
                                go = lod.go, deps = new List <ResDepBase>(_deps)
                            });
                            nDepCount += _deps.Count;
                        }
                    }
                }
                item.resDep = resDep;
                _items[i]   = item;
            }

            return(nDepCount);
        }