AssetBundle系列——场景资源之解包(二)
本篇接着上一篇继续和大家分享场景资源这一主题,主要包括两个方面:
(1)加载场景
场景异步加载的代码比较简单,如下所示:
private IEnumerator LoadLevelCoroutine()
{
string url = "ftp://127.0.0.1/TestScene.unity3d";
int verNum = ; WWW wwwForScene = WWW.LoadFromCacheOrDownload(url, verNum);
while (wwwForScene.isDone == false)
yield return null; AssetBundle bundle = wwwForScene.assetBundle;
yield return Application.LoadLevelAsync("TestScene");
wwwForScene.assetBundle.Unload(false);
}
(2)加载场景物件
主要包含以下细分步骤:
a、下载并解析场景配表,得到场景物件信息。场景物件的数据结构如下所示:
public class XmlSceneGameobjectProp
{
// Mesh信息
public class MeshInfo
{
public string name;
public string shader; public bool hasColor = false;
public Vector4 color; public bool isStatic = true;
public int lightmapIndex;
public Vector4 lightmapTilingOffset;
} public string name;
public string group;
// Transform信息
public float posX, posY, posZ;
public float rotX, rotY, rotZ;
public float scaleX, scaleY, scaleZ;
// Mesh列表,一个模型可以包含多个MeshRenderer
public List<MeshInfo> LstMesh = new List<MeshInfo>();
}
xml解析的主体代码如下所示:
private void ParseChildNode(XmlElement xmlGroup, XmlElement xmlChild)
{
SceneGameobjectProp newChild = new SceneGameobjectProp();
newChild.group = xmlGroup.GetAttribute("name");
newChild.name = xmlChild.GetAttribute("name");
// 注册资源名字
if (lstRes.Contains(newChild.name) == false)
{
lstRes.Add(newChild.name);
} // Tranform节点
XmlNode xmlTransform = xmlChild.SelectSingleNode("Transform");
// MeshRenderer节点
XmlNode xmlMeshRenderer = xmlChild.SelectSingleNode("MeshRenderer"); if (xmlTransform != null && xmlTransform is XmlElement)
{
CXmlRead goReader = new CXmlRead(xmlTransform as XmlElement);
newChild.posX = goReader.Float("posX", 0f);
newChild.posY = goReader.Float("posY", 0f);
newChild.posZ = goReader.Float("posZ", 0f);
newChild.rotX = goReader.Float("rotX", 0f);
newChild.rotY = goReader.Float("rotY", 0f);
newChild.rotZ = goReader.Float("rotZ", 0f);
newChild.scaleX = goReader.Float("scaleX", 1f);
newChild.scaleY = goReader.Float("scaleY", 1f);
newChild.scaleZ = goReader.Float("scaleZ", 1f);
} if (xmlMeshRenderer != null && xmlMeshRenderer is XmlElement)
{
foreach (XmlNode node in xmlMeshRenderer.ChildNodes)
{
if ((node is XmlElement) == false)
continue; SceneGameobjectProp.MeshInfo mesh = new SceneGameobjectProp.MeshInfo();
mesh.name = (node as XmlElement).GetAttribute("Mesh");
mesh.shader = (node as XmlElement).GetAttribute("Shader");
XmlNode xmlLightmap = node.SelectSingleNode("Lightmap");
if (xmlLightmap != null && xmlLightmap is XmlElement)
{
CXmlRead reader = new CXmlRead(xmlLightmap as XmlElement);
mesh.isStatic = reader.Bool("IsStatic", true);
mesh.lightmapIndex = reader.Int("LightmapIndex", -);
mesh.lightmapTilingOffset = new Vector4(reader.Float("OffsetX", 0f), reader.Float("OffsetY", 0f), reader.Float("OffsetZ", 0f), reader.Float("OffsetW", 0f));
}
XmlNode xmlColor = node.SelectSingleNode("Color");
if (xmlColor != null && xmlColor is XmlElement)
{
CXmlRead reader = new CXmlRead(xmlColor as XmlElement);
mesh.hasColor = reader.Bool("hasColor", false);
mesh.color = new Vector4(reader.Float("r", 0f), reader.Float("g", 0f), reader.Float("b", 0f), reader.Float("a", 0f));
}
newChild.LstMesh.Add(mesh);
}
} lstGameObjectProp.Add(newChild);
}
b、加载场景物件asset
同时开启多个Coroutine进行WWW的LoadFromCacheOrDownload操作,经测试开启的WWW线程越多,速度会越快,但是需要考虑实际的机器或平台的承载能力。
注意,我这儿的WWW操作是直接从缓存里面载入内存,而不是从网上下载。所有更新的游戏物件,可以在游戏开始的时候一次从网上Download到Cache,这样,在游戏过程中就不需要从网上Download资源了,wifi下载3G玩,爽歪歪~
如果一定要在此处从网上Download资源的话,线程数最好设为5个,很多平台有自己的限制,比如有的网页浏览器只能同时开6个等等......
// 同时开启的Coroutine的数目
private const int ThreadNum = ;
// 记录每个加载线程的进度,只有每个线程都加在结束了,场景加载才算完成
private int[] arrThreadProggress = new int[ThreadNum]; // 加载完成后的回掉
public delegate void LoadFinishDelegate();
public LoadFinishDelegate OnLoadFinish = null; // 需要下载的资源列表
private List<string> lstRes = new List<string>();
// 是否加载完毕的标记
private bool hasFinished = false; private void LoadAsset()
{for (int i = ; i < ThreadNum; ++i)
{
CoroutineProvider.Instance().StartCoroutine(LoadAssetCoroutine(i));
}
} private IEnumerator LoadAssetCoroutine(int threadIndex)
{
while (arrThreadProggress[threadIndex] < lstRes.Count)
{
// 载入资源
string name = lstRes[arrThreadProggress[threadIndex]];
GameApp.GetResourceManager().LoadAsync(GlobalSetting.SceneAssetPath + name, typeof(GameObject));
while (GameApp.GetResourceManager().IsResLoaded(GlobalSetting.SceneAssetPath + name) == false)
{
yield return null;
}
arrThreadProggress[threadIndex] += ThreadNum;
}
// 线程资源下载完毕,进行加载回掉
if (IsLoadFinished() && hasFinished == false)
{
hasFinished = true;if (OnLoadFinish != null)
{
OnLoadFinish();
}
}
}
上面的黑体标出的代码是是实际的加载代码,具体实现已经在帖子“AssetBundle系列——资源的加载、简易的资源管理器”中讲解过了,此处不再赘述。
c、实例化场景物件
// 实例化
GameObject goIns = GameObject.Instantiate(asset) as GameObject;
goIns.name = goProp.name; // 设置父节点
GameObject goGroup = null;
dicGroupGameobject.TryGetValue(goProp.group, out goGroup);
if (goGroup != null)
goIns.transform.parent = goGroup.transform;
else
goIns.transform.parent = goRoot.transform; // 设置Transform
goIns.transform.position = new Vector3(goProp.posX, goProp.posY, goProp.posZ);
goIns.transform.eulerAngles = new Vector3(goProp.rotX, goProp.rotY, goProp.rotZ);
goIns.transform.localScale = new Vector3(goProp.scaleX, goProp.scaleY, goProp.scaleZ); // 设置Shader、Lightmap
int index = ;
int meshCount = goProp.LstMesh.Count;
foreach (MeshRenderer mr in goIns.gameObject.GetComponentsInChildren<MeshRenderer>(true))
{
if (mr.sharedMaterial != null)
{
if (index < meshCount)
{
SceneGameobjectProp.MeshInfo meshProp = goProp.LstMesh[index];
mr.sharedMaterial.shader = Shader.Find(meshProp.shader);
if (meshProp.hasColor)
mr.sharedMaterial.color = meshProp.color;
bool isStatic = meshProp.isStatic;
mr.gameObject.isStatic = isStatic;
if (isStatic)
{
mr.lightmapIndex = meshProp.lightmapIndex;
mr.lightmapTilingOffset = meshProp.lightmapTilingOffset;
}
}
index++;
}
}
本帖主要是关于assetbundle的处理方法,关于物体材质的实例化逻辑,有很多种做法,我这只是提供了其中一种做法。其中有一点需要注意的,就是material和sharedMaterial的区别,上面实例化中的代码,我用的是sharedMaterial来设置,使用material是有问题的,因为每一次对material的赋值会导致生成一个materil的instance产生。
AssetBundle系列——场景资源之解包(二)的更多相关文章
- AssetBundle系列——场景资源之打包(一)
本篇讲解的是3D游戏的场景资源打包方式,首先简单的分析一下场景中所包含的资源的类型. 场景资源一般包含:地表模型(或者是Unity Terrain),非实例化物体(摄像机.空气墙.光源.各种逻辑物体之 ...
- AssetBundle系列——游戏资源打包(二)
本篇接着上一篇.上篇中说到的4步的代码分别如下所示: (1)将资源打包成assetbundle,并放到自定目录下 using UnityEditor; using UnityEngine; using ...
- (转)AssetBundle系列——游戏资源打包(二)
转自:http://www.cnblogs.com/sifenkesi/p/3557290.html 本篇接着上一篇.上篇中说到的4步的代码分别如下所示: (1)将资源打包成assetbundle,并 ...
- AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...
- AssetBundle系列——游戏资源打包(一)
将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新.服务器上包含以下资源列表:(1)游戏内容资源assetbundle(2)资源维护列表,包含每个资源的名字(完整路径名)和对应的版本号[资源名 ...
- [Unity Asset]AssetBundle系列——游戏资源打包
转载:http://www.cnblogs.com/sifenkesi/p/3557231.html 将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新.服务器上包含以下资源列表:(1)游戏内 ...
- (转)AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...
- (转)AssetBundle系列——游戏资源打包(一)
转自:http://www.cnblogs.com/sifenkesi/p/3557231.html 将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新.服务器上包含以下资源列表:(1)游戏内 ...
- 从零系列--开发npm包(二)
一.利用shell简化组合命令 set -e CVERSION=$(git tag | ) echo "current version:$CVERSION" echo " ...
随机推荐
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- mysql优化之表建设
就拿常见的用户表.文章类的表.日志表来分析如下 CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMEN ...
- 为CentOS 6 配置本地YUM源
在网上找了很多为CentOS 6配置本地YUM源的方法,其中有很多是与网络相关的,我只想配个自己用的,结果就发现这个方法比较简单实用,就转过来了. 环境:CentOS 6.0 默认的yum是以网络来安 ...
- nginx 配置其他路径
gedit /etc/nginx/sites-enabled/default location /hlstest { types { application/vnd.apple.mpegurl m3u ...
- 小白学数据分析----->ARPDAU的价值
最近盛大刚刚发布了财报,有人给我打电话问什么是ARPDAU?ARPDAU能够起到什么作用?本文就这个问题给大家解析一下ARPDAU.在讲ARPDAU之前,有两个概念大家应该很清楚,一个是ARPU,另一 ...
- easyui tree 编辑后保留原先状态
$(function () { var selected = $('#depttree').tree('getSelected'); $('#depttree').tree({ checkbox: f ...
- javascript提升复习
https://www.baidu.com/s?wd=JavaScript+%E9%A2%84%E8%A7%A3%E6%9E%90 http://www.cnblogs.com/HPNiuYear/a ...
- Spark使用总结与分享
背景 使用spark开发已有几个月.相比于python/hive,scala/spark学习门槛较高.尤其记得刚开时,举步维艰,进展十分缓慢.不过谢天谢地,这段苦涩(bi)的日子过去了.忆苦思甜,为了 ...
- 参数传递的四种形式----- URL,超链接,js,form表单
什么时候用GET, 查,删, 什么时候用POST,增,改 (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http:/ ...
- metaq安装实例
下载metaq: http://fnil.net/downloads/index.html 安装metaq: [root@localhost software]# pwd /export/softwa ...