Home > OS >  Is there a way to use a class as an array? (Unity C#)
Is there a way to use a class as an array? (Unity C#)

Time:10-16

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?

CodePudding user response:

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 .

CodePudding user response:

Muhammad Usama Alam commented on my question linking me to another similar post. But in said post they already solved my problem and were trying to solve another. The link is right here: stackoverflow.com/a/55407835/4812203

My problem was that (suggested by hijinxbassist and Cool guy) I wasn´t using [System.Serializable]. But that didn't solve all my problems. Then I had to [Suggested by S.Hoseinpoor] changed the type from array to List using List<>.

And lastly, [extracted from Muhammad Usama Alam's comment] I needed to make an editor script to make the whole array of variables appear on the editor.}

To brake it down:

This is my script containing the dialogue line base:

[System.Serialible]   
public class DialogueLine
{
            [SerializeField] private string input;
            [SerializeField] private Color textColor;
            [SerializeField] private float delay;
}

Then my DialogueBase script:

public class DialogueBaseClass : MonoBehaviour
    {
        
        [SerializeField]
        public List<DialogueLine> DialogueSize = new List<DialogueLine>();
        (...)
    }

And lastly my editor script so that the editor renders everything as I want it to (extrected directly from the link, credit goes to Dubi Duboni)

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(DialogueBase))]
public class DialogueBase : Editor
{
    private SerializedProperty _dialogues;

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        _dialogues = serializedObject.FindProperty("dialogue");
        serializedObject.Update();

        for (int i = 0; i < _dialogues.arraySize; i  )
        {
            var dialogue = _dialogues.GetArrayElementAtIndex(i);
            EditorGUILayout.PropertyField(dialogue, new GUIContent("Dialogue "   i));
        }
    }
}

Hope anyone who finds this gets what they need. Again, credit for that last part goes to the real author, I do not claim to be the original writter of that code. You can read the whole thread containing the last part here: stackoverflow.com/a/55407835/4812203 (Suggested by Muhammad Usama Alam).

  • Related