[英]How can I pull a string from a separate class while keeping the class in a list in my main (C#, Unity)
I feel like I should preface this by saying, I am still a beginner and knowing me, I could very well be forgetting something that is ridiculously simple and over complicating in the process.我觉得我应该先说,我仍然是一个初学者并且了解我,我很可能会忘记一些在这个过程中非常简单和过于复杂的东西。 But nonetheless, I have been unable to figure this out.
但尽管如此,我一直无法弄清楚这一点。
I'm trying to create a dialogue system in unity that displays a string of text as well as a sprite.我正在尝试创建一个统一的对话系统,显示一串文本和一个精灵。 However, I am not sure how to pull the string from a separate class while also ensuring that I can keep the class in a list so I can cycle through the text and sprite together.
但是,我不确定如何从单独的 class 中提取字符串,同时确保我可以将 class 保留在列表中,以便我可以一起循环浏览文本和精灵。 Currently the class I'm attempting to pull from looks like this:
目前我试图从中提取的 class 看起来像这样:
public class DialogueInfo
{
public Sprite emotion;
public string Words;
}
The list looks like this:该列表如下所示:
public List<DialogueInfo> dialogueInfo = new List<DialogueInfo>();
And I'm trying to use the "Words" string here:我正在尝试在这里使用“Words”字符串:
NPC.dialogLines = dialogueInfo.Words;
This however, does not work.然而,这不起作用。 I am getting an error saying that "'List<dialogueHolder.DialogueInfo>' does not contain a definition for 'Words' and no accessible extension method..." Any suggestions?
我收到一条错误消息,说“'List<dialogueHolder.DialogueInfo>' 不包含 'Words' 的定义并且没有可访问的扩展方法......”有什么建议吗? This whole process could be a very bad way of doing things and if so, suggestions would be appreciated.
整个过程可能是一种非常糟糕的做事方式,如果是这样,我们将不胜感激。 If more context is needed, I will gladly provide it.
如果需要更多上下文,我很乐意提供。
Note: by using a list i am just trying to keep a format that looks like this: Unity's formatting of a list/array but with both text and a sprite in a single element If I have to use a different approach then that is alright.注意:通过使用列表,我只是想保持如下所示的格式: Unity 的列表/数组格式,但在单个元素中同时包含文本和精灵如果我必须使用不同的方法,那没关系。
Your variable name dialogInfo
is misleading.您的变量名
dialogInfo
具有误导性。 It's not a dialogInfo
, it's a box (a list) where there is a lot of dialogInfo inside.它不是一个
dialogInfo
,它是一个盒子(一个列表),里面有很多 dialogInfo 。
Let's call it dialogInfoList
instead.让我们将其
dialogInfoList
。
public List<DialogueInfo> dialogueInfoList = new List<DialogueInfo>();
If you want to get all the "Words" properties of all the element, you can use linq for this:如果要获取所有元素的所有“单词”属性,可以为此使用 linq:
NPC.dialogLines = dialogueInfoList.Select(dialogInfo => dialogInfo.Words);
Depending on the type of NPC.dialogueLines
(List for instance), you may want to add .ToList()
after the Select()
.根据
NPC.dialogueLines
的类型(例如 List),您可能需要在Select()
.ToList()
) 。
You mentioned you might miss something simpler.你提到你可能会错过一些更简单的东西。
It really depends on what you want to achieve, the quesiton is not very clear about that, but what about keeping all the "DialogInfo" list with the NPC object instead?这真的取决于你想要实现什么,这个问题不是很清楚,但是用NPC object 保留所有“DialogInfo”列表怎么样?
Something like:就像是:
NPC.DialogueInfoList = dialogueInfoList;
With DialogueInfoList
being a property / field of type List<DialogueInfo>
DialogueInfoList
是List<DialogueInfo>
类型的属性/字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.