标签:
用lua就表示项目用到了热更新,通常每次热更新都会从服务器获取最新的lua脚本放到Android/ios设备的本地目录下,但是lua应该放到哪个目录下呢,这里就先说说lua里面的路径问题
1、不可以放到Resources文件夹下,因为无法找到其在设备下的位置,Resources下的东西是由unity来管理的,使用的时候通过Resources.load/loadAll来加载
2、不可以放到StreamingAssets文件夹,因为在设备下StreamingAssets文件夹里面的资源只能读,不能写,当我们热更新最新的资源的时候,是不好操作的!而且StreamingAssets文件夹下的东西最终在生成apk的时候会被unity打包进一个pkg.apk的apk安装包里,解压这个pkg.apk可以看到StreamingAssets文件夹下的资源,在unity出包的时候,设置PlayerSettings下的Install Location为Perfer External(优先安装到外部设备,也就是sdcard),安装此包,用文件查看器查看,才能看到此程序安装的文件,通常如下:
com.zwh.p1是我的包名,如果程序就是安装到外部,那么都是能看到他的安装文件的,其他应用也是如此
如果Install Location设置为Force Internal(强制安装到手机内存),那么是看不到程序的安装文件的!
如何访问StreamingAssets文件下的资源:
string GetStreamingAssetsPath() { return #if UNITY_ANDROID //"jar:file://" + Application.dataPath + "!/assets/"; UnityEngine.Application.streamingAssetsPath+"/"; #elif UNITY_IPHONE Application.dataPath + "/Raw/"; #elif UNITY_STANDALONE_WIN || UNITY_EDITOR "file://" + Application.dataPath + "/StreamingAssets/"; #else string.Empty; #endif }
3、先看看unity中几个路径有关的类,在android平台输出的路径为什么
Debug.Log ("temporaryCachePath = " + UnityEngine.Application.temporaryCachePath); //temporaryCachePath = /storage/sdcard0/Android/data/com.zwh.p1/cache Debug.Log ("dataPath = " + UnityEngine.Application.dataPath); //dataPath = /mnt/asec/com.zwh.p1-2/pkg.apk Debug.Log ("persistentDataPath = " + UnityEngine.Application.persistentDataPath); //persistentDataPath = /storage/sdcard0/Android/data/com.zwh.p1/files Debug.Log ("streamingAssetsPath = " + UnityEngine.Application.streamingAssetsPath); //streamingAssetsPath = jar:file:///mnt/asec/com.zwh.p1-2/pkg.apk!/assets
可以看出dataPath和streamingAssetsPath指的是程序安装的位置,而temporaryCachePath和persistentDataPath指的则是sdcard的位置,所以可以选择这两个路径作为存放lua文件的地方,前提是unity出包的时候,要设置Playersettings下Write Access为External(SDCard),不然这两个路径的位置是不一样的!
标签:
原文地址:http://www.cnblogs.com/MrZivChu/p/UnityLua.html