StreamingAssets文件夹的读取异常

 1、今天在读取StreamingAssets文件夹中的文本文件的时候,出现了异常,花了一个多小时解决了,把解决结果给大家梳理一下

 

2、文本文件夹所在位置:在StreamingAssets文件夹中新建一个文件名为AI_01的文本文件

 

3、文本文件中的内容,测试使用:

[Idle]
NoHealth>Dead
SawPlayer>Pursuit

[Dead]

[Pursuit]
NoHealth>Dead
ReachPlayer>Attacking
LosePlayer>Default

 

4、读取文本文件的代码:

复制代码
 1 public static String Load(string aiConfigFile)  2         {  3             //1、构造 配置文件的完整路径  4             aiConfigFile = Path.Combine(Application.streamingAssetsPath, aiConfigFile);  5   6             WWW www = new WWW(aiConfigFile);  7             //2、  8             while (true)  9             { 10                 if (!string.IsNullOrEmpty(www.error)) 11                 { 12                     throw new Exception("AI配置文件读取异常"); 13                 } 14                 if (www.isDone) 15                 { 16                     return www.text; 17                 } 18  19             } 20         }
复制代码

 

 

5、然后在其他的类中进行方法的调用来读取文件,获得其中的字符串文本

复制代码
 [HideInInspector]
 //文件在StreamingAssets中的位置
public string aiConfigFile = "AI_01.txt";


//测试方法,来读取文件
private void Test()
{
  string temp= AIConfigurationReader.Load(aiConfigFile);
}
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信