简体   繁体   English

如何从单独的 class 中提取字符串,同时将 class 保留在我的主列表中(C#,Unity)

[英]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>();

Extract all the words提取所有单词

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() ) 。

Simpler: just link all the original list更简单:只需链接所有原始列表

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> DialogueInfoListList<DialogueInfo>类型的属性/字段

暂无
暂无

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

相关问题 我如何访问从单独的类到主类的getter和setter(在C#中向下转换) - How do I get access to getters and setters from a separate class to the main class(with downcasting in C#) 如何使用来自我的 Class 的数据构建 C# 列表 - How can I build a C# list using data from my Class UNITY C# - 如何获取标有自定义属性(字段)的 class 实例的所有字段? - UNITY C# - How can I get all the fields of the an instance of a class marked with my custom atribute(the fields)? C#:如何将类传递给单独dll中的函数 - C#: How can I pass a class to a function in separate dll 在与C#中的类不同的文件中包含Main()例程 - Having a Main() routine in a separate file from a Class in C# 如何在另一个 class 方法中使用 class 和构造函数并将它们存储到我的列表中? C# - How can I use a class and constructor in a another class, method and store them to my list? C# 我如何将一个整数的值从一个类返回到我的main? C# - How do i return the value of a integer from a class to my main? C# C#试图从一个类的问题列表进入我的Main()类循环 - C# Trying to get a list of questions from one class into my Main() class loop UNITY,我如何从另一个 C# class 获取所有属性并将它们放入枚举 - UNITY, How can I get all properties from another C# class and put them into an enum 如何在主类C#中将值从一个子类传递到另一个子类? - How can I pass value from one subclass to another subclass in a main class C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM