首页 > 解决方案 > Is there a way to use a class as an array? (Unity C#)

问题描述

So i have a class with a bunch of variables:

class DialogueBase
{
        [SerializeField] private string input;
        [SerializeField] private Color textColor;
        [SerializeField] private float delay;
}

And I want to have another script with an array of this class so that I can add a bunch of dialogues and edit each one in the editor. So for example, I need to have 3 dialogues in a single dialogue, so I make the lenght of the array to 3 and edit each value in the editor.

Is it possible to do this? Or am I being dumb?

标签: c#unity3d

解决方案


As hijinxbassist said, you need to declare your class serializable.

[System.Serializable]
class DialogueBase
{
        [SerializeField] private string input;
        [SerializeField] private Color textColor;
        [SerializeField] private float delay;
}

Then

public DialoguBase[] dialoges;

For inspector editing

And one extra thing. You don't need to use private keyword to make your fields private. According to C# docs, a field is private by default. So private int A is literally the same as int A .


推荐阅读