Unity3D开发之加载text文本

unity3D项目中很多需要配置文件,对于一些简单的配置,我们可以用text文件来保存我们的配置数据。下面是我的解析代码:

using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;

public class LoadText
{
    public static string textPath = Application.streamingAssetsPath + "/Parama.txt";
 
    public static string[] LoadText1()
    {
        byte[] dataBytes=new byte[100];
        string[] dataList=new string[19];
        try
        {
            FileStream file = new FileStream(textPath, FileMode.Open);
            file.Seek(0, SeekOrigin.Begin);
            file.Read(dataBytes, 0, 100);
            string s = Encoding.Default.GetString(dataBytes);
            dataList=s.Split(',');
            file.Close();
            return dataList;
        }
        catch (Exception)
        {
            throw;
        }
    }
}

c#的流形式用完一定要关闭。否则会报错。

还有一个做测试用的,我们把输出信息保存到文本里可以看到程序运行情况。上代码:

 public void Save(string information)
    {
        Debug.Log("Save");
        string path = Application.streamingAssetsPath + @"/TestText.txt";
        FileStream aFile;
        if (!File.Exists(path))
        {
            aFile = new FileStream(path, FileMode.OpenOrCreate);
        }
        else
        {
            aFile = new FileStream(path, FileMode.Append);
        }
        StreamWriter sw = new StreamWriter(aFile);
        sw.Write(information);
        sw.Close();
        sw.Dispose();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值