//
// AppLovinEditorCoroutine.cs
// AppLovin MAX Unity Plugin
//
// Created by Santosh Bagadi on 7/25/19.
// Copyright © 2019 AppLovin. All rights reserved.
//
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
namespace AppLovinMax.Scripts.IntegrationManager.Editor
{
///
/// A coroutine that can update based on editor application update.
///
public class AppLovinEditorCoroutine
{
///
/// Keeps track of the coroutine currently running.
///
private IEnumerator enumerator;
///
/// Keeps track of coroutines that have yielded to the current enumerator.
///
private readonly List history = new List();
private AppLovinEditorCoroutine(IEnumerator enumerator)
{
this.enumerator = enumerator;
}
///
/// Creates and starts a coroutine.
///
/// The coroutine to be started
/// The coroutine that has been started.
public static AppLovinEditorCoroutine StartCoroutine(IEnumerator enumerator)
{
var coroutine = new AppLovinEditorCoroutine(enumerator);
coroutine.Start();
return coroutine;
}
private void Start()
{
EditorApplication.update += OnEditorUpdate;
}
///
/// Stops the coroutine.
///
public void Stop()
{
if (EditorApplication.update == null) return;
EditorApplication.update -= OnEditorUpdate;
}
private void OnEditorUpdate()
{
if (enumerator.MoveNext())
{
// If there is a coroutine to yield for inside the coroutine, add the initial one to history and continue the second one
if (enumerator.Current is IEnumerator)
{
history.Add(enumerator);
enumerator = (IEnumerator) enumerator.Current;
}
}
else
{
// Current coroutine has ended, check if we have more coroutines in history to be run.
if (history.Count == 0)
{
// No more coroutines to run, stop updating.
Stop();
}
// Step out and finish the code in the coroutine that yielded to it
else
{
var index = history.Count - 1;
enumerator = history[index];
history.RemoveAt(index);
}
}
}
}
}