我想通过以下代码访问资源文件夹中的.txt
文件
void Start () {
TextAsset txtAsset = (TextAsset)Resources.Load("Dialogue0", typeof(TextAsset));
string dialogue = txtAsset.text;
lines = new List<DialogueLine>();
LoadDialogue(dialogue);
}
void LoadDialogue(string filename)
{
string line;
StreamReader r = new StreamReader(filename);
using (r)
{
do
{
line = r.ReadLine();
if (line != null)
{
string[] lineData = line.Split('|');
if (lineData[0] == "Player")
{
DialogueLine lineEntry = new DialogueLine(lineData[0], "", 0,0, "");
lineEntry.options = new string[lineData.Length - 1];
for (int i = 1; i < lineData.Length; i++)
{
lineEntry.options[i - 1] = lineData[i];
}
lines.Add(lineEntry);
}
else
{
DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), int.Parse(lineData[3]), lineData[4]);
lines.Add(lineEntry);
}
}
}
while (line != null);
r.Close();
}
}
但总是会出现错误:
ArgumentException: Empty path not allowed
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding,
Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
(at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:159)
System.IO.StreamReader..ctor (System.String path)
(wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
DialogueParser.LoadDialogue (System.String filename) (at Assets/Script/DialogueParser.cs:54)
DialogueParser.Start () (at Assets/Script/DialogueParser.cs:44)
The .txt file is really exist, named "Dialogue0.txt"
我已经搜索了一段时间,但没有找到解决问题的方法。
答案 0 :(得分:0)
“ Dialogue0.txt”的内容将用作文件名。因此,请检查Dialogue0.txt的内容。打印“对话”,发现会发生什么。也许最后有一个空行。
更新:
这也许是一个统一的问题。您可以阅读TextAsset.text is "" although the file does have content以获得更多详细信息。
对于其他情况,您可以尝试此操作或更改统一版本。
string content = lang.text;
if(content == "")
content = System.Text.Encoding.Default.GetString(lang.bytes);
答案 1 :(得分:0)
我认为您应该使用扩展名“ Dialogue0.txt”,而不只是“ Dialogue0”:
TextAsset txtAsset = (TextAsset)Resources.Load("Dialogue0.txt", typeof(TextAsset));
答案 2 :(得分:0)
您始终可以打印出所有资源以从API级别签出。
var allResources = Resources.LoadAll(“ [PathRelativeToResourcesDir]”)