I would like to be able to execute a list of coroutines at specific times and allow this list to be serialized.
To implement this I thought that I would create a base “Script” class:
public abstract class Script
{
IEnumerator action;
float time;
public Script(ScriptSettings settings)
{
time = settings.Time;
}
public void Execute()
{
float elapsedTime = time - Time.time;
// Check to see if the script has expired
if (elapsedTime < 0)
{
return;
}
OnExecute(elapsedTime);
}
protected abstract void OnExecute(float elapsedTime);
}
The derived classes would then have the specific coroutine or “action” which would be called from Execute:
public class Fire : Script
{
IEnumerator action;
float time;
public Fire(ScriptSettings settings)
: base(settings)
{
}
protected override void OnExecute(float elapsedTime)
{
StartCoroutine(Firing(elapsedTime)); // Can't use this function outside of a MonoBehaviour derived class
}
IEnumerator Firing(float startTime)
{
yield return new WaitForSeconds(startTime);
// Fire
Debug.Log("Fire " + Time.time);
}
}
This method fails to work as I can’t derive from both MonoBehaviour and Script.
What methods do you guys use for executing a list of coroutines at specific times?
It would normally seem straightforward but I will need to serialize each type of coroutine and the time at which it will execute. The time part is easy but I thought the coroutine will require a specific class to which it is associated?