简体   繁体   English

TextAsset返回null

[英]TextAsset returning null

I have a script where I want to convert a text asset into a string array and then use that to modify PlayerPrefs. 我有一个脚本,希望将文本资产转换为字符串数组,然后使用该脚本来修改PlayerPrefs。 However, I keep getting a NullReferenceException Error during runtime on the second line in the code I have pasted below. 但是,在运行时,我在下面粘贴的代码的第二行中始终收到NullReferenceException错误。 I assume it's because I'm converting to the text asset into a string the wrong way. 我认为这是因为我以错误的方式将文本资产转换为字符串。 Could someone tell me what I'm doing wrong please? 有人可以告诉我我在做什么错吗? Thanks 谢谢

            TextAsset txt = (TextAsset)Resources.Load("SaveKeys1", typeof(TextAsset));

        string txtString = txt.text;



        char sep = (',');
        string[] keys = txtString.Split(sep);

        foreach(string key in keys)
        {
            PlayerPrefs.DeleteKey(key);
            Debug.Log(key);
        }

I assume it's because I'm converting to the text asset into a string the wrong way. 我认为这是因为我以错误的方式将文本资产转换为字符串。 Could someone tell me what I'm doing wrong please? 有人可以告诉我我在做什么错吗?

The code: 编码:

TextAsset txt = (TextAsset)Resources.Load("SaveKeys1", typeof(TextAsset));
string txtString = txt.text;

is the correct way to do this. 是执行此操作的正确方法。 There is nothing wrong there and since you mentioned that the sceond line which is string txtString = txt.text; 那里没有什么错,因为您提到过第二行是string txtString = txt.text; is causing an exception, there are two possible reasons for this. 导致异常,这可能有两个原因。

1 .Your file name is not spelt correctly. 1。你的文件名的拼写不正确。 Please copy the file name directly from where it is located and paste the name directly into the Resources.Load function. 请直接从文件所在的位置复制该文件名,然后将其直接粘贴到Resources.Load函数中。 Make sure not to include the extension( .txt , .xml ...) in the function. 确保函数中包括扩展名( .txt.xml ...)。

2 .The SaveKeys1 file does not have the right extension. 2 .The SaveKeys1文件没有正确的扩展名。 TextAsset only supports file extensions like .txt , .html , .htm , .xml , .bytes , .json , .csv , .yaml , and .fnt TextAsset只支持文件扩展名状.txt.html.htm.xml.bytes.json.csv.yaml.fnt

3 . 3 SaveKeys1 file is not placed in the right folder. SaveKeys1文件未放置在正确的文件夹中。 For the Resources.Load function to work, the file must be placed in a folder called Resources . 为了使Resources.Load函数起作用,必须将文件放置在名为Resources的文件夹中。 Usually, placing this file in any folder called Resources should work but just for testing purposes, create the folder in the Assets/Resources directory then put your SaveKeys1 file there. 通常,将此文件放置在名为Resources任何文件夹中应该可以工作,但仅出于测试目的,请在Assets/Resources目录中创建该文件夹,然后将SaveKeys1文件放在此处。 This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM