在streamingAssets文件夹或其他文件夹下新建txt文件,

代码部分
using System.Collections;
using System.Collections.Generic;
using System.IO; //文件处理命名空间
using UnityEngine;
public class Test : MonoBehaviour
{
public string s;
void Start()
{
string fileAddress = System.IO.Path.Combine(Application.streamingAssetsPath, "test.txt");
FileInfo fInfo0 = new FileInfo(fileAddress);//这里写文件的绝对路径+文件名+后缀(下面这种方式也可),,用fInfo0来接收这个文件
//FileInfo fInfo0 = new FileInfo("E:/w/Downloads/UnityProject/Json/Assets/StreamingAssets/test.txt");
if (fInfo0.Exists)//判断文件是否存在
{
StreamReader r = new StreamReader(fileAddress);
//StreamReader默认的是UTF8的不需要转格式了,因为有些中文字符的需要有些是要转的,下面是转成String代码
//byte[] data = new byte[1024];
// data = Encoding.UTF8.GetBytes(r.ReadToEnd());

这篇博客介绍了如何在Unity游戏引擎中使用C#代码从StreamingAssets文件夹读取txt文件。通过示例代码展示了利用System.IO命名空间的Path和FileINFO类检查文件是否存在并读取其内容的过程,适用于游戏开发中加载配置文件或文本资源的场景。
最低0.47元/天 解锁文章
913

被折叠的 条评论
为什么被折叠?



