AssetBundle系列_场景资源之打包(一)

上传人:飞*** 文档编号:39949436 上传时间:2018-05-21 格式:DOCX 页数:5 大小:21.21KB
返回 下载 相关 举报
AssetBundle系列_场景资源之打包(一)_第1页
第1页 / 共5页
AssetBundle系列_场景资源之打包(一)_第2页
第2页 / 共5页
AssetBundle系列_场景资源之打包(一)_第3页
第3页 / 共5页
AssetBundle系列_场景资源之打包(一)_第4页
第4页 / 共5页
AssetBundle系列_场景资源之打包(一)_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《AssetBundle系列_场景资源之打包(一)》由会员分享,可在线阅读,更多相关《AssetBundle系列_场景资源之打包(一)(5页珍藏版)》请在金锄头文库上搜索。

1、AssetBundle 系列系列场景资源之打包(一)场景资源之打包(一)本篇讲解的是 3D 游戏的场景资源打包方式,首先简单的分析一下场景中所包含的资源的 类型。场景资源一般包含:地表模型地表模型(或者是 Unity Terrain),非实例化物体非实例化物体(摄像机、空气 墙、光源、各种逻辑物体之类的)、场景物体场景物体(花草树木、房子箱子之类的)。 因为场景物体大多是公用的,所以将场景物体都打成单独的包,将地表模型、非实例化物将场景物体都打成单独的包,将地表模型、非实例化物 体打包到场景包中体打包到场景包中。那么场景 TestScene 所对应的资源就包括:场景包 TestScene.un

2、ity3d、场景资源配表 TestSceneXML.xml。打包场景单个物体的具体细节在前面的帖子中已有讲解,此处不再赘述,本帖主要分享场 景本身的打包和场景资源的序列化。(1)打包一个场景)打包一个场景打包当前场景的代码如下所示,使用 BuildStreamedSceneAssetBundle 和 BuildPlayer 函数都可以实现此功能。public class PackScene public static void Execute(UnityEditor.BuildTarget target)string assetPath = SceneAssetProcesser.GetPla

3、tformPath(target);string exportPath = assetPath + “Scene/“;if (Directory.Exists(exportPath) = false)Directory.CreateDirectory(exportPath);string currentScene = EditorApplication.currentScene;string currentSceneName = currentScene.Substring(currentScene.LastIndexOf(/) + 1, currentScene.LastIndexOf(.)

4、 - currentScene.LastIndexOf(/) - 1);string fileName = exportPath + currentSceneName + “.unity3d“;BuildPipeline.BuildStreamedSceneAssetBundle(new string1 EditorApplication.currentScene , fileName, target);/ 另外一种方式/ BuildPipeline.BuildPlayer(new string1 EditorApplication.currentScene , fileName, targe

5、t, BuildOptions.BuildAdditionalStreamedScenes);将 TestScene 打包成 TestScene.unity3d 的包。(2)序列化场景物体)序列化场景物体对于一个场景物体,主要序列化其下列信息:(1)Transform 信息:位置、朝向、缩放(2)Mesh 信息:Shader 名字主颜色 Color光照贴图信息:是否为 static 对象(static 的对象才有光照贴图属性)、 LightmapIndex、LightmapTilingOffset主体代码如下所示:public static void ExportXML(string save

6、Path)/ 所有的动态加载的物体都挂在 ActiveObjectRoot 下面GameObject parent = GameObject.Find(“ActiveObjectRoot“);if (parent = null)Debug.LogError(“No ActiveObjectRoot Node!“);return;XmlDocument XmlDoc = new XmlDocument();XmlElement XmlRoot = XmlDoc.CreateElement(“Root“);XmlRoot.SetAttribute(“level“, EditorApplicatio

7、n.currentScene);XmlDoc.AppendChild(XmlRoot);foreach (Transform tranGroup in parent.transform)XmlElement xmlGroupNode = XmlDoc.CreateElement(“Group“);XmlRoot.AppendChild(xmlGroupNode);CreateTransformNode(XmlDoc, xmlGroupNode, tranGroup);foreach (Transform tranNode in tranGroup.transform)XmlElement xm

8、lNode = XmlDoc.CreateElement(“Node“);xmlGroupNode.AppendChild(xmlNode);CreateTransformNode(XmlDoc, xmlNode, tranNode);CreateMeshNode(XmlDoc, xmlNode, tranNode);string path = savePath + “Scene/“;if (Directory.Exists(path) = false)Directory.CreateDirectory(path);string levelPath = EditorApplication.cu

9、rrentScene;string levelName = levelPath.Substring(levelPath.LastIndexOf(/) + 1, levelPath.LastIndexOf(.) - levelPath.LastIndexOf(/) - 1);XmlDoc.Save(path + “Xml“ + levelName + “.xml“);XmlDoc = null;private static void CreateTransformNode(XmlDocument XmlDoc, XmlElement xmlNode, Transform tran)if (Xml

10、Doc = null | xmlNode = null | tran = null)return;XmlElement xmlProp = XmlDoc.CreateElement(“Transform“);xmlNode.AppendChild(xmlProp);xmlNode.SetAttribute(“name“, tran.name);xmlProp.SetAttribute(“posX“, tran.position.x.ToString();xmlProp.SetAttribute(“posY“, tran.position.y.ToString();xmlProp.SetAttr

11、ibute(“posZ“, tran.position.z.ToString();xmlProp.SetAttribute(“rotX“, tran.eulerAngles.x.ToString();xmlProp.SetAttribute(“rotY“, tran.eulerAngles.y.ToString();xmlProp.SetAttribute(“rotZ“, tran.eulerAngles.z.ToString();xmlProp.SetAttribute(“scaleX“, tran.localScale.x.ToString();xmlProp.SetAttribute(“

12、scaleY“, tran.localScale.y.ToString();xmlProp.SetAttribute(“scaleZ“, tran.localScale.z.ToString();private static void CreateMeshNode(XmlDocument XmlDoc, XmlElement xmlNode, Transform tran)if (XmlDoc = null | xmlNode = null | tran = null)return;XmlElement xmlProp = XmlDoc.CreateElement(“MeshRenderer“

13、);xmlNode.AppendChild(xmlProp);foreach (MeshRenderer mr in tran.gameObject.GetComponentsInChildren(true)if (mr.material != null)XmlElement xmlMesh = XmlDoc.CreateElement(“Mesh“);xmlProp.AppendChild(xmlMesh);/ 记录 Mesh 名字和 ShaderxmlMesh.SetAttribute(“Mesh“, mr.name);xmlMesh.SetAttribute(“Shader“, mr.m

14、aterial.shader.name);/ 记录主颜色XmlElement xmlColor = XmlDoc.CreateElement(“Color“);xmlMesh.AppendChild(xmlColor);bool hasColor = mr.material.HasProperty(“_Color“);xmlColor.SetAttribute(“hasColor“, hasColor.ToString();if (hasColor)xmlColor.SetAttribute(“r“, mr.material.color.r.ToString();xmlColor.SetAtt

15、ribute(“g“, mr.material.color.g.ToString();xmlColor.SetAttribute(“b“, mr.material.color.b.ToString();xmlColor.SetAttribute(“a“, mr.material.color.a.ToString();/ 光照贴图信息XmlElement xmlLightmap = XmlDoc.CreateElement(“Lightmap“);xmlMesh.AppendChild(xmlLightmap);/ 是否为 static,static 的对象才有 lightmap 信息xmlLi

16、ghtmap.SetAttribute(“IsStatic“, mr.gameObject.isStatic.ToString();xmlLightmap.SetAttribute(“LightmapIndex“, mr.lightmapIndex.ToString();xmlLightmap.SetAttribute(“OffsetX“, mr.lightmapTilingOffset.x.ToString();xmlLightmap.SetAttribute(“OffsetY“, mr.lightmapTilingOffset.y.ToString();xmlLightmap.SetAttribute(“OffsetZ“, mr.lightmapTilingOffset.z.ToString();xmlLightmap.SetAttribute(“OffsetW“,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号