Unity3d 文件的创建,写入,和读取

128 篇文章 1 订阅
64 篇文章 1 订阅

文件的创建,写入,读取,需要使用流来操作。

示例代码如下:

  1. void Start ()
  2.     {
  3.         Createfile (Application.dataPath, "FileName", "TestInfo0");
  4.         Createfile (Application.dataPath, "FileName", "TestInfo1");
  5.         Createfile (Application.dataPath, "FileName", "TestInfo2");
  6.     
  7.     }
  8.     //文件的创建,写入
  9.     void Createfile (string path, string name, string info)
  10.     {
  11.         StreamWriter sw;//流信息
  12.         FileInfo t = new FileInfo (path + "//" + name);
  13.         if (!t.Exists) {//判断文件是否存在
  14.             sw = t.CreateText ();//不存在,创建
  15.         } else {
  16.             sw = t.AppendText ();//存在,则打开
  17.         }
  18.         sw.WriteLine (info);//以行的形式写入信息
  19.         sw.Close ();//关闭流
  20.         sw.Dispose ();//销毁流
  21.     }

 下面是文件的读取实例:

  1. void Start ()
  2.     {
  3.        //读取文件
  4.         ArrayList info = LoadFile (Application.dataPath, "FileName");
  5.         foreach (string str in info) {//打印信息
  6.             Debug.Log (str);
  7.         }
  8.     }
  9.     
  10.     ArrayList LoadFile (string path, string name)
  11.     {
  12.         StreamReader sr = null;//文件流
  13.         try {
  14.             //通过路径和文件名读取文件
  15.             sr = File.OpenText (path + "//" + name);
  16.         } catch (Exception ex) {
  17.             return null;
  18.         }
  19.         string line;
  20.         ArrayList arrlist = new ArrayList ();
  21.         while ((line = sr.ReadLine ()) != null) {//读取每一行加入到ArrayList中
  22.             arrlist.Add (line);
  23.         }
  24.         sr.Close ();
  25.         sr.Dispose ();
  26.         return arrlist;
  27.     }

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D中,我们可以使用C#的文件读写方法来实现在安卓上读取文件的功能。 首先,我们需要将要读取文件放置在安卓设备的存储空间中。可以使用Android的API方法来实现文件的存储。例如,可以使用Unity的Application.persistentDataPath属性来获得设备存储空间的路径,然后使用System.IO命名空间下的File类来创建写入文件。 接下来,要在安卓设备上读取文件,我们需要使用System.IO命名空间下的File类和StreamReader类。首先,通过File.OpenText方法打开文件,该方法接受文件的完整路径作为参数。然后,使用StreamReader来读取文件内容。StreamReader的ReadLine方法可以按行读取文件内容。 读取文件内容后,记得要关闭StreamReader和File,释放资源。 以下是一个简单示例代码,展示了在Unity3D中如何读取安卓设备上的文本文件: ```c# using System.IO; using UnityEngine; public class AndroidFileReader : MonoBehaviour { void Start() { string filePath = Path.Combine(Application.persistentDataPath, "example.txt"); ReadFile(filePath); } private void ReadFile(string path) { if (File.Exists(path)) { using (StreamReader sr = File.OpenText(path)) { string line; while ((line = sr.ReadLine()) != null) { Debug.Log(line); } } } else { Debug.Log("File does not exist."); } } } ``` 上述代码将会从设备存储空间中读取名为"example.txt"的文件,并逐行输出文件内容到Unity控制台中。 当在Unity3D中开发安卓应用时,很重要的一点是要确保获取到的路径是设备存储空间的路径,即通过Application.persistentDataPath属性获得的路径。这样才能保证在不同的安卓设备上都能正常读取文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值