我正在尝试使用位于我的资源文件夹中的文本文件的名称来实例化来自资源文件夹的预制。Prefab也位于我的资源文件夹中。然而,我得到一个错误提示“我要实例化的对象是空的”。
当我直接给出预制件的名字时,它就起作用了。我似乎不能理解这个问题。
我的代码是:
public void Loadd()
{
TextAsset txtasset = (TextAsset)Resources.Load (txtfile);
txtcontents = txtasset.text;
string[] linesinfile = txtasset.text.Split ('\n');
for(int i= 0; i<1; i++)
{
for (int x = 0; x < buttonholder.Length; x++)
{
//GameObject Instance = Instantiate (Resources.Load ("F260001", typeof(GameObject))) as GameObject; // works
string name = linesinfile [x];
Debug.Log (name);
GameObject Instance = Instantiate (Resources.Load (name, typeof(GameObject))) as GameObject;
}
}
}发布于 2017-02-10 14:36:36
我还在Unity answers论坛上寻求帮助,@corpsinheretoo提供了帮助。问题在于我如何拆分字符串。我使用的是'\n‘,但当按return时,某些文本编辑器添加了\r\n。所以我认为字符串看起来像这样: F26001\nF26002\nF26003实际上看起来像这样: F26001\r\nF26002\r\nF26003。
因此,我只是简单地将split切换为';‘,并像这样编辑我的代码和文本文件,它就起作用了。:http://i.imgur.com/MnlFvnd.png和文本文件:http://i.imgur.com/nsWtG9G.png ..
https://stackoverflow.com/questions/42112395
复制相似问题