- Home /
Custom editor for inherited class but is not MonoBehaviour or ScriptableObject
Hi all, to begin, here's my code:
[Serializable]
public abstract class QuestTask
{
public string Description;
}
public enum QuestGoalType
{
Kill,
Capture
}
[Serializable]
public class KillQuestTask : QuestTask
{
public string Target;
public int Amount;
}
[Serializable]
public class CaptureQuestTask : QuestTask
{
public Item captureItem;
public int Amount;
}
[Serializable]
public class QuestGoal
{
public QuestGoalType questGoalType;
public QuestTask questTask;
}
[Serializable]
public class Quest
{
public string title;
public string description;
// .etc
public List<QuestGoal> questGoalls = new List<QuestGoal>();
}
public class QuestGiver : MonoBehaviour
{
public Quest quests;
}
How I should create an editor of QuestTask, in which I can change dynamically type of QuestGoalType and it will load the content of the inherited class. I mean, when I change the dropdown of enum on for example KillQuestTask, I should see all properties of KillQuestTask.
I want these classes to be serialized.
Or maybe there is some other way to achieve this effect? Easier way ...?
Thanks in advance to everyone guys.
Answer by ErikAndroid · Nov 28, 2020 at 08:28 AM
Have you tried using Scriptable Objects?
It will always be serialized and it gives us a more flexible way doing things.
Here is the changes (note that it is a demo):
using System;
using System.Collections.Generic;
using UnityEngine;
public class QuestTask : ScriptableObject
{
public string Description;
}
public enum QuestGoalType
{
Kill,
Capture
}
[CreateAssetMenu]
public class KillQuestTask : QuestTask
{
public string Target;
public int Amount;
}
[CreateAssetMenu]
public class CaptureQuestTask : QuestTask
{
public string captureItem;
public int Amount;
}
[Serializable]
public class QuestGoal
{
public QuestGoalType questGoalType;
public QuestTask questTask;
}
[CreateAssetMenu]
public class Quest : ScriptableObject
{
public string title;
public string description;
// .etc
public List<QuestGoal> questGoals = new List<QuestGoal>();
}
public class QuestGiver : MonoBehaviour
{
public Quest quest;
}
Yes, I tried Scriptable Objects, but I don't want to create every time separate new Quest Task. I would like to create all information dynamically in the Inspector.
You can try creating a CustomPropertyDrawer for the base class. Its worth trying.
@ErikAndroid, it is a good idea and I also tried this, but I think about an easier way. CustomPropertyDrawer is complicated. If I don't find any better solution, I will try again CustomPropertyDrawer. ;)