Hi, I have this class that I made for using dialogs in a way that will fit my code.
I want each NPC to have an array of dialogs that implement IDialog.
The problem is I can’t add those items manually in the inspector.
I can do it by simply using dialog’s class without the abstraction or interface but maybe I am missing something.
using System;
using UnityEngine;
[Serializable]
public abstract class Dialog : IDialog
{
public int test;
[SerializeField, SerializeReference]
protected string NpcName;
[SerializeField]
protected string Sentence;
public abstract string[] GetContent();
public virtual string GetNpcName() => NpcName;
public virtual string GetSentence() => Sentence;
}
[Serializable]
public class DialogSentence : Dialog
{
public override string[] GetContent()
{
return null;
}
}
[Serializable]
public class DialogWithOptions : Dialog
{
[SerializeField]
public string[] Options;
public override string[] GetContent()
{
return Options;
}
}
public interface IDialog
{
string[] GetContent();
string GetNpcName();
string GetSentence();
}
Could you post where these are used? You may have to use SerializeReference on that field. And it will not be drag & droppable because it’s not a UnityEngine.Object type. You may want to make Dialog a ScriptableObject instead.
SerializeReference has no effect on a string. It also overrides SerializeField, so where you use it you’d use it without SerializeField.
A sentence “is not a” dialog. A dialog “has a” number of sentences.
DialogSentence should not inherit from Dialog, there is nothing meaningful to a sentence that it should inherit from a dialog. That’s also why you have to awkwardly return null from GetContent in DialogSentence because a sentence clearly is not a dialog.
Instead, Dialog ought to have a List or even simply List sentences.
You can use [SerializeReference] to serialise plain C# types via an interface or base class, but Unity does not have any built in inspector support for selecting sub types. You will need to do custom inspector work to make it possible, or use an addon that lets you work with SerializeReference properly.