用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载。比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕。应该优先加载用户附近的场景资源,在游戏的过程中,不影响操作的情况下,后台加载剩余的资源,直到所有加载完毕。
本文包含一些代码片段讲述实现这个技术的一种方法。本方法不一定是最好的,希望能抛砖引玉。代码是C#写的,用到了Json,还有C#的事件机制。
在讲述代码之前,先想象这样一个网络游戏的开发流程。首先美工制作场景资源的3D建模,游戏设计人员把3D建模导进Unity3D,托托拽拽编辑场景,完成后把每个gameobject导出成XXX.unity3d格式的资源文件(参看BuildPipeline),并且把整个场景的信息生成一个配置文件,xml或者Json格式(本文使用Json)。最后还要把资源文件和场景配置文件上传到服务器,最好使用CMS管理。客户端运行游戏时,先读取服务器的场景配置文件,再根据玩家的位置从服务器下载相应的资源文件并加载,然后开始游戏,注意这里并不是下载所有的场景资源。在游戏的过程中,后台继续加载资源直到所有加载完毕。
一个简单的场景配置文件的例子:
MyDemoSence.txt
- {
- "AssetList" : [{
- "Name" : "Chair 1",
- "Source" : "Prefabs/Chair001.unity3d",
- "Position" : [2,0,-5],
- "Rotation" : [0.0,60.0,0.0]
- },
- {
- "Name" : "Chair 2",
- "Source" : "Prefabs/Chair001.unity3d",
- "Position" : [1,0,-5],
- "Rotation" : [0.0,0.0,0.0]
- },
- {
- "Name" : "Vanity",
- "Source" : "Prefabs/vanity001.unity3d",
- "Position" : [0,0,-4],
- "Rotation" : [0.0,0.0,0.0]
- },
- {
- "Name" : "Writing Table",
- "Source" : "Prefabs/writingTable001.unity3d",
- "Position" : [0,0,-7],
- "Rotation" : [0.0,0.0,0.0],
- "AssetList" : [{
- "Name" : "Lamp",
- "Source" : "Prefabs/lamp001.unity3d",
- "Position" : [-0.5,0.7,-7],
- "Rotation" : [0.0,0.0,0.0]
- }]
- }]
- }
主程序:复制代码
- 。。。
- public class MainMonoBehavior : MonoBehaviour {
- public delegate void MainEventHandler(GameObject dispatcher);
- public event MainEventHandler StartEvent;
- public event MainEventHandler UpdateEvent;
- public void Start() {
- ResourceManager.getInstance().LoadSence("Scenes/MyDemoSence.txt");
- if(StartEvent != null){
- StartEvent(this.gameObject);
- }
- }
- public void Update() {
- if (UpdateEvent != null) {
- UpdateEvent(this.gameObject);
- }
- }
- }
- 。。。
- }
ResourceManager.cs复制代码
- 。。。
- private MainMonoBehavior mainMonoBehavior;
- private string mResourcePath;
- private Scene mScene;
- private Asset mSceneAsset;
- private ResourceManager() {
- mainMonoBehavior = GameObject.Find("Main Camera").GetComponent<MainMonoBehavior>();
- mResourcePath = PathUtil.getResourcePath();
- }
- public void LoadSence(string fileName) {
- mSceneAsset = new Asset();
- mSceneAsset.Type = Asset.TYPE_JSON;
- mSceneAsset.Source = fileName;
- mainMonoBehavior.UpdateEvent += OnUpdate;
- }
- 。。。
在LoadSence方法里先创建一个Asset的对象,这个对象是对应于配置文件的,设置type是Json,source是传进来的“Scenes/MyDemoSence.txt”。然后注册MainMonoBehavior的update事件。复制代码
- public void OnUpdate(GameObject dispatcher) {
- if (mSceneAsset != null) {
- LoadAsset(mSceneAsset);
- if (!mSceneAsset.isLoadFinished) {
- return;
- }
- //clear mScene and mSceneAsset for next LoadSence call
- mScene = null;
- mSceneAsset = null;
- }
- mainMonoBehavior.UpdateEvent -= OnUpdate;
- }
最核心的LoadAsset方法:复制代码
- private Asset LoadAsset(Asset asset) {
- string fullFileName = mResourcePath + "/" + asset.Source;
- //if www resource is new, set into www cache
- if (!wwwCacheMap.ContainsKey(fullFileName)) {
- if (asset.www == null) {
- asset.www = new WWW(fullFileName);
- return null;
- }
- if (!asset.www.isDone) {
- return null;
- }
- wwwCacheMap.Add(fullFileName, asset.www);
- }
- 。。。
继续LoadAsset方法:复制代码
- 。。。
- if (asset.Type == Asset.TYPE_JSON) { //Json
- if (mScene == null) {
- string jsonTxt = mSceneAsset.www.text;
- mScene = JsonMapper.ToObject<Scene>(jsonTxt);
- }
- //load scene
- foreach (Asset sceneAsset in mScene.AssetList) {
- if (sceneAsset.isLoadFinished) {
- continue;
- } else {
- LoadAsset(sceneAsset);
- if (!sceneAsset.isLoadFinished) {
- return null;
- }
- }
- }
- }
- 。。。
继续LoadAsset方法:复制代码
- 。。。
- else if (asset.Type == Asset.TYPE_GAMEOBJECT) { //Gameobject
- if (asset.gameObject == null) {
- wwwCacheMap[fullFileName].assetBundle.LoadAll();
- GameObject go = (GameObject)GameObject.Instantiate(wwwCacheMap[fullFileName].assetBundle.mainAsset);
- UpdateGameObject(go, asset);
- asset.gameObject = go;
- }
- if (asset.AssetList != null) {
- foreach (Asset assetChild in asset.AssetList) {
- if (assetChild.isLoadFinished) {
- continue;
- } else {
- Asset assetRet = LoadAsset(assetChild);
- if (assetRet != null) {
- assetRet.gameObject.transform.parent = asset.gameObject.transform;
- } else {
- return null;
- }
- }
- }
- }
- }
- asset.isLoadFinished = true;
- return asset;
- }
UpdateGameObject方法:复制代码
- private void UpdateGameObject(GameObject go, Asset asset) {
- //name
- go.name = asset.Name;
- //position
- Vector3 vector3 = new Vector3((float)asset.Position[0], (float)asset.Position[1], (float)asset.Position[2]);
- go.transform.position = vector3;
- //rotation
- vector3 = new Vector3((float)asset.Rotation[0], (float)asset.Rotation[1], (float)asset.Rotation[2]);
- go.transform.eulerAngles = vector3;
- }
最后是Scene和asset代码:复制代码
- public class Scene {
- public List<Asset> AssetList {
- get;
- set;
- }
- }
- public class Asset {
- public const byte TYPE_JSON = 1;
- public const byte TYPE_GAMEOBJECT = 2;
- public Asset() {
- //default type is gameobject for json load
- Type = TYPE_GAMEOBJECT;
- }
- public byte Type {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string Source {
- get;
- set;
- }
- public double[] Bounds {
- get;
- set;
- }
- public double[] Position {
- get;
- set;
- }
- public double[] Rotation {
- get;
- set;
- }
- public List<Asset> AssetList {
- get;
- set;
- }
- public bool isLoadFinished {
- get;
- set;
- }
- public WWW www {
- get;
- set;
- }
- public GameObject gameObject {
- get;
- set;
- }
- }
代码就讲完了,在我实际测试中,会看到gameobject一个个加载并显示在屏幕中,并不会影响到游戏操作。代码还需要进一步完善适合更多的资源类型,如动画资源,文本,字体,图片和声音资源。
动态加载资源除了网络游戏必需,对于大公司的游戏开发也是必须的。它可以让游戏策划(负责场景设计),美工和程序3个角色独立出来,极大提高开发效率。试想如果策划改变了什么NPC的位置,美工改变了某个动画,或者改变了某个程序,大家都要重新倒入一遍资源是多么低效和麻烦的一件事。