Skip to content

Instantly share code, notes, and snippets.

@tsubaki

tsubaki/GetTypeTest.cs

Last active Sep 23, 2015
Embed
What would you like to do?
リフレクションテスト
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.Assertions;
using System.Reflection;
public class GetTypeTest {
[MenuItem("Test/Test")]
static void Test () {
// SceneViewタイプを取得しインスタンス化
System.Type sceneViewType = Types.GetType ("UnityEditor.SceneView", "UnityEditor.dll");
Assert.IsNotNull (sceneViewType, "not found service type");
System.Object window = ScriptableObject.CreateInstance (sceneViewType);
// Showメソッドを取得し呼び出す。オーバーロードしているので、一覧から一致するものを選択
MethodInfo shoeMethod = null;
MethodInfo[] sceneViewMethods = sceneViewType.GetMethods (BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var method in sceneViewMethods) {
if( method.Name == "Show" && method.GetParameters().Length == 0 ){
shoeMethod = method;
break;
}
}
Assert.IsNotNull (shoeMethod, "not found Show method");
shoeMethod.Invoke (window, null);
// Cameraオブジェクトを取得する
FieldInfo cameraField = sceneViewType.GetField ("m_Camera", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.IsNotNull (cameraField, "not found camera field");
System.Object camera = cameraField.GetValue (window);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment