位置: 编程技术 - 正文
推荐整理分享Unity WWW网络动态加载和储存在本地,希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!
原文地址: 关于加载:首先是检查本地文件里是否存在相同的资源包文件(检查和校验版本号),如果都是正确的就不需要从服务器端下载了直接从本地(手机就是SD卡里了)加载就可以了。 如果版本号不对那么就要下载最新版本的资源了,当然要把老版本的从本地删除,不然在手机里会很占储存空间的。 新建一个Resource类,作为加载工具类继承与MonoBehaviour。 新建方法,传入必要信息提供加载: System.IO包里提供了一个File类,其Exists方法就是查询指定路径下是否有指定的文件存在。
[AppleScript] 纯文本查看 复制代码?public static stringsuffix = “.unity3d”;//资源包后缀 public static Dictionary<string,GameObject>cache =new Dictionary<string,GameObject>(); public void load(stringpath,stringname,intversion){ if(!File.Exists(Application.persistentDataPathname suffix version)){ StartCoroutine(loader(path,name,version));//网络加载 }else{ StartCoroutine(loadBundleFromLocal(path,name,version));//本地加载 } } 在加载时需要启动一个协程,异步加载。如果加载成功则就写入本地和内存里方便调用,如果有新的版本则需要吧老版本的删除掉。[AppleScript] 纯文本查看 复制代码?private IEnumerator loadBundle(stringpath,stringname,intversion){ print(“fuck you”); int oldVersion =version - 1;//写死了 stringpathName =Application.persistentDataPath path name; stringurl = path name suffix; using(WWW www =newWWW(url)){ yieldreturnwww; if(www.error==null){ if(File.Exists(pathNameoldVersion)){ File.Delete(pathNameoldVersion); Debug.Log(“deleteold versiondata succeed...”); } if(!File.Exists(pathNameversion)){ //writefile in local File.WriteAllBytes(pathNameversion,www.bytes); //other function // FileStream fs =new FileStream(Application.persistentDataPathpath name version,FileMode.OpenOrCreate); // fs.Write(www.bytes,0,www.bytes.Length); // fs.Flush(); // fs.Close(); Debug.Log(“loadbundlesucceed,writefile path:”pathNameversion); }else{ Debug.Log(“writefile error...”); } AssetBundle ab =www.assetBundle; GameObject go =ab.mainAsset asGameObject; cache[name]=go; Resource.go=go; //ab.Unload(false); } } } 从本地加载就更简单了,直接把本地文件所在的路径传给WWW作为参数就可以了。[AppleScript] 纯文本查看 复制代码?private IEnumerator loadBundleFromLocal(stringpath,stringname,intversion){ using(WWW www =new WWW(“ name version)){ yieldreturnwww; if(www.error==null){ AssetBundle ab =www.assetBundle; GameObject go =ab.mainAsset asGameObject; cache[name]=go; Resource.go=go; Debug.Log(“loaddatafrome localsucceed...”); } } } 好了,Resource这个类就基本建立完成了。准备调用~ 一般思路:[AppleScript] 纯文本查看 复制代码?usingUnityEngine; usingSystem.Collections; publicclassLoadUIResponse :MonoBehaviour { public Resource rs; void Start(){ if(rs==null){ rs=new Resource(); } intversion= 1; rs.load(“ } } 但是在运行时会报出异常:NullReferenceException UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine)。 这是咋回事,原来我的Resource是继承与MonoBehaviour的。看看这个警告: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() 这就要注意了,这个Resource不能用new。是需要加在一个显示组件上通过AddComponent()方法来获取的。 so...正确做法如下: 在响应组件上添加Resource脚本,和UIResponse脚本。 在UIResponse上添加代码:[AppleScript] 纯文本查看 复制代码?private Resource rs; stringpath = “ int version= 1; stringname = ”monster1“; void start(){ rs=gameObject.GetComponent(typeof(Resource))as Resource; Resource.instance=rs; } void OnMouseDown(){ Resource.instance.load(path,name,version); } 这样,随着游戏的初始化,加载工具类Resource就被初始化出来了,并且已经存放在Resource的instance变量里。以后就可以直接使用Resource.instance来调用加载了。 并且已有的加载都储存在cache里了,也可以先根据name到内存里去查找一遍再去加载。 最终加载出来的gameObject需要Instantiate实例化出来。 以后就可以直接使用Resource.instance来访问Resource里的个个加载方法了。 Resource的instance的get/set方法的写法如下:[AppleScript] 纯文本查看 复制代码?private static Resource _instance; //添加get和set public static Resource instance{ set{ _instance=value; } get{ if(_instance==null){ //_instance=new Resource(); Debug.Log(”noinstance.“); } return_instance; } } 看起来和单例的创建方法很类,如果直接使用调用单例的方式来调用的话。执行到建立协程StartCoroutine时就会抛出空引用的异常,并出现不能创建新的MonoBehaviour实例的警告。Spline Power 场景插件 插件主要用于构建由重复片段组成的物体,例如赛道:创作者可以使赛道呈现不同形态SplineFormerSegmentMesh:重复片段的模型MeshFiter:片段的材质Caps:片段
Unity中实现全局管理类的几种方式 如何在Unity中实现全局管理类?由于Unity脚本的运行机制和面向组件编程(COP)的思想,实现起来和普通的方式略有差别。第一种方式是使用静态类。适
Unity3D(二)用反射、泛型读取XML后动态创建实例并赋值 前言:最近小匹夫参与的游戏项目到了需要读取数据的阶段了,那么觉得自己业余时间也该实践下数据相关的内容。那么从哪入手呢?因为用的是unity3d
下一篇:Unity中实现全局管理类的几种方式(unity全屏)
友情链接: 武汉网站建设