Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Reflection;
- using System;
- public class ScriptTypeData
- {
- public string m_strScriptName;
- public string m_strFieldName;
- public System.Type m_cType;
- public System.Type m_cBaseType;
- public string m_strCallFunctionName;
- }
- public class FunctionData
- {
- public List<string> m_strNameList = new List<string>();
- public int m_nIndex = 0;
- public List<FunctionData> m_cMethodList = new List<FunctionData>();
- public string m_strSelectionClassName;
- public string m_strSelectionMethodName;
- public string m_strError;
- public bool m_bIsCompleted;
- }
- public class BehaviorTestRunView : EditorWindow
- {
- const string TOOL_NAME = "BehaviorTestRunView";
- const string MENU_PATH = "Tools/" + TOOL_NAME;
- Vector2 m_vScrollPosition = Vector2.zero;
- static List<ScriptTypeData> m_lTypeDataList = new List<ScriptTypeData>();
- BehaviourList m_cBehaviourList = new BehaviourList();
- List<FunctionData> m_lFuncPopupDataList = new List<FunctionData>();
- bool m_bIsTestRun = false;
- [MenuItem(MENU_PATH)]
- static public void Open()
- {
- EditorWindow cWindow = EditorWindow.GetWindow<BehaviorTestRunView>();
- cWindow.titleContent = new GUIContent(TOOL_NAME);
- cWindow.Show();
- cWindow.minSize = new Vector2(500.0f, 300.0f);
- }
- static void ClassParamCreate()
- {
- m_lTypeDataList.Clear();
- using (StreamWriter sw = new StreamWriter("Assets/Editor/BehaviourList.cs"))
- {
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("");
- sb.AppendLine("public class BehaviourList");
- sb.AppendLine("{");
- var cBehaviourList = FindObjectsOfType<MonoBehaviour>();
- foreach(var cBehaviour in cBehaviourList)
- {
- if (cBehaviour == null) continue;
- if (cBehaviour.GetType().Module.ScopeName != "Assembly-CSharp.dll") continue;
- if (m_lTypeDataList.Find(i => i.m_strScriptName == cBehaviour.GetType().FullName) != null) continue;
- ScriptTypeData cAddData = new ScriptTypeData();
- cAddData.m_strScriptName = cBehaviour.GetType().FullName;
- cAddData.m_strFieldName = "m_c" + cBehaviour.GetType().Name;
- m_lTypeDataList.Add(cAddData);
- sb.AppendLine(" " + cAddData.m_strScriptName + " " + cAddData.m_strFieldName + "= null;");
- }
- sb.AppendLine(" public void Load()");
- sb.AppendLine(" {");
- foreach (var script in m_lTypeDataList)
- {
- sb.AppendLine(" " + "BehaviorManagerView.SetClassType(ref " + script.m_strFieldName + ");");
- }
- sb.AppendLine(" }");
- sb.AppendLine(" public void Call(FunctionData cData,params object[] param)");
- sb.AppendLine(" {");
- foreach (var script in m_lTypeDataList)
- {
- sb.AppendLine(" " + "BehaviorManagerView.Call(" + script.m_strFieldName + ", cData, param" + ");");
- }
- sb.AppendLine(" }");
- sb.AppendLine("}");
- sw.Write(sb);
- }
- AssetDatabase.Refresh();
- }
- void Update ()
- {
- Repaint();
- }
- void OnGetClassProcess()
- {
- m_lFuncPopupDataList.Clear();
- ClassParamCreate();
- m_cBehaviourList.Load();
- }
- int num = 0;
- void OnGUI()
- {
- if (GUILayout.Button("① クラスを取得する"))
- {
- OnGetClassProcess();
- }
- GUILayout.Space(10.0f);
- EditorGUILayout.LabelField("テスト関数");
- for (int i = 0; i < m_lFuncPopupDataList.Count; i++)
- {
- FunctionData cData = m_lFuncPopupDataList[i];
- if (cData == null) continue;
- EditorGUILayout.BeginHorizontal();
- {
- cData.m_nIndex = EditorGUILayout.Popup(cData.m_nIndex,
- cData.m_strNameList.ToArray());
- FunctionData cMethodData = cData.m_cMethodList[cData.m_nIndex];
- if (cMethodData == null) continue;
- cMethodData.m_nIndex = EditorGUILayout.Popup(cMethodData.m_nIndex,
- cMethodData.m_strNameList.ToArray());
- EditorGUILayout.LabelField("ParameterSize : ", GUILayout.Width(100));
- num = EditorGUILayout.IntField(num, GUILayout.Width(50));
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- {
- for (int paramIndex = 0; paramIndex < num; paramIndex++)
- {
- EditorGUILayout.TextArea("");
- }
- }
- EditorGUILayout.EndHorizontal();
- }
- if (GUILayout.Button("② 追加"))
- {
- if (m_lTypeDataList.Count == 0)
- {
- OnGetClassProcess();
- Debug.LogWarning("クラスリストが空だったので、取得しました。");
- }
- FunctionData cAddData = new FunctionData();
- foreach (var data in m_lTypeDataList)
- {
- cAddData.m_strNameList.Add(data.m_strScriptName);
- FunctionData cMethodAddData = new FunctionData();
- foreach (var method in data.m_cType.GetMembers(
- BindingFlags.Public | BindingFlags.NonPublic |
- BindingFlags.Instance | BindingFlags.Static |
- BindingFlags.DeclaredOnly))
- {
- // 除外するタイプ
- if (method.GetType().Name == "MonoCMethod") continue;
- if (method.GetType().Name == "MonoField") continue;
- if (method.GetType().Name == "MonoType") continue;
- cMethodAddData.m_strNameList.Add(method.Name);
- }
- cAddData.m_cMethodList.Add(cMethodAddData);
- }
- m_lFuncPopupDataList.Add(cAddData);
- m_bIsTestRun = false;
- }
- GUILayout.Space(10.0f);
- if (GUILayout.Button("③ 実行テストをする"))
- {
- for (int i = 0; i < m_lFuncPopupDataList.Count; i++)
- {
- FunctionData cClassData = m_lFuncPopupDataList[i];
- FunctionData cMethodData = cClassData.m_cMethodList[cClassData.m_nIndex];
- cClassData.m_strSelectionClassName = cClassData.m_strNameList[cClassData.m_nIndex];
- cClassData.m_strSelectionMethodName = cMethodData.m_strNameList[cMethodData.m_nIndex];
- m_cBehaviourList.Call(cClassData, null);
- }
- m_bIsTestRun = true;
- }
- GUILayout.Space(50.0f);
- EditorGUILayout.LabelField("実行結果");
- GUILayout.Box("", GUILayout.Height(1), GUILayout.MinWidth(10000));
- m_vScrollPosition = EditorGUILayout.BeginScrollView(m_vScrollPosition,GUI.skin.box);
- {
- if (m_bIsTestRun)
- {
- for (int i = 0; i < m_lFuncPopupDataList.Count; i++)
- {
- FunctionData cData = m_lFuncPopupDataList[i];
- GUIStyle style = new GUIStyle();
- style.normal.textColor = cData.m_bIsCompleted ? Color.green : Color.red;
- string strLabel = cData.m_strSelectionClassName + " : " + cData.m_strSelectionMethodName;
- strLabel += cData.m_bIsCompleted ? " <= 成功" : " <= 失敗 ( " + cData.m_strError + " )";
- EditorGUILayout.LabelField(strLabel, style);
- }
- }
- }
- EditorGUILayout.EndScrollView();
- }
- public static void Call<T>(T cBehaviour, FunctionData cData, params object[] param) where T : MonoBehaviour
- {
- if (cData.m_strSelectionClassName != cBehaviour.GetType().Name) return;
- MethodInfo mi = cBehaviour.GetType().GetMethod(cData.m_strSelectionMethodName,
- BindingFlags.Public | BindingFlags.NonPublic |
- BindingFlags.Instance | BindingFlags.Static |
- BindingFlags.DeclaredOnly);
- if (mi == null) return;
- var findItem = m_lTypeDataList.Find(i => i.m_strScriptName == cBehaviour.GetType().Name);
- if (findItem == null)
- {
- Debug.LogError(cBehaviour.GetType().Name + " がリストに登録してありません。");
- return;
- }
- findItem.m_strCallFunctionName = mi.Name;
- try
- {
- mi.Invoke(cBehaviour, param);
- cData.m_bIsCompleted = true;
- return;
- }
- catch (InvalidOperationException)
- {
- cData.m_bIsCompleted = false;
- cData.m_strError = "無効なメソッドが呼ばれました。";
- }
- catch (System.Reflection.TargetParameterCountException)
- {
- cData.m_bIsCompleted = false;
- cData.m_strError = "呼び出し時に指定された引数の数があっていません。";
- }
- catch (System.Reflection.TargetInvocationException)
- {
- cData.m_bIsCompleted = false;
- cData.m_strError = "リフレクションによって呼ばれたメソッドでErrorがありました。";
- }
- catch (MissingMethodException)
- {
- cData.m_bIsCompleted = false;
- cData.m_strError = "存在しないメソッドにアクセスしようとしました。";
- }
- catch (System.Reflection.TargetException)
- {
- cData.m_bIsCompleted = false;
- cData.m_strError = "無効なターゲットが呼ばれました。";
- }
- catch (ArgumentException)
- {
- cData.m_bIsCompleted = false;
- cData.m_strError = "無効な引数が存在します。";
- }
- }
- public static void SetClassType<T>(ref T cBehaviour) where T : MonoBehaviour
- {
- var findBehaviourType = FindObjectOfType<T>();
- if (findBehaviourType == null) return;
- var finItem = m_lTypeDataList.Find(i => i.m_strScriptName == findBehaviourType.GetType().FullName);
- if (finItem == null) return;
- finItem.m_cType = findBehaviourType.GetType();
- if (finItem.m_cType.BaseType != null)
- {
- finItem.m_cBaseType = finItem.m_cType.BaseType;
- }
- cBehaviour = findBehaviourType;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement