[Unity][Android][LUA][IOS]读取写入txt文件
发布日期:2021-05-09 11:52:45 浏览次数:20 分类:精选文章

本文共 2074 字,大约阅读时间需要 6 分钟。

接着上一篇继续

创建 txt 文件,来保存文件内容。读取和写入。

从test文件 中读取内容,并创建 新的 文件test1.txt,替换 test1.txt 的某些内容。

显示结果

test1文件(test1.txt文件内容如果 不清空,就会 在原有文件中继续 写入 内容,根据情况,自行判断。)

using System.Collections;using System.Collections.Generic;using UnityEngine;using SLua;using System.IO;public class Test : MonoBehaviour {    public TextAsset TxtFile;   //建立TextAsset    private string Mytxt;       //用来存放文本内容        LuaSvr l;    private string m_sFileName = "test1.txt"; // 文件名    // Use this for initialization    void Start()    {        l = new LuaSvr();        l.init(null, () =>        {            l.start("test");        });        string path = "";        if (Application.platform == RuntimePlatform.Android)        {            path = Application.persistentDataPath;        }        else if (Application.platform == RuntimePlatform.WindowsPlayer)        {            path = Application.dataPath;        }        else if (Application.platform == RuntimePlatform.WindowsEditor)        {            path = Application.dataPath;        }        Mytxt = TxtFile.text;        print(Mytxt);           //输出验证        //找到文件 替换 某些内容        string text = Mytxt.Replace("Hello","Well Done!123");        CreateOrOPenFile(path, m_sFileName, text);    }    void CreateOrOPenFile(string path, string name, string info)    {          //路径、文件名、写入内容        FileInfo fi = new FileInfo(path + "//" + name);        StreamWriter sw ;       // StreamWriter sw = new StreamWriter(path+ name, false);        //File.Create(path + name);        //sw.WriteLine("");        print("      create");        //删除文件,重写文件的内容        fi.Delete();        if (!fi.Exists)        {            //File.Create(path+name);            //创建文件            sw = fi.CreateText();        }        else        {            sw = fi.AppendText();        }        //在 文件原有内容上 ,写入文件        sw.WriteLine(info);        sw.Close();        sw.Dispose();            }}

//在这里 把 路径 加上 /Slua/Resources/就效果如下所示
        //找到文件 替换 某些内容
        string text = Mytxt.Replace("Hello","Well Done!123");
        CreateOrOPenFile(path+"/Slua/Resources/", m_sFileName, text);

参考资料:

1.

2.

3.

4.

5.

6.

上一篇:[Unity][UGUI]UGUI的饼状统计图
下一篇:[Unity][LUA][SLUA]选择LUA库导入Unity中

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年05月07日 23时23分46秒