Unity与安卓开发的一些路径知识

时间:2023-03-08 22:14:57
Unity与安卓开发的一些路径知识

APK安装之后找不到路径

公司的测试机(安卓)基本都是不带SD卡的。

APK在安卓手机上安装之后,使用手机助手类的软件打开文件管理,打开 内置SDK卡/Android/data/ 在这个目录下却发现 找不到以应用的包名(com.xxx.xxx) 开头的文件夹,那比如要打开这个目录查看里面的文件呢?

Unity与安卓开发的一些路径知识

但是却能看到一些其它APP的目录,那么请检查以下设置:

1、打开 FileBuild Settings -  选择 Player Settings ,请确认已经切换到了Android 平台,找到 Configuration 这一部分设置

2、Install Location 选择 Automatic

Write Permission 选择 External (SDCard)

Unity与安卓开发的一些路径知识

3、重新打包APK,并安装,就可以在文件管理中找到这个目录了。

安卓的写入路径

比如你想在安装目录下创建一个目录并往里面写入文件,路径建议这样写:(和windows下的路径符号不同,而是和浏览器中网络的路径符号相同)

string savePath = Application.persistentDataPath + "/" + "SaveTextures/";

而如果你这样写,那么极有可能出现错误!

string savePath = Application.persistentDataPath + "\\SaveTextures\\"

我在安卓上测试,会出现文件名变成:files\SaveTextures\2017-01-13_02-12-54.png 也就是说文件名变成了路径,所以当你使用路径加载时,就会报文件不存在。

WWW加载的文件协议

使用WWW 加载非Assetbundle文件,比如原始的音乐文件(mp3,wav),原始的贴图文件(png,jpg)

比如这个文件放在应用程序的沙盒内或SD卡内:Application.persistentDataPath

public static string GetFileProtocol()
{
string fileProtocol = "file://";
if (Application.platform == RuntimePlatform.WindowsEditor ||
Application.platform == RuntimePlatform.WindowsPlayer
#if !UNITY_5_4_OR_NEWER
|| Application.platform == RuntimePlatform.WindowsWebPlayer
#endif
)
{
fileProtocol = "file:///";
} return fileProtocol;
}

使用 www 加载示例:

public static IEnumerator LoadByWWW(string fullFilePath, Action<Texture2D> callback)
{
string loadPath = GetFileProtocol() + fullFilePath;
WWW www = new WWW(loadPath);
yield return www;
if (www != null && string.IsNullOrEmpty(www.error))
{
//获取Texture
Texture2D texture = www.texture;
if (callback != null) callback(texture);
}
else
{
Log.LogError("www 加载图片失败:{0}", www.error);
if (callback != null) callback(null);
}
}

测试环境

本文的测试环境如下:

Unity 5.5.0f3

安卓4.2.3