unity读取Text


sing UnityEngine;
using System.Collections;
using System.IO; //需要导入System.IO,主要使用它的File类
public class TextTest : MonoBehaviour {

private string Mytxt; //用来存放文本内容

void Start()
{

Mytxt = ReadFile("C:\\Users\\Admin\\Desktop\\测试\\text.txt",4 ); //txt文件的绝对路径
print(Mytxt); //输出验证

}

//按路径读取txt文本的内容,第一个参数是路径名,第二个参数是第几行,返回值是sring[]数组
string ReadFile(string PathName, int linenumber)
{
string[] strs = File.ReadAllLines(PathName);//读取txt文本的内容,返回sring数组的元素是每行内容
if (linenumber == 0)
{
return "";
}
else
{
return strs[linenumber - 1]; //返回第linenumber行内容
}
}
}

 

2.读取txt中一行中的几个字符

test文档中有

密码:123

账号2:45666

    
string path = @"C:\Users\Administrator\Desktop\test.txt";
public void Read() { string[] s = File.ReadAllLines(path); string ss = s[0]; Debug.Log(ss.Substring(3, 3)); string s1 = s[1]; Debug.Log(s1.Substring(4, 5)); }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM