Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. using System.Reflection;
  8. using System;
  9.  
  10. public class ScriptTypeData
  11. {
  12. public string m_strScriptName;
  13. public string m_strFieldName;
  14.  
  15.  
  16. public System.Type m_cType;
  17. public System.Type m_cBaseType;
  18. public string m_strCallFunctionName;
  19.  
  20. }
  21.  
  22. public class FunctionData
  23. {
  24. public List<string> m_strNameList = new List<string>();
  25. public int m_nIndex = 0;
  26. public List<FunctionData> m_cMethodList = new List<FunctionData>();
  27.  
  28. public string m_strSelectionClassName;
  29. public string m_strSelectionMethodName;
  30.  
  31. public string m_strError;
  32. public bool m_bIsCompleted;
  33. }
  34.  
  35. public class BehaviorTestRunView : EditorWindow
  36. {
  37.  
  38. const string TOOL_NAME = "BehaviorTestRunView";
  39. const string MENU_PATH = "Tools/" + TOOL_NAME;
  40.  
  41. Vector2 m_vScrollPosition = Vector2.zero;
  42.  
  43. static List<ScriptTypeData> m_lTypeDataList = new List<ScriptTypeData>();
  44. BehaviourList m_cBehaviourList = new BehaviourList();
  45.  
  46. List<FunctionData> m_lFuncPopupDataList = new List<FunctionData>();
  47.  
  48. bool m_bIsTestRun = false;
  49.  
  50. [MenuItem(MENU_PATH)]
  51. static public void Open()
  52. {
  53. EditorWindow cWindow = EditorWindow.GetWindow<BehaviorTestRunView>();
  54. cWindow.titleContent = new GUIContent(TOOL_NAME);
  55. cWindow.Show();
  56. cWindow.minSize = new Vector2(500.0f, 300.0f);
  57. }
  58.  
  59. static void ClassParamCreate()
  60. {
  61. m_lTypeDataList.Clear();
  62.  
  63. using (StreamWriter sw = new StreamWriter("Assets/Editor/BehaviourList.cs"))
  64. {
  65. StringBuilder sb = new StringBuilder();
  66.  
  67. sb.AppendLine("");
  68.  
  69. sb.AppendLine("public class BehaviourList");
  70. sb.AppendLine("{");
  71.  
  72. var cBehaviourList = FindObjectsOfType<MonoBehaviour>();
  73.  
  74. foreach(var cBehaviour in cBehaviourList)
  75. {
  76. if (cBehaviour == null) continue;
  77. if (cBehaviour.GetType().Module.ScopeName != "Assembly-CSharp.dll") continue;
  78.  
  79. if (m_lTypeDataList.Find(i => i.m_strScriptName == cBehaviour.GetType().FullName) != null) continue;
  80.  
  81. ScriptTypeData cAddData = new ScriptTypeData();
  82. cAddData.m_strScriptName = cBehaviour.GetType().FullName;
  83. cAddData.m_strFieldName = "m_c" + cBehaviour.GetType().Name;
  84.  
  85. m_lTypeDataList.Add(cAddData);
  86.  
  87. sb.AppendLine(" " + cAddData.m_strScriptName + " " + cAddData.m_strFieldName + "= null;");
  88. }
  89.  
  90. sb.AppendLine(" public void Load()");
  91. sb.AppendLine(" {");
  92.  
  93. foreach (var script in m_lTypeDataList)
  94. {
  95. sb.AppendLine(" " + "BehaviorManagerView.SetClassType(ref " + script.m_strFieldName + ");");
  96. }
  97.  
  98. sb.AppendLine(" }");
  99.  
  100. sb.AppendLine(" public void Call(FunctionData cData,params object[] param)");
  101. sb.AppendLine(" {");
  102.  
  103. foreach (var script in m_lTypeDataList)
  104. {
  105. sb.AppendLine(" " + "BehaviorManagerView.Call(" + script.m_strFieldName + ", cData, param" + ");");
  106. }
  107.  
  108. sb.AppendLine(" }");
  109. sb.AppendLine("}");
  110.  
  111. sw.Write(sb);
  112.  
  113. }
  114.  
  115. AssetDatabase.Refresh();
  116.  
  117. }
  118.  
  119.  
  120. void Update ()
  121. {
  122. Repaint();
  123. }
  124.  
  125. void OnGetClassProcess()
  126. {
  127. m_lFuncPopupDataList.Clear();
  128.  
  129. ClassParamCreate();
  130. m_cBehaviourList.Load();
  131. }
  132. int num = 0;
  133.  
  134. void OnGUI()
  135. {
  136. if (GUILayout.Button("① クラスを取得する"))
  137. {
  138. OnGetClassProcess();
  139. }
  140.  
  141. GUILayout.Space(10.0f);
  142.  
  143. EditorGUILayout.LabelField("テスト関数");
  144.  
  145.  
  146. for (int i = 0; i < m_lFuncPopupDataList.Count; i++)
  147. {
  148. FunctionData cData = m_lFuncPopupDataList[i];
  149. if (cData == null) continue;
  150.  
  151. EditorGUILayout.BeginHorizontal();
  152. {
  153. cData.m_nIndex = EditorGUILayout.Popup(cData.m_nIndex,
  154. cData.m_strNameList.ToArray());
  155.  
  156. FunctionData cMethodData = cData.m_cMethodList[cData.m_nIndex];
  157. if (cMethodData == null) continue;
  158.  
  159. cMethodData.m_nIndex = EditorGUILayout.Popup(cMethodData.m_nIndex,
  160. cMethodData.m_strNameList.ToArray());
  161.  
  162. EditorGUILayout.LabelField("ParameterSize : ", GUILayout.Width(100));
  163. num = EditorGUILayout.IntField(num, GUILayout.Width(50));
  164. }
  165. EditorGUILayout.EndHorizontal();
  166.  
  167. EditorGUILayout.BeginHorizontal();
  168. {
  169. for (int paramIndex = 0; paramIndex < num; paramIndex++)
  170. {
  171. EditorGUILayout.TextArea("");
  172. }
  173. }
  174. EditorGUILayout.EndHorizontal();
  175.  
  176. }
  177.  
  178. if (GUILayout.Button("② 追加"))
  179. {
  180. if (m_lTypeDataList.Count == 0)
  181. {
  182. OnGetClassProcess();
  183.  
  184. Debug.LogWarning("クラスリストが空だったので、取得しました。");
  185. }
  186.  
  187. FunctionData cAddData = new FunctionData();
  188.  
  189. foreach (var data in m_lTypeDataList)
  190. {
  191. cAddData.m_strNameList.Add(data.m_strScriptName);
  192.  
  193. FunctionData cMethodAddData = new FunctionData();
  194.  
  195. foreach (var method in data.m_cType.GetMembers(
  196. BindingFlags.Public | BindingFlags.NonPublic |
  197. BindingFlags.Instance | BindingFlags.Static |
  198. BindingFlags.DeclaredOnly))
  199. {
  200. // 除外するタイプ
  201. if (method.GetType().Name == "MonoCMethod") continue;
  202. if (method.GetType().Name == "MonoField") continue;
  203. if (method.GetType().Name == "MonoType") continue;
  204.  
  205. cMethodAddData.m_strNameList.Add(method.Name);
  206. }
  207.  
  208. cAddData.m_cMethodList.Add(cMethodAddData);
  209. }
  210.  
  211. m_lFuncPopupDataList.Add(cAddData);
  212.  
  213. m_bIsTestRun = false;
  214. }
  215.  
  216. GUILayout.Space(10.0f);
  217.  
  218. if (GUILayout.Button("③ 実行テストをする"))
  219. {
  220. for (int i = 0; i < m_lFuncPopupDataList.Count; i++)
  221. {
  222. FunctionData cClassData = m_lFuncPopupDataList[i];
  223. FunctionData cMethodData = cClassData.m_cMethodList[cClassData.m_nIndex];
  224.  
  225. cClassData.m_strSelectionClassName = cClassData.m_strNameList[cClassData.m_nIndex];
  226. cClassData.m_strSelectionMethodName = cMethodData.m_strNameList[cMethodData.m_nIndex];
  227.  
  228. m_cBehaviourList.Call(cClassData, null);
  229. }
  230.  
  231. m_bIsTestRun = true;
  232. }
  233.  
  234. GUILayout.Space(50.0f);
  235.  
  236. EditorGUILayout.LabelField("実行結果");
  237. GUILayout.Box("", GUILayout.Height(1), GUILayout.MinWidth(10000));
  238.  
  239. m_vScrollPosition = EditorGUILayout.BeginScrollView(m_vScrollPosition,GUI.skin.box);
  240. {
  241. if (m_bIsTestRun)
  242. {
  243. for (int i = 0; i < m_lFuncPopupDataList.Count; i++)
  244. {
  245. FunctionData cData = m_lFuncPopupDataList[i];
  246.  
  247. GUIStyle style = new GUIStyle();
  248. style.normal.textColor = cData.m_bIsCompleted ? Color.green : Color.red;
  249.  
  250. string strLabel = cData.m_strSelectionClassName + " : " + cData.m_strSelectionMethodName;
  251. strLabel += cData.m_bIsCompleted ? " <= 成功" : " <= 失敗 ( " + cData.m_strError + " )";
  252.  
  253. EditorGUILayout.LabelField(strLabel, style);
  254. }
  255. }
  256. }
  257. EditorGUILayout.EndScrollView();
  258.  
  259. }
  260.  
  261.  
  262. public static void Call<T>(T cBehaviour, FunctionData cData, params object[] param) where T : MonoBehaviour
  263. {
  264. if (cData.m_strSelectionClassName != cBehaviour.GetType().Name) return;
  265.  
  266. MethodInfo mi = cBehaviour.GetType().GetMethod(cData.m_strSelectionMethodName,
  267. BindingFlags.Public | BindingFlags.NonPublic |
  268. BindingFlags.Instance | BindingFlags.Static |
  269. BindingFlags.DeclaredOnly);
  270.  
  271. if (mi == null) return;
  272.  
  273. var findItem = m_lTypeDataList.Find(i => i.m_strScriptName == cBehaviour.GetType().Name);
  274.  
  275. if (findItem == null)
  276. {
  277. Debug.LogError(cBehaviour.GetType().Name + " がリストに登録してありません。");
  278. return;
  279. }
  280.  
  281. findItem.m_strCallFunctionName = mi.Name;
  282.  
  283. try
  284. {
  285. mi.Invoke(cBehaviour, param);
  286. cData.m_bIsCompleted = true;
  287. return;
  288. }
  289. catch (InvalidOperationException)
  290. {
  291. cData.m_bIsCompleted = false;
  292. cData.m_strError = "無効なメソッドが呼ばれました。";
  293. }
  294. catch (System.Reflection.TargetParameterCountException)
  295. {
  296. cData.m_bIsCompleted = false;
  297. cData.m_strError = "呼び出し時に指定された引数の数があっていません。";
  298. }
  299. catch (System.Reflection.TargetInvocationException)
  300. {
  301. cData.m_bIsCompleted = false;
  302. cData.m_strError = "リフレクションによって呼ばれたメソッドでErrorがありました。";
  303. }
  304. catch (MissingMethodException)
  305. {
  306. cData.m_bIsCompleted = false;
  307. cData.m_strError = "存在しないメソッドにアクセスしようとしました。";
  308. }
  309. catch (System.Reflection.TargetException)
  310. {
  311. cData.m_bIsCompleted = false;
  312. cData.m_strError = "無効なターゲットが呼ばれました。";
  313. }
  314. catch (ArgumentException)
  315. {
  316. cData.m_bIsCompleted = false;
  317. cData.m_strError = "無効な引数が存在します。";
  318. }
  319. }
  320.  
  321. public static void SetClassType<T>(ref T cBehaviour) where T : MonoBehaviour
  322. {
  323. var findBehaviourType = FindObjectOfType<T>();
  324. if (findBehaviourType == null) return;
  325.  
  326. var finItem = m_lTypeDataList.Find(i => i.m_strScriptName == findBehaviourType.GetType().FullName);
  327. if (finItem == null) return;
  328.  
  329. finItem.m_cType = findBehaviourType.GetType();
  330.  
  331. if (finItem.m_cType.BaseType != null)
  332. {
  333. finItem.m_cBaseType = finItem.m_cType.BaseType;
  334. }
  335.  
  336. cBehaviour = findBehaviourType;
  337.  
  338. }
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement