- Home /
Custom editors, enums and arrays
Hi, I'm currently working on a custom event system for providing lots of different types of triggers.
Detailed explanation
As a basis, I've created a class that I'd like to be abstract (because it will be derived from later). However, making it abstract doesn't work as it won't be shown in the inspector anymore. This is not the problem, though. Here's the top of the class:
[System.Serializable]
public class TWMEvent {
public string id; // the id of this event
public TWMEventType type; // the type of event represented by this event
public delegate void EventListener(); // a delegate for any void that can be used as a listener
private List<EventListener> listeners; // the list of listeners attached to this event
public bool consumable; // whether this event should be able to be consumed
public bool consumed; // whether this event has been consumed and can't be triggered again
Note that it's just a Serializable and doesn't inherit from MonoBehaviour. This class will have subclasses in the future that should look something like this:
public class TimeEvent : TWMEvent {
public int executionTime;
public TimeEvent(string id, bool consumable, int executionTime) : base(id, TWMEventType.TimeEvent, consumable) {
this.executionTime = executionTime;
}
public override bool Check() {
return Time.time >= executionTime;
}
}
To manage these events, I've created an event manager that's fairly empty at the moment and looks like this:
public class EventManager : MonoBehaviour {
[SerializeField] private TWMEvent[] events;
// run once every frame
public void Update() {
foreach(TWMEvent e in events)
e.TryTrigger();
}
}
The content of this doesn't really matter except for the first array. I'd like to be able to add events of different types from the editor without the need to show all possible properties. This will be my fallback option though. This is what the editor looks like at the moment:
I'd now like to know how I can write a custom editor script that will allow me to only display the properties of the selected type (e.g. if I select TimeEvent I only want to see executionTime as a property and want to hide other things like executionPosition from PositionEvent and so on).
TL;DR
I've already tried a couple of things with custom editors but have always failed because my case just seems to be too specific. The one solution that brought me the closest was to have an editor of a script that is a MonoBehaviour that only has one object that changes its properties based on an enum. However, I've got a hierarchy of classes that are not MonoBehaviours. In addition to that, they're not used as single variables but are stored in an array instead.
It'd be awesome if anyone could help me out, thanks in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220612214226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Custom Editors : EnumFields, Proper Use Of 0 Answers
Grabbing an Inherited Variable 0 Answers
Problems with enums and switches (C#) 2 Answers