using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.Reflection;
public class UnityReflectionHelper : EditorWindow
{
[MenuItem("Window/Reflection/Helper")]
static void Init ()
{
var w = GetWindow <UnityReflectionHelper> ();
w.Show ();
}
string _result = "";
string _className = "";
string _name = "";
string _assemblyName;
Vector2 _scrollPos = Vector2.zero;
string[] _ignoreNames = null;
/// <summary>
/// Private Field or Method
/// </summary>
bool _isPrivate = false;
void OnGUI ()
{
GUI.skin.textArea.richText = true;
EditorGUILayout.LabelField (_name);
_isPrivate = EditorGUILayout.ToggleLeft ("Private", _isPrivate);
GUILayout.BeginHorizontal ();
_assemblyName = EditorGUILayout.TextField (_assemblyName);
_className = EditorGUILayout.TextField (_className);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
if (GUILayout.Button ("Find Methods"))
FindMethods ();
if (GUILayout.Button ("Find Fields"))
FindFields ();
GUILayout.EndHorizontal ();
_scrollPos = GUILayout.BeginScrollView (_scrollPos);
GUILayout.TextArea (_result);
GUILayout.EndScrollView ();
}
void SetupIgnoreMethod ()
{
var t = Type.GetType ("UnityEngine.Object,UnityEngine");
_ignoreNames = t.GetMethods ().Select (x => x.Name).ToArray ();
}
void SetupIgnoreFields ()
{
var t = Type.GetType ("UnityEngine.Object,UnityEngine");
_ignoreNames = t.GetFields ().Select (x => x.Name).ToArray ();
}
void FindMethods ()
{
SetupIgnoreMethod ();
_result = "";
_name = _assemblyName + "." + _className;
var t = Type.GetType (_name + "," + _assemblyName);
if (t != null)
{
var methods = _isPrivate ? t.GetMethods (BindingFlags.NonPublic | BindingFlags.Instance) : t.GetMethods ();
foreach(var m in methods)
{
bool isStatic = m.IsStatic;
string pre = isStatic ? "[S] " : " ";
var element = pre + m.Name + "\n";
if (_result.Contains (element) == false && _ignoreNames.Any (x=>x == m.Name) == false){
_result += element;
}
}
}
else
{
Fail (_name);
}
}
void FindFields ()
{
SetupIgnoreFields ();
_result = "";
// フルパス,アセンブリ名
_name = _assemblyName + "." + _className;
var t = Type.GetType (_name + "," + _assemblyName);
if (t != null)
{
var fields = _isPrivate ? t.GetFields (BindingFlags.NonPublic | BindingFlags.Instance) : t.GetFields ();
foreach(var m in fields)
{
bool isStatic = m.IsStatic;
string pre = isStatic ? "[S] " : " ";
var element = pre + m.Name + "\n";
if (_result.Contains (element) == false && _ignoreNames.Any (x=>x == m.Name) == false){
_result += element;
}
}
}
else
{
Fail (_name);
}
}
string CreateMethodName (MethodInfo m)
{
if (m.IsPrivate) {
return string.Format ("<color=yellow>{0}</color>", m.Name);
}
return m.Name;
}
void Fail (string name)
{
ShowNotification (new GUIContent("\""+ name + "\"\nis Fail"));
}
}