ScriptSelector.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace UnityTest
  7. {
  8. public class ScriptSelector
  9. {
  10. public List<MethodInfo> GetSortedMenuList(Type type, Type firstArgumentType, Type secondArgumentType)
  11. {
  12. var list = GetMethodList(type, firstArgumentType, secondArgumentType);
  13. var result = new List<MethodInfo> ();
  14. result.AddRange(FilterDeclaredPublic(list));
  15. result.AddRange(FilterDeclaredStatic(list));
  16. result.AddRange(FilterInheritedPublic(list));
  17. result.AddRange(FilterInheritedStatic(list));
  18. return result;
  19. }
  20. public List<MethodInfo> GetStaticSortedMenuList(Type type, Type firstArgument, Type secondArgument)
  21. {
  22. var list = GetMethodList(type, firstArgument, secondArgument);
  23. var result = new List<MethodInfo>();
  24. result.AddRange(FilterDeclaredStatic(list));
  25. result.AddRange(FilterInheritedStatic(list));
  26. return result;
  27. }
  28. internal List<MethodInfo> GetMethodList(Type classType, Type firstArgumentType, Type secondArgumentType)
  29. {
  30. var allPublic = classType.GetMethods();
  31. var allStatic = classType.GetMethods(BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
  32. var result = new List<MethodInfo> ().Union (allPublic).Union (allStatic);
  33. result = result.Where (info => !info.ContainsGenericParameters);
  34. result = result.Where (info =>
  35. {
  36. var methodParams = info.GetParameters ();
  37. if (methodParams.Length == 0) return true;
  38. if (firstArgumentType!=null && methodParams.Length == 1)
  39. {
  40. if (methodParams[0].ParameterType.IsAssignableFrom(firstArgumentType) || methodParams[0].ParameterType.IsAssignableFrom(typeof(GameObject)))
  41. return true;
  42. }
  43. if (firstArgumentType != null && secondArgumentType!=null && methodParams.Length == 2)
  44. {
  45. if ((methodParams[0].ParameterType.IsAssignableFrom(firstArgumentType)
  46. && methodParams[1].ParameterType.IsAssignableFrom(secondArgumentType))
  47. ||
  48. (methodParams[0].ParameterType.IsAssignableFrom(typeof(GameObject))
  49. && methodParams[1].ParameterType.IsAssignableFrom(typeof(GameObject)))
  50. )
  51. return true;
  52. }
  53. return false;
  54. });
  55. return result.ToList ();
  56. }
  57. internal IEnumerable<MethodInfo> FilterDeclaredPublic (IEnumerable<MethodInfo> methods)
  58. {
  59. return methods.Where(m => m.IsPublic && !m.IsStatic && m.ReflectedType == m.DeclaringType);
  60. }
  61. internal IEnumerable<MethodInfo> FilterDeclaredStatic (IEnumerable<MethodInfo> methods)
  62. {
  63. return methods.Where(m => m.IsPublic && m.IsStatic && m.ReflectedType == m.DeclaringType);
  64. }
  65. internal IEnumerable<MethodInfo> FilterInheritedPublic (IEnumerable<MethodInfo> methods)
  66. {
  67. return methods.Where(m => m.IsPublic && !m.IsStatic && m.ReflectedType != m.DeclaringType);
  68. }
  69. internal IEnumerable<MethodInfo> FilterInheritedStatic (IEnumerable<MethodInfo> methods)
  70. {
  71. return methods.Where(m => m.IsPublic && m.IsStatic && m.ReflectedType != m.DeclaringType);
  72. }
  73. }
  74. }