Пример #1
0
        /// <summary>
        /// 遍历资源
        /// </summary>
        override protected void VisitAssets(ResDescCollector resCollector, VisitResDelegate visitor)
        {
            for (int i = 0, ci = _items.Count; i < ci; ++i)
            {
                var item = _items[i];
                if (null != item.color)
                {
                    var resName = visitor(item.color);
                    resCollector.AddLightMap(resName, item.renderer, true);
                }

                if (null != item.dir)
                {
                    var resName = visitor(item.dir);
                    resCollector.AddLightMap(resName, item.renderer, false);
                }
            }
        }
Пример #2
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());
            }
        }