What problem I'm trying to solve: I need the ability to incrementally fire off i number of methods, one method per trigger in the specified order.
These methods have to be public unity events so that they can be used to do different things.
The "I don't know what I'm doing" version of this is below - like it works, mostly, but it's horrible. Besides the horrible structure, it's not dynamic.
public class IncrementEventHandler : MonoBehaviour
{
int eventIncrement = 0;
public UnityEvent onTriggered1;
public UnityEvent onTriggered2;
public UnityEvent onTriggered3;
public UnityEvent onTriggered4;
public UnityEvent onTriggered5;
public UnityEvent onTriggered6;
public UnityEvent onTriggered7;
public UnityEvent onTriggered8;
public UnityEvent onTriggered9;
public UnityEvent onTriggered10;
public void triggerEvents()
{
eventIncrement++;
if(eventIncrement == 1)
{
onTriggered1.Invoke();
}
if(eventIncrement == 2)
{
onTriggered2.Invoke();
}
if(eventIncrement == 3)
{
onTriggered3.Invoke();
}
if(eventIncrement == 4)
{
onTriggered4.Invoke();
}
if(eventIncrement == 5)
{
onTriggered5.Invoke();
}
if(eventIncrement == 6)
{
onTriggered6.Invoke();
}
if(eventIncrement == 7)
{
onTriggered7.Invoke();
}
if(eventIncrement == 8)
{
onTriggered8.Invoke();
}
if(eventIncrement == 9)
{
onTriggered9.Invoke();
}
if(eventIncrement == 10)
{
onTriggered10.Invoke();
}
}
}
Attached image https://i.sstatic.net/RbttT.png is what it looks like in the inspector. And that's what I want to create but actually make it DYNAMIC. Have a public int i that I populate in the inspector, that then declares the i number of public UnityEvents named "onTriggered"+"i" that automatically appear in the inspector that then can be used to do whatever is needed. Then whatever else in the game calls triggerEvents() and that can be called up to i times to do the whole list of events.
If these were not methods then I'd have a basic grasp of how to serialize all of this for the inspector, but methods are static so no serialization. I tried to learn about delegates to fix the static issue, but still have absolutely no understanding of how to apply them to my specific use case. I have a basic grasp that they are used to subscribe classes to methods they don't inherently have but that doesn't seem to solve anything for me in terms of making a dynamic list of methods in the inspector. So I don't know if that's the correct thread to follow.
The other hurdle is I don't know how to dynamically name these methods for declaration.
Basically I need to declare the methods in a for loop and then execute them incrementally.
Appreciate any help.
Have you tried making a list of unityEvents ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class IncrementEventHandler : MonoBehaviour
{
public List<UnityEvent> unityEvents = new List<UnityEvent>();
public int eventIncrement = 0;
public void triggerEvents()
{
eventIncrement++;
for (int i = eventIncrement ; i < unityEvents.Count; i++)
{
unityEvents[i]?.Invoke();
}
}