Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文本文件中的每一行从Unity中的资源导入C中的列表#_C#_Unity3d - Fatal编程技术网

C# 将文本文件中的每一行从Unity中的资源导入C中的列表#

C# 将文本文件中的每一行从Unity中的资源导入C中的列表#,c#,unity3d,C#,Unity3d,我的资产/资源/文本中有一个文本文件,在Start()上,我想将文本文件中的每一行作为列表中的字符串导入。使用这个代码对我来说就行了 string[] AllWords = File.ReadAllLines(@"C:\Users\jamal\Downloads\English\english-words-master\New folder\AllWords.txt"); listOfWords = new List<string>(AllWord

我的资产/资源/文本中有一个文本文件,在
Start()
上,我想将文本文件中的每一行作为列表中的字符串导入。使用这个代码对我来说就行了

string[] AllWords = File.ReadAllLines(@"C:\Users\jamal\Downloads\English\english-words-master\New folder\AllWords.txt");
        listOfWords = new List<string>(AllWords);
首先

如果违反Unity自己的建议,出于某种原因,您仍然希望这样做,那么您必须在您的案例中以类似的方式加载该资源

// assuming your path is e.g. "Assets/Resources/Text/AllWords.txt"
var file = Resources.Load<TextAsset>("Text/AllWords");
var content = file.text;
var AllWords = content.Split("\n");
listOfWords = new List<string>(AllWords);
或者-取决于平台,例如在Android中,您不能直接访问
流化资产

StartCoroutine(LoadWords());

...

private IEnumerator GetRequest()
{
    var url = Path.Combine(Application.streamingAssetsPath, "AllWords.txt");
    using (var webRequest = UnityWebRequest.Get(url))
    {
        // Request and wait for result
        yield return webRequest.SendWebRequest();

        switch (webRequest.result)
        {    
            case UnityWebRequest.Result.Success:
                var content = webRequest.downloadHandler.text;
                var AllWords = content.Split("\n");
                listOfWords = new List<string>(AllWords);
                break;

            default:
                Debug.LogError($"Failed loading file \"{url}\" - {webRequest.error}", this);
                break;
         }
    }
}

然后通过

var content = file.text;
var AllWords = content.Split("\n");
listOfWords = new List<string>(AllWords);
var content=file.text;
var AllWords=content.Split(“\n”);
listOfWords=新列表(所有单词);
首先

如果违反Unity自己的建议,出于某种原因,您仍然希望这样做,那么您必须在您的案例中以类似的方式加载该资源

// assuming your path is e.g. "Assets/Resources/Text/AllWords.txt"
var file = Resources.Load<TextAsset>("Text/AllWords");
var content = file.text;
var AllWords = content.Split("\n");
listOfWords = new List<string>(AllWords);
或者-取决于平台,例如在Android中,您不能直接访问
流化资产

StartCoroutine(LoadWords());

...

private IEnumerator GetRequest()
{
    var url = Path.Combine(Application.streamingAssetsPath, "AllWords.txt");
    using (var webRequest = UnityWebRequest.Get(url))
    {
        // Request and wait for result
        yield return webRequest.SendWebRequest();

        switch (webRequest.result)
        {    
            case UnityWebRequest.Result.Success:
                var content = webRequest.downloadHandler.text;
                var AllWords = content.Split("\n");
                listOfWords = new List<string>(AllWords);
                break;

            default:
                Debug.LogError($"Failed loading file \"{url}\" - {webRequest.error}", this);
                break;
         }
    }
}

然后通过

var content = file.text;
var AllWords = content.Split("\n");
listOfWords = new List<string>(AllWords);
var content=file.text;
var AllWords=content.Split(“\n”);
listOfWords=新列表(所有单词);

但当文件位于我的资源文件夹中时,我似乎无法使用该文件。ReadAllLines
请更新帖子以包含您尝试过的内容。
但是当文件位于我的资源文件夹中时,我如何做同样的事情,我似乎无法使用文件。ReadAllLines
请更新帖子以包含您尝试过的内容。非常感谢!这就成功了。我甚至不知道你能从检查员那里查到任何文件。有了这个,我的基本游戏终于完成了:)现在只需要在另一个UI上工作。再次感谢,干杯@贾马尔。A很乐意帮忙!:)这就是StackOverflow对我来说的意义:帮助他人真正学习新知识,而不是仅仅解决问题;)非常感谢你!这就成功了。我甚至不知道你能从检查员那里查到任何文件。有了这个,我的基本游戏终于完成了:)现在只需要在另一个UI上工作。再次感谢,干杯@贾马尔。A很乐意帮忙!:)这就是StackOverflow对我来说的意义:帮助他人真正学习新知识,而不是仅仅解决问题;)
var content = file.text;
var AllWords = content.Split("\n");
listOfWords = new List<string>(AllWords);