99using UnityObject = UnityEngine . Object ;
1010
1111namespace UInspectorPlus {
12- enum PropertyType {
12+ internal enum PropertyType {
1313 Unknown ,
1414 Bool ,
1515 Enum ,
@@ -35,25 +35,25 @@ enum PropertyType {
3535 RectInt ,
3636 }
3737
38- enum MethodMode {
38+ internal enum MethodMode {
3939 Method ,
4040 Constructor ,
4141 Indexer
4242 }
4343
44- struct ComponentMethod {
44+ internal struct ComponentMethod {
4545 public MethodMode mode ;
4646 public MemberInfo member ;
4747 public object target ;
4848 }
4949
50- struct ComponentFields {
50+ internal struct ComponentFields {
5151 public FieldInfo field ;
5252 public PropertyInfo property ;
5353 public UnityObject target ;
5454 }
5555
56- interface IReflectorDrawer {
56+ internal interface IReflectorDrawer {
5757 void Draw ( ) ;
5858 bool UpdateIfChanged ( ) ;
5959 bool UpdateValue ( ) ;
@@ -68,30 +68,18 @@ interface IReflectorDrawer {
6868 public static class Helper {
6969 internal static readonly Dictionary < Type , PropertyType > propertyTypeMapper = new Dictionary < Type , PropertyType > ( ) ;
7070 internal static readonly Dictionary < Type , HashSet < string > > blackListedTypes = new Dictionary < Type , HashSet < string > > ( ) ;
71- static double clickTime ;
72-
71+ private static double clickTime ;
72+
7373 // Delegate hacks to access the internal methods
74- internal static readonly Func < GUIContent , Rect , Gradient , Gradient > doGradientField = Delegate . CreateDelegate (
75- typeof ( Func < GUIContent , Rect , Gradient , Gradient > ) ,
76- typeof ( EditorGUI ) . GetMethod (
77- "GradientField" ,
78- BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Static ,
79- null , CallingConventions . Any ,
80- new [ ] { typeof ( GUIContent ) , typeof ( Rect ) , typeof ( Gradient ) } ,
81- null
82- )
83- ) as Func < GUIContent , Rect , Gradient , Gradient > ;
74+ private delegate Gradient DoGradientField ( GUIContent guiContent , Rect rect , Gradient gradient ) ;
75+ private static readonly DoGradientField doGradientField =
76+ GetDelegate < DoGradientField > ( typeof ( EditorGUI ) , "GradientField" ) ;
8477
85- internal static readonly Func < GUIContent , Gradient , GUILayoutOption [ ] , Gradient > doLayoutGradiantField = Delegate . CreateDelegate (
86- typeof ( Func < GUIContent , Gradient , GUILayoutOption [ ] , Gradient > ) ,
87- typeof ( EditorGUILayout ) . GetMethod (
88- "GradientField" ,
89- BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Static ,
90- null , CallingConventions . Any ,
91- new [ ] { typeof ( GUIContent ) , typeof ( Gradient ) , typeof ( GUILayoutOption [ ] ) } ,
92- null
93- )
94- ) as Func < GUIContent , Gradient , GUILayoutOption [ ] , Gradient > ;
78+ private delegate Gradient DoLayoutGradientField ( GUIContent guiContent , Gradient gradient , params GUILayoutOption [ ] options ) ;
79+ private static readonly DoLayoutGradientField doLayoutGradiantField =
80+ GetDelegate < DoLayoutGradientField > ( typeof ( EditorGUILayout ) , "GradientField" ) ;
81+
82+ private static readonly Hashtable storedState = new Hashtable ( ) ;
9583
9684 internal static void InitPropertyTypeMapper ( ) {
9785 if ( propertyTypeMapper . Count > 0 )
@@ -124,20 +112,20 @@ internal static void InitPropertyTypeMapper() {
124112 AddPropertyTypeMap ( "UnityEngine.Vector3Int, UnityEngine.dll" , PropertyType . Vector3Int ) ;
125113 AddPropertyTypeMap ( "UnityEngine.RectInt, UnityEngine.dll" , PropertyType . RectInt ) ;
126114
127- // Danger properties! Do not use them or they will instanlize junks
115+ // Danger properties! Do not use them or they will instanate junks
128116 AddBlacklistedType ( "UnityEngine.MeshFilter, UnityEngine.dll" , "mesh" ) ;
129117 AddBlacklistedType ( "UnityEngine.Renderer, UnityEngine.dll" , "material" , "materials" ) ;
130118 AddBlacklistedType ( "UnityEngine.Collider, UnityEngine.dll" , "material" ) ;
131119 AddBlacklistedType ( "UnityEngine.Collider2D, UnityEngine.dll" , "material" ) ;
132120 }
133121
134- static void AddPropertyTypeMap ( string typeName , PropertyType propType ) {
122+ private static void AddPropertyTypeMap ( string typeName , PropertyType propType ) {
135123 Type type = Type . GetType ( typeName , false , false ) ;
136124 if ( type != null )
137125 propertyTypeMapper . Add ( type , propType ) ;
138126 }
139127
140- static void AddBlacklistedType ( string typeName , params string [ ] props ) {
128+ private static void AddBlacklistedType ( string typeName , params string [ ] props ) {
141129 Type type = Type . GetType ( typeName , false , false ) ;
142130 if ( type == null ) return ;
143131 HashSet < string > list ;
@@ -146,8 +134,6 @@ static void AddBlacklistedType(string typeName, params string[] props) {
146134 list . UnionWith ( props ) ;
147135 }
148136
149- static readonly Hashtable storedState = new Hashtable ( ) ;
150-
151137 internal static void StoreState ( object key , object value ) {
152138 if ( storedState . ContainsKey ( key ) )
153139 storedState [ key ] = value ;
@@ -279,8 +265,8 @@ internal static object EnumField(GUIContent label, Type type, object value, para
279265 return EnumFieldPostProcess ( itemValues , newVal ) ;
280266 }
281267
282- static int EnumFieldPreProcess ( Type type , object rawValue , out GUIContent [ ] itemNames , out Array itemValues ) {
283- itemNames = Enum . GetNames ( type ) . Select ( x => new GUIContent ( x ) ) . ToArray ( ) ;
268+ private static int EnumFieldPreProcess ( Type type , object rawValue , out GUIContent [ ] itemNames , out Array itemValues ) {
269+ itemNames = Array . ConvertAll ( Enum . GetNames ( type ) , x => new GUIContent ( x ) ) ;
284270 itemValues = Enum . GetValues ( type ) ;
285271 long val = Convert . ToInt64 ( rawValue ) ;
286272 for ( int i = 0 ; i < itemValues . Length ; i ++ )
@@ -289,7 +275,7 @@ static int EnumFieldPreProcess(Type type, object rawValue, out GUIContent[] item
289275 return 0 ;
290276 }
291277
292- static object EnumFieldPostProcess ( Array itemValues , int val ) {
278+ private static object EnumFieldPostProcess ( Array itemValues , int val ) {
293279 return itemValues . GetValue ( val ) ;
294280 }
295281
@@ -318,7 +304,7 @@ internal static object MaskedEnumField(GUIContent label, Type type, object mask,
318304 return MaskedEnumFieldPostProcess ( type , itemValues , mask , val , newVal ) ;
319305 }
320306
321- static int MaskedEnumFieldPreProcess ( Type type , object rawValue , out string [ ] itemNames , out Array itemValues ) {
307+ private static int MaskedEnumFieldPreProcess ( Type type , object rawValue , out string [ ] itemNames , out Array itemValues ) {
322308 itemNames = Enum . GetNames ( type ) ;
323309 itemValues = Enum . GetValues ( type ) ;
324310 int maskVal = 0 ;
@@ -334,7 +320,7 @@ static int MaskedEnumFieldPreProcess(Type type, object rawValue, out string[] it
334320 return maskVal ;
335321 }
336322
337- static object MaskedEnumFieldPostProcess ( Type enumType , Array itemValues , object rawValue , int maskVal , int newMaskVal ) {
323+ private static object MaskedEnumFieldPostProcess ( Type enumType , Array itemValues , object rawValue , int maskVal , int newMaskVal ) {
338324 int changes = maskVal ^ newMaskVal ;
339325 long value = Convert . ToInt64 ( rawValue ) , itemValue ;
340326 for ( int i = 0 ; i < itemValues . Length ; i ++ )
@@ -429,22 +415,22 @@ internal static UnityObject ObjectField(Rect position, GUIContent label, UnityOb
429415 }
430416
431417 internal static Gradient GradientField ( Rect position , GUIContent label , Gradient value ) {
432- return doGradientField . Invoke ( label , position , value ) ;
418+ return doGradientField ( label , position , value ) ;
433419 }
434420
435421 internal static Gradient GradientField ( GUIContent label , Gradient value , params GUILayoutOption [ ] options ) {
436- return doLayoutGradiantField . Invoke ( label , value , options ) ;
422+ return doLayoutGradiantField ( label , value , options ) ;
437423 }
438424
439- static void ClickObject ( UnityObject obj ) {
425+ private static void ClickObject ( UnityObject obj ) {
440426 var newClickTime = EditorApplication . timeSinceStartup ;
441427 if ( newClickTime - clickTime < 0.3 && obj != null )
442428 Selection . activeObject = obj ;
443429 clickTime = newClickTime ;
444430 EditorGUIUtility . PingObject ( obj ) ;
445431 }
446432
447- static int CountLines ( string str ) {
433+ private static int CountLines ( string str ) {
448434 if ( string . IsNullOrEmpty ( str ) )
449435 return 1 ;
450436 int cursor = 0 , count = 0 , length = str . Length , i = - 1 ;
@@ -551,7 +537,27 @@ internal static GUIStyle GetGUIStyle(string styleName) {
551537 return value == null ? defaultValue : ( T ) value ;
552538 }
553539
554- [ MenuItem ( "Window/Script Tester/Inspector+" ) ]
540+ internal static T GetDelegate < T > ( Type fromType , string methodName ) where T : class {
541+ Type t = typeof ( T ) ;
542+ if ( ! t . IsSubclassOf ( typeof ( Delegate ) ) )
543+ throw new ArgumentException ( string . Format ( "{0} is not delegate." , t . Name ) ) ;
544+ MethodInfo invokeMethod = t . GetMethod ( "Invoke" ) ;
545+ if ( invokeMethod == null )
546+ throw new ArgumentException ( string . Format (
547+ "Cannot determine what parameters does {0} have, as no Invoke(...) signature found. Perhaps this is not a valid delegate." ,
548+ t . Name
549+ ) ) ;
550+ MethodInfo method = fromType . GetMethod (
551+ methodName ,
552+ BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Static ,
553+ null , CallingConventions . Any ,
554+ Array . ConvertAll ( invokeMethod . GetParameters ( ) , p => p . ParameterType ) ,
555+ null
556+ ) ;
557+ return method == null ? null : Delegate . CreateDelegate ( t , method ) as T ;
558+ }
559+
560+ [ MenuItem ( "Window/Inspector+" ) ]
555561 public static void ShowInspectorPlus ( ) {
556562 EditorWindow . GetWindow ( typeof ( InspectorPlus ) ) ;
557563 }
0 commit comments