在Unity3D的网络游戏中实现资源动态加载
用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); } }}。。。}。。。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;}。。。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;}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); }。。。。。。 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; } } } } 。。。。。。 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;}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;}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; }}