@@ -12,29 +12,47 @@ namespace UnityExplorer.Inspectors.Reflection
12
12
{
13
13
public class InteractiveValue
14
14
{
15
+ /// <summary>
16
+ /// Get the <see cref="InteractiveValue"/> subclass which supports the provided <paramref name="type"/>.
17
+ /// </summary>
18
+ /// <param name="type">The <see cref="Type"/> which you want the <see cref="InteractiveValue"/> Type for.</param>
19
+ /// <returns>The best subclass of <see cref="InteractiveValue"/> which supports the provided <paramref name="type"/>.</returns>
15
20
public static Type GetIValueForType ( Type type )
16
21
{
22
+ // rather ugly but I couldn't think of a cleaner way that was worth it.
23
+ // switch-case doesn't really work here.
24
+
25
+ // arbitrarily check some types, fastest methods first.
17
26
if ( type == typeof ( bool ) )
18
27
return typeof ( InteractiveBool ) ;
19
- else if ( type == typeof ( string ) )
20
- return typeof ( InteractiveString ) ;
28
+ // if type is primitive then it must be a number if its not a bool
21
29
else if ( type . IsPrimitive )
22
30
return typeof ( InteractiveNumber ) ;
31
+ // check for strings
32
+ else if ( type == typeof ( string ) )
33
+ return typeof ( InteractiveString ) ;
34
+ // check for enum/flags
23
35
else if ( typeof ( Enum ) . IsAssignableFrom ( type ) )
24
36
{
25
- if ( type . GetCustomAttributes ( typeof ( FlagsAttribute ) , true ) is object [ ] attributes && attributes . Length > 0 )
37
+ // NET 3.5 doesn't have "GetCustomAttribute", gotta use the multiple version.
38
+ if ( type . GetCustomAttributes ( typeof ( FlagsAttribute ) , true ) is object [ ] fa && fa . Length > 0 )
26
39
return typeof ( InteractiveFlags ) ;
27
40
else
28
41
return typeof ( InteractiveEnum ) ;
29
42
}
43
+ // check for unity struct types
30
44
else if ( InteractiveUnityStruct . SupportsType ( type ) )
31
45
return typeof ( InteractiveUnityStruct ) ;
46
+ // check Transform, force InteractiveValue so they dont become InteractiveEnumerables.
32
47
else if ( typeof ( Transform ) . IsAssignableFrom ( type ) )
33
48
return typeof ( InteractiveValue ) ;
49
+ // check Dictionaries before Enumerables
34
50
else if ( ReflectionHelpers . IsDictionary ( type ) )
35
51
return typeof ( InteractiveDictionary ) ;
52
+ // finally check for Enumerables
36
53
else if ( ReflectionHelpers . IsEnumerable ( type ) )
37
54
return typeof ( InteractiveEnumerable ) ;
55
+ // fallback to default
38
56
else
39
57
return typeof ( InteractiveValue ) ;
40
58
}
0 commit comments