Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active September 23, 2015 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/a0c400a0ed9d7901eaa1 to your computer and use it in GitHub Desktop.
Save tsubaki/a0c400a0ed9d7901eaa1 to your computer and use it in GitHub Desktop.
リフレクションテスト
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