Last active
December 6, 2016 09:21
-
-
Save rngtm/3329ecdf550da153046603ce535c5fd4 to your computer and use it in GitHub Desktop.
EditorWindowからコルーチンを実行させるコードサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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