android读写pc文件,在pc端和安卓端创建、写入、读取txt文件

需要using System.IO;

主要代码如下:

private string fileDir; // 文件路径

public void Init()

{

// 创建文件夹路径

string dir = "";

#if UNITY_EDITOR

dir = Application.dataPath.Replace("Assets", "/GameData"); //Application.dataPath在电脑上为Assets资源文件夹的绝对路径,但是在移动端它是完全没用

#endif

#if UNITY_ANDROID && !UNITY_EDITOR

dir = Application.persistentDataPath + "/GameData"; //Application.persistentDataPath路径,这个路径可读、可写,但是只能在程序运行时才能读写操作,不能提前将数据放入这个路径。

#endif

if (!Directory.Exists(dir))

Directory.CreateDirectory(dir);

fileDir = dir + "/GameData.txt";

Debug.Log("fileDir=" + fileDir);

if (!File.Exists(fileDir))

{

InitGameData(); // 初始化关卡数据

}

else

{

ReadGameData();

}

}

private void InitGameData()

{

Debug.Log("初始化关卡数据");

string strsWrite = null;

// 创建txt文件,用于存储GameData

StreamWriter sw = File.CreateText(fileDir);

// 初始化数据.....

// 写入文档

sw.Write(strsWrite);

sw.Flush();

sw.Close();

}

private void ReadGameData()

{

Debug.Log("读取关卡数据");

// 读取关卡数据

string[] strsRead = File.ReadAllLines(fileDir); // 读取文件的所有行,并将数据读取到定义好的字符串数组,一行存一个单元

}

// 保存关卡数据

public void SaveGameData()

{

Debug.Log("SaveGameData");

string strsWrite = null;

// 关卡数据....

// 写入文档

File.WriteAllText(fileDir, strsWrite);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值