#Inspector Panel List with Different Object Types

1 messages · Page 1 of 1 (latest)

primal geode
#

I've been butting my head against a wall for the past couple of days trying to come up with a good solution for this issue.
I'm working on designing a system that will allow me to create my own in-engine cutscenes for a 2D game. I have a bunch of classes representing different events that can take place during a cutscene e.g. moving an object, changing a sprite. These events have different fields (moving a game object requires a game object to move, a speed, and a time. Changing a sprite requires a sprite renderer and sprite). I need to be able to display a list in the inspector that will let me add a new event of any available type so that the fields can be set. And then in the backend, a manager class will take this list and execute all of the event actions.

The system I designed uses an interface with an ExecuteEvent() function that the concrete event classes (such as EventMoveGameObject) implement, along with whatever fields they specifically require. Since Unity can't display interfaces in the inspector panel (and even if it could, it wouldn't be able to display the fields of the concrete class implementing the interface), I made a class called EventContainer which contains an enum with options for each event type and an EventContainer class which stored the event type enum of an event and the interface.

I thought maybe I could downcast the interface to its concrete class in a custom editor script, render the concrete fields in the inspector panel, and that way I'd be able to edit the fields while still keeping all of these different event classes in one list. This failed miserably hehehe. There's a bunch of issues with this implementation that mean it just won't work. It's obvious now but I didn't realise at the time that I couldn't cast the serialized interface back into a class.

This is a long-winded way of asking, what would be a better way to organise this data? Events that require different variables and execute different actions all displayed together in the inspector panel, ready to edit fields and create new instances.

Relevant classes and functions should all be here but feel free to ask for any additional code: https://paste.ofcode.org/ZrVbx2ivhFbt7GmCYshPza

plush field
#

Interfaces works more just as blueprints. I don't think that would be very useful unless you want each event to have the same variables. Using base and derived classes through polymorphism allows you to store different classes as the same base class.
If you want more flexibility, you could try mixing polymorphism and scriptable objects, so you can create your events in the inspector.

#

You could have a simple SO base class like this ```c#
using UnityEngine;

public abstract class CutsceneEvent : ScriptableObject
{
public abstract void ExecuteEvent();
}

#

With your events like

[CreateAssetMenu(menuName ="Cutscene Event/Event Object Movement")]
public class EventObjectMovement : CutsceneEvent
{
    public GameObject obj;
    public Vector3 destination;
    public float speed;
    //whichever variables you need.

    public override void ExecuteEvent()
    {
        Debug.Log("I just moved this object.");
    }
}```
```c#
[CreateAssetMenu(menuName = "Cutscene Event/Event Dialogue")]
public class EventDialogue : CutsceneEvent
{
    public string dialogue;
    public Sprite chatBubble;
    // etc..
    public override void ExecuteEvent()
    {
        Debug.Log("Stay a while and listen.");
    }
}```
#

And then finally, you could create a SO that stores your events in a list with a method to execute them

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Cutscene Event/Start Cutscene")]
public class PlayCutscene : ScriptableObject
{

    public List<CutsceneEvent> events;

    public void PlaySequence()
    {
        foreach (CutsceneEvent cutsceneEvent in events)
        {
            cutsceneEvent.ExecuteEvent();
        }
    }

}```
primal geode
#

With this system, would I need to make a different asset for every different event? For example, if I wanted two dialogue events, one with "Hello there" and one with "How's it going", would I need to make 2 different assets? If so, I don't think that would be the best way to set this up. I don't want to have to make a different asset every time I want to move a game object by a different amount

plush field
primal geode
#

it would moreso be an issue for the EventObjectMovement case. it's something that i'd need to use hundreds of times with varying destinations and speeds which would be quite a hassle if i had to make a new asset for each variation

plush field
#

If you want to do a lot of things in a cutscene, you’re essentially going to have to store that information somewhere. You can try to merge or cut off some of the crust to get it to look cleaner, for example, if you want an object to move and bounce around to several destinations in a single sequence, concider having List<Vector3> instead of a single variable. If you have special cases where you need to do something specific, you can create a new class that contains that logic.
Otherwise I don’t know of a simpler way of doing it. Afaik cutscenes are notorious for being tedious to program

primal geode
#

Gotcha. I appreciate the help nonetheless. Gonna spend some more time thinking about it.