using System; using System.Collections; using UnityEngine; public class StaticCoroutine : MonoBehaviour { public static void Start(IEnumerator routine) { GameObject gameObject = new GameObject(); gameObject.hideFlags = HideFlags.HideAndDontSave; UnityEngine.Object.DontDestroyOnLoad(gameObject); gameObject.AddComponent().Init(routine); } private void Init(IEnumerator routine) { base.StartCoroutine(this.Execute(routine)); } private IEnumerator Execute(IEnumerator routine) { try { while (routine.MoveNext()) { object obj = routine.Current; yield return obj; } } finally { UnityEngine.Object.Destroy(base.gameObject); } yield break; } }