最新unity动态加载.doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《最新unity动态加载.doc》由会员分享,可在线阅读,更多相关《最新unity动态加载.doc(33页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、精品资料unity动态加载.用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载。比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕。应该优先加载用户附近的场景资源,在游戏的过程中,不影响操作的情况下,后台加载剩余的资源,直到所有加载完毕。 本文包含一些代码片段讲述实现这个技术的一种方法。本方法不一定是最好的,希望能抛砖引玉。代码是C#写的,用到了Json,还有C#的事件机制。 在讲述代码之前,先想象这样一个网络游戏的开发流程。首先美工制作场景资源的3D建模,游戏设计人员把3D建模导进Unity3D,托托拽拽编辑场景,完成后把每个gam
2、eobject导出成XXX.unity3d格式的资源文件(参看BuildPipeline),并且把整个场景的信息生成一个配置文件,xml或者Json格式(本文使用Json)。最后还要把资源文件和场景配置文件上传到服务器,最好使用CMS管理。客户端运行游戏时,先读取服务器的场景配置文件,再根据玩家的位置从服务器下载相应的资源文件并加载,然后开始游戏,注意这里并不是下载所有的场景资源。在游戏的过程中,后台继续加载资源直到所有加载完毕。 一个简单的场景配置文件的例子: MyDemoSence.txt Json代码 AssetList : Name : Chair 1, Source : Prefab
3、s/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/writin
4、gTable001.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 AssetList : Name : Chair 1, Source : Prefabs/Chair001.unity3d, Position : 2,0,-5, Rotation : 0.0,60.0,0.0 , Name : Chair 2, Source :
5、 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, So
6、urce : Prefabs/lamp001.unity3d, Position : -0.5,0.7,-7, Rotation : 0.0,0.0,0.0 AssetList:场景中资源的列表,每一个资源都对应一个unity3D的gameobject Name:gameobject的名字,一个场景中不应该重名 Source:资源的物理路径及文件名 Position:gameobject的坐标 Rotation:gameobject的旋转角度 你会注意到Writing Table里面包含了Lamp,这两个对象是父子的关系。配置文件应该是由程序生成的,手工也可以修改。另外在游戏上线后,客户端接收
7、到的配置文件应该是加密并压缩过的。 主程序: C#代码。 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
8、.txt); if(StartEvent != null) StartEvent(this.gameObject); public void Update() if (UpdateEvent != null) UpdateEvent(this.gameObject); 。 。public class MainMonoBehavior : MonoBehaviour public delegate void MainEventHandler(GameObject dispatcher); public event MainEventHandler StartEvent; public event
9、 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); 。这里面用到了C#的事件机制,大家可以看看我以前翻译过的国外一个牛人的文章。C# 事件和Unity3D 在sta
10、rt方法里调用ResourceManager,先加载配置文件。每一次调用update方法,MainMonoBehavior会把update事件分发给ResourceManager,因为ResourceManager注册了MainMonoBehavior的update事件。 ResourceManager.cs C#代码。 private MainMonoBehavior mainMonoBehavior; private string mResourcePath; private Scene mScene; private Asset mSceneAsset; private Resource
11、Manager() mainMonoBehavior = GameObject.Find(Main Camera).GetComponent(); mResourcePath = PathUtil.getResourcePath(); public void LoadSence(string fileName) mSceneAsset = new Asset(); mSceneAsset.Type = Asset.TYPE_JSON; mSceneAsset.Source = fileName; mainMonoBehavior.UpdateEvent += OnUpdate; 。privat
12、e MainMonoBehavior mainMonoBehavior;private string mResourcePath;private Scene mScene;private Asset mSceneAsset;private ResourceManager() mainMonoBehavior = GameObject.Find(Main Camera).GetComponent(); mResourcePath = PathUtil.getResourcePath();public void LoadSence(string fileName) mSceneAsset = ne
13、w Asset(); mSceneAsset.Type = Asset.TYPE_JSON; mSceneAsset.Source = fileName; mainMonoBehavior.UpdateEvent += OnUpdate;。在LoadSence方法里先创建一个Asset的对象,这个对象是对应于配置文件的,设置type是Json,source是传进来的“Scenes/MyDemoSence.txt”。然后注册MainMonoBehavior的update事件。 C#代码public void OnUpdate(GameObject dispatcher) if (mSceneAs
14、set != null) LoadAsset(mSceneAsset); if (!mSceneAsset.isLoadFinished) return; /clear mScene and mSceneAsset for next LoadSence call mScene = null; mSceneAsset = null; mainMonoBehavior.UpdateEvent -= OnUpdate; public void OnUpdate(GameObject dispatcher) if (mSceneAsset != null) LoadAsset(mSceneAsset)
15、; if (!mSceneAsset.isLoadFinished) return; /clear mScene and mSceneAsset for next LoadSence call mScene = null; mSceneAsset = null; mainMonoBehavior.UpdateEvent -= OnUpdate;OnUpdate方法里调用LoadAsset加载配置文件对象及所有资源对象。每一帧都要判断是否加载结束,如果结束清空mScene和mSceneAsset对象为下一次加载做准备,并且取消update事件的注册。 最核心的LoadAsset方法: C#代码p
16、rivate 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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 最新 unity 动态 加载
![提示](https://www.deliwenku.com/images/bang_tan.gif)
限制150内