一、代码:
1.通过C#的IO进行文件内容读取:
using UnityEngine.UI;
public class Test : MonoBehaviour {
Text text;
string result = "";
// Use this for initialization
void Start () {
text = GetComponent<Text>();
ReadIP();
}
void ReadIP()
{
#if UNITY_EDITOR
string filepath = Application.dataPath + "/StreamingAssets" + "/ip.txt";
#elif UNITY_IPHONE
//没有试过
string filepath = Application.dataPath +"/Raw"+"/ip.txt";
#elif UNITY_ANDROID
//android路径:/storage/emulated/0/Android/data/包名/files/ip.txt
string filepath = Application.persistentDataPath + "/ip.txt";
#endif
result = filepath;
if (File.Exists(filepath))
{
result += "\n" + File.ReadAllText(filepath);
}
}
// Update is called once per frame
void Update () {
text.text = result;
}
}
二、参考博客:
1.https://blog.csdn.net/u014147126/article/details/81276707
三、注意
1.代码中的路径设置PlayerSetting下:
2、Application.persistentDataPath 路径(推荐使用)
此属性返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。这个路径可读、可写,但是只能在程序运行时才能读写操作,不能提前将数据放入这个路径。在IOS上是应用程序的沙盒,可以被iCloud自动备份,可以通过同步推送一类的助手直接取出文件;在Android上的位置是根据Project Setting里设置的Write Access路径,可以设置是程序沙盒还是sdcard,注意:如果在Android设置保存在沙盒中,那么就必须root以后才能用电脑取出文件,因此建议写入sdcard里。一般情况下,建议将获得的文件保存在这个路径下,例如可以从StreamingAsset中读取的二进制文件或者从AssetBundle读取的文件写入PersistentDatapath。
3、Application.temporaryCachePath 路径
此属性返回一个临时数据的缓存目录,跟Application.persistentDataPath类似,但是在IOS上不能被自动备份。
4、 /sdcard/.. 路径
表示Android手机的SD卡根目录。
5、 /storage/emulated/0/.. 路径
表示Android手机的内置存储根目录。
以上各路径中的资源加载方式都可以用WWW类加载,但要注意各个平台路径需要加的访问名称,例如Android平台的路径前要加”jar:file://”,其他平台使用”file://”。