四个平台同理类推:android,pc,edit,iphone
第一使用条件编译指令:
优点是选择性编译,只会编译符合条件的
话不多说直接上代码
默认读取的文件名字是 string fileName
#if UNITY_EDITOR || UNITY_STANDALONE
//编译器&pc
url = "file://" + Application.dataPath + "/StreamingAssets/"+fileName;
#elif UNITY_IPHONE
//iphone
url = "file://" + Application.dataPath + "/Raw/"+fileName;
#elif UNITY_ANDROID
//aAndroid
url = "jar:file://" + Application.dataPath + "!/assets/"+fileName;
#endif
eg:如果是android平台,那么前面两个代码就不会被带走
2使用Application类进行判断
//and
if (Application.platform == RuntimePlatform.Android)
{
url = "jar:file://" + Application.dataPath + "!/assets/" + fileName;
}
else
{
//iPhone
if(Application.platform ==RuntimePlatform.IPhonePlayer)
{
url = "file://" + Application.dataPath + "/Raw/" + fileName;
}
else
{
//edit&pc
url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
}
}
最后顺便附上一个WWW类读取文件
WWW www = new WWW(url);
while(true)
{
if(www.isDone)
return www.text;
}