本篇接着上一篇继续和大家分享场景资源这一主题,主要包括两个方面:

(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系列——场景资源之解包(二)的更多相关文章

  1. AssetBundle系列——场景资源之打包(一)

    本篇讲解的是3D游戏的场景资源打包方式,首先简单的分析一下场景中所包含的资源的类型. 场景资源一般包含:地表模型(或者是Unity Terrain),非实例化物体(摄像机.空气墙.光源.各种逻辑物体之 ...

  2. AssetBundle系列——游戏资源打包(二)

    本篇接着上一篇.上篇中说到的4步的代码分别如下所示: (1)将资源打包成assetbundle,并放到自定目录下 using UnityEditor; using UnityEngine; using ...

  3. (转)AssetBundle系列——游戏资源打包(二)

    转自:http://www.cnblogs.com/sifenkesi/p/3557290.html 本篇接着上一篇.上篇中说到的4步的代码分别如下所示: (1)将资源打包成assetbundle,并 ...

  4. AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...

  5. AssetBundle系列——游戏资源打包(一)

    将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新.服务器上包含以下资源列表:(1)游戏内容资源assetbundle(2)资源维护列表,包含每个资源的名字(完整路径名)和对应的版本号[资源名 ...

  6. [Unity Asset]AssetBundle系列——游戏资源打包

    转载:http://www.cnblogs.com/sifenkesi/p/3557231.html 将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新.服务器上包含以下资源列表:(1)游戏内 ...

  7. (转)AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...

  8. (转)AssetBundle系列——游戏资源打包(一)

    转自:http://www.cnblogs.com/sifenkesi/p/3557231.html 将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新.服务器上包含以下资源列表:(1)游戏内 ...

  9. 从零系列--开发npm包(二)

    一.利用shell简化组合命令 set -e CVERSION=$(git tag | ) echo "current version:$CVERSION" echo " ...

随机推荐

  1. C++11中async中future用法(一)

    async意味着异步执行代码,看如下示例: #include <future> #include <thread> #include <chrono> #inclu ...

  2. Leetcode 8 String to Integer (atoi) 字符串处理

    题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...

  3. Leetcode 119 Pascal's Triangle II 数论递推

    杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...

  4. LDR 和 ADR 彻底详解

    0.什么是位指令? 答:伪指令(Pseudo instruction)是用于告诉汇编程序如何进行汇编的指令.它既不控制机器的操作也不被汇编成机器代码, 只能为汇编程序所识别并指导汇编如何进行. 1.L ...

  5. 运行(WIN+R)中能使用的命令:ms-settings:,shell:,cpl,mmc...

    ms-settings: --- DESC --- --- CMD --- Battery Saver ms-settings:batterysaver Battery Saver Settings ...

  6. jQuery 之父:每天写代码

    去年秋天我的支线代码项目 遇到了一些问题,项目进展不足,而且我没法找到一个完成更多代码的方法(在不影响我在Khan Academy方面的工作的前提下). 我主要在周末进行我的支线,当然有时候也在晚上进 ...

  7. 移动APP的IM后台架构浅析

    IM(InstantMessaging 即时通讯)作为一项基础功能,很多APP都有,比如:手机QQ.微信.易信.钉钉.飞信.旺旺.咚咚.陌陌等.而IM如同我们日常生活中的水和电一样,必不可少,也是很多 ...

  8. WebClient的异步处理

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Te ...

  9. 机器学习基石--学习笔记02--Hard Dual SVM

    背景 上一篇文章总结了linear hard SVM,解法很直观,直接从SVM的定义出发,经过等价变换,转成QP问题求解.这一讲,从另一个角度描述hard SVM的解法,不那么直观,但是可以避免fea ...

  10. javascript - encodeURI和encodeURIComponent的区别

    这两个函数功能上面比较接近,但是有一些区别. encodeURI:不会进行编码的字符有82个 :!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z, ...