Unity安卓打包

试错:


//string filepath = Application.streamingAssetsPath + "/navy.cdb";




string filepath = Application.streamingAssetsPath + "/navy.cdb";
if(!File.Exists(filepath)){
WWW loadDB = new WWW( filepath );  // this is the path to your StreamingAssets in android

while(!loadDB.isDone) {}  // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check


File.WriteAllBytes(filepath, loadDB.bytes);
}
string connection = "URI=file:" + filepath;
this._Config = new CDBProject( connection );




/*
string AndroidPath = Application.persistentDataPath + "/navy.cdb";
if(!File.Exists(AndroidPath))
{
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "navy.cdb"); 
File.WriteAllBytes(AndroidPath, loadDB.bytes);
}
//this._Config = new CDBProject("URI=file:" + AndroidPath);
*/

//this._Config = new CDBProject("jar:file://" + Application.dataPath + "!/assets/navy.cdb");
//this._Config = new CDBProject("jar:file://" + Application.dataPath + "!/assets/navy.cdb");
//this._Config = new CDBProject("jar:file://" + Application.streamingAssetsPath.ToString() + "!/assets/navy.cdb");
//this._Config = new CDBProject(Application.streamingAssetsPath.ToString() + "/navy.cdb");






##################################
##################################




Application.dataPath: 只可读不可写,放置一些资源数据
Application.persistentDataPath 可读可写,可以放一些存档文件


这个关键在于一个设置,就是player setting中的write access。打开后,应用程序才有权限写到sdcard中。这个时候Application.persistentDataPath始终是/sdcard/Android/data/com.xxx.xxx/files,www的缓存地址在/sdcard/Android/data/下面。




5. Unity常用目录对应的Android && iOS平台地址
IOS:
Application.dataPath : Application/xxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxx/Documents
Application.temporaryCachePath : Application/xxxxx/Library/Caches




Android:
Application.dataPath : /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath : /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath : /data/data/xxx.xxx.xxx/cache




 代码中我们保存文件的路径是Application.persistentDataPath。 如果你写的路径是 Application.dataPath在编辑器中是可以正常读取,但是在IOS与Android中是无法读取的,昨天问我的那个朋友就是因为这里路径写的有问题没能成功的写入文件。 Application.persistentDataPath路径就是将文件保存在手机的沙盒中,如果在编辑器中运行本程序文件将保存在Finder-》 资源库-》Caches-》你的工程-》保存的文件 。本例的路径就是 Finder->资源库-> Caches -> txt->FileName.txt。




安卓上跟其他平台不一样,安装后,这些文件实际上是在一个Jar压缩包里,所以不能直接用读取文件的函数去读,而要用WWW方式。具体做法如下: 
//1.把你要读取的文件放在Unity项目的Assets/StreamingAssets文件夹下面,没有这个文件夹的话自己建一个。 
//2.读取的代码(假设名为"文件.txt") 
byte[] InBytes;                                                                                                        //用来存储读入的数据 
if (Application.platform == RuntimePlatform.Android)                                            //判断当前程序是否运行在安卓下 

        string FileName = "jar:file://" + Application.dataPath + "!/assets/" + "文件.txt"; 
        WWW www = new WWW(FileName);                                                            //WWW会自动开始读取文件 
        while(!www.isDone){}                                                                       //WWW是异步读取,所以要用循环来等待 
        InBytes = www.bytes;                                                                       //存到字节数组里 



else 

                        //其他平台的读取代码 
}








string filepath = Application.persistentDataPath + "/navy.cdb";
if(!File.Exists(filepath)){
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "navy.cdb");  // this is the path to your StreamingAssets in android

while(!loadDB.isDone) {}  // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check


File.WriteAllBytes(filepath, loadDB.bytes);
}
string connection = "URI=file:" + filepath;
this._Config = new CDBProject( connection );
s










On iOS, you should use:
 path = Application.dataPath + "/Raw";
On Android, you should use:
 path = "jar:file://" + Application.dataP  ath + "!/assets/";




如果不用assetsbundle,发布时是没法自动把不在resources文件夹里的文档包括进去的,同时在发布后是无法对resources里面的东西进行更改的,也就是说没法实现写入。


解决方法是把必须要发布的文件放在resources里面,第一次启动的时候,把resources里面的东西读取进内存,然后存储到自己想要存的文件夹里面去。然后用destroy(_obj,true)来直接删除掉resource里面的这个文档资源(也可以留着不删)


在移动平台上发布后,ios wp这两个平台对文件夹的读写权限是有严格限制的。因此application.datapath是无法支持写入的。这个时候要换用application.persistentdatapath,才有写入的权限。只要用户不卸载这个应用,这个文件夹是不会被删除或者更改的(包括更新应用,也不会去更改这个文件夹)。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值