EditorWindowからコルーチンを実行させるコードサンプル
namespace hoge | |
{ | |
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.Reflection; | |
public class TestCoroutineWindow : EditorWindow | |
{ | |
public TestMonobehaviour targetObject; | |
[MenuItem("EditorWindow/EditorWindowからコルーチンを実行させるサンプル")] | |
static void Open() | |
{ | |
GetWindow<TestCoroutineWindow>(); | |
} | |
void OnGUI() | |
{ | |
this.targetObject = (TestMonobehaviour) EditorGUILayout.ObjectField(this.targetObject, typeof(TestMonobehaviour), true); | |
if (GUILayout.Button("コルーチン実行")) | |
{ | |
this.ExecCoroutine(); | |
} | |
} | |
void ExecCoroutine() | |
{ | |
// メソッドを取り出す | |
var component = targetObject.GetComponent<TestMonobehaviour>(); | |
var flag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; | |
var method = typeof(TestMonobehaviour).GetMethod("TestCoroutine", flag); | |
// StartCoroutineを取り出す | |
var argumentTypes = new System.Type[] { typeof(IEnumerator) }; | |
var startCoroutine = typeof(TestMonobehaviour).GetMethod("StartCoroutine", argumentTypes); | |
// メソッドを実行してIEnumeratorを取得 | |
object[] parameters = new object[0]{}; | |
var coroutine = method.Invoke(component, parameters); | |
// コルーチン実行 | |
startCoroutine.Invoke(component, new object[] { coroutine }); | |
} | |
} | |
} |
namespace hoge | |
{ | |
using UnityEngine; | |
using System.Collections; | |
public class TestMonobehaviour : MonoBehaviour | |
{ | |
private IEnumerator TestCoroutine() | |
{ | |
Debug.Log("TestCoroutine Start"); | |
yield return new WaitForSeconds(1f); | |
Debug.Log("TestCoroutine Done"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment