@@ -13,17 +13,39 @@ public class InspectorEditor : UnityEditor.Editor
1313 {
1414 private SerializedProperty script ;
1515
16- private IEnumerable < FieldInfo > fields ;
17- private HashSet < FieldInfo > groupedFields ;
18- private Dictionary < string , List < FieldInfo > > groupedFieldsByGroupName ;
19- private IEnumerable < FieldInfo > nonSerializedFields ;
20- private IEnumerable < PropertyInfo > nativeProperties ;
21- private IEnumerable < MethodInfo > methods ;
16+ private List < FieldInfo > fields ;
17+ private readonly HashSet < FieldInfo > groupedFields ;
18+ private readonly Dictionary < string , List < FieldInfo > > groupedFieldsByGroupName ;
19+ private List < FieldInfo > nonSerializedFields ;
20+ private List < PropertyInfo > nativeProperties ;
21+ private List < MethodInfo > methods ;
2222
23- private Dictionary < string , SerializedProperty > serializedPropertiesByFieldName ;
23+ private readonly Dictionary < string , SerializedProperty > serializedPropertiesByFieldName ;
2424
2525 private bool useDefaultInspector ;
2626
27+ bool FilterFields ( FieldInfo f ) => this . serializedObject . FindProperty ( f . Name ) != null ;
28+ readonly Func < FieldInfo , bool > filterFieldsDelegate ;
29+
30+ // Cache non-serialized fields
31+ bool NonSerializedFieldsFilter ( FieldInfo f )
32+ {
33+ return f . GetCustomAttribute < DrawerAttribute > ( true ) != null && this . serializedObject . FindProperty ( f . Name ) == null ;
34+ }
35+
36+
37+ public InspectorEditor ( )
38+ {
39+ groupedFieldsByGroupName = new Dictionary < string , List < FieldInfo > > ( ) ;
40+ serializedPropertiesByFieldName = new Dictionary < string , SerializedProperty > ( ) ;
41+ groupedFields = new HashSet < FieldInfo > ( ) ;
42+ fields = new List < FieldInfo > ( 100 ) ;
43+ nonSerializedFields = new List < FieldInfo > ( 100 ) ;
44+ nativeProperties = new List < PropertyInfo > ( 100 ) ;
45+ methods = new List < MethodInfo > ( 100 ) ;
46+ filterFieldsDelegate = FilterFields ;
47+ }
48+
2749 private void OnEnable ( )
2850 {
2951 try
@@ -38,10 +60,16 @@ private void OnEnable()
3860 }
3961
4062 // Cache serialized fields
41- this . fields = ReflectionUtility . GetAllFields ( this . target , f => this . serializedObject . FindProperty ( f . Name ) != null ) ;
63+ this . fields = ReflectionUtility . GetAllFieldsEfficiently ( this . target , filterFieldsDelegate , this . fields ) ;
64+ // Cache serialized properties by field name
65+ this . serializedPropertiesByFieldName . Clear ( ) ;
66+ foreach ( var field in this . fields )
67+ {
68+ this . serializedPropertiesByFieldName [ field . Name ] = this . serializedObject . FindProperty ( field . Name ) ;
69+ }
4270
4371 // If there are no NaughtyAttributes use default inspector
44- if ( this . fields . All ( f => f . GetCustomAttributes ( typeof ( NaughtyAttribute ) , true ) . Length == 0 ) )
72+ if ( this . fields . All ( f => f . GetCustomAttribute < NaughtyAttribute > ( true ) == null ) )
4573 {
4674 this . useDefaultInspector = true ;
4775 }
@@ -50,17 +78,24 @@ private void OnEnable()
5078 this . useDefaultInspector = false ;
5179
5280 // Cache grouped fields
53- this . groupedFields = new HashSet < FieldInfo > ( this . fields . Where ( f => f . GetCustomAttributes ( typeof ( GroupAttribute ) , true ) . Length > 0 ) ) ;
54-
81+ this . groupedFields . Clear ( ) ;
82+ foreach ( var fi in fields )
83+ {
84+ if ( fi . GetCustomAttribute < GroupAttribute > ( true ) != null )
85+ {
86+ this . groupedFields . Add ( fi ) ;
87+ }
88+ }
89+
5590 // Cache grouped fields by group name
56- this . groupedFieldsByGroupName = new Dictionary < string , List < FieldInfo > > ( ) ;
91+ groupedFieldsByGroupName . Clear ( ) ;
5792 foreach ( var groupedField in this . groupedFields )
5893 {
59- string groupName = ( groupedField . GetCustomAttributes ( typeof ( GroupAttribute ) , true ) [ 0 ] as GroupAttribute ) . Name ;
94+ string groupName = ( groupedField . GetCustomAttribute < GroupAttribute > ( true ) ) . Name ;
6095
61- if ( this . groupedFieldsByGroupName . ContainsKey ( groupName ) )
96+ if ( this . groupedFieldsByGroupName . TryGetValue ( groupName , out var list ) )
6297 {
63- this . groupedFieldsByGroupName [ groupName ] . Add ( groupedField ) ;
98+ list . Add ( groupedField ) ;
6499 }
65100 else
66101 {
@@ -71,25 +106,17 @@ private void OnEnable()
71106 }
72107 }
73108
74- // Cache serialized properties by field name
75- this . serializedPropertiesByFieldName = new Dictionary < string , SerializedProperty > ( ) ;
76- foreach ( var field in this . fields )
77- {
78- this . serializedPropertiesByFieldName [ field . Name ] = this . serializedObject . FindProperty ( field . Name ) ;
79- }
80109 }
81110
82- // Cache non-serialized fields
83- this . nonSerializedFields = ReflectionUtility . GetAllFields (
84- this . target , f => f . GetCustomAttributes ( typeof ( DrawerAttribute ) , true ) . Length > 0 && this . serializedObject . FindProperty ( f . Name ) == null ) ;
111+ this . nonSerializedFields = ReflectionUtility . GetAllFieldsEfficiently ( this . target , NonSerializedFieldsFilter , nonSerializedFields ) ;
85112
86113 // Cache the native properties
87- this . nativeProperties = ReflectionUtility . GetAllProperties (
88- this . target , p => p . GetCustomAttributes ( typeof ( DrawerAttribute ) , true ) . Length > 0 ) ;
114+ this . nativeProperties = ReflectionUtility . GetAllPropertiesEfficiently (
115+ this . target , p => p . GetCustomAttribute < DrawerAttribute > ( true ) != null , nativeProperties ) ;
89116
90117 // Cache methods with DrawerAttribute
91- this . methods = ReflectionUtility . GetAllMethods (
92- this . target , m => m . GetCustomAttributes ( typeof ( DrawerAttribute ) , true ) . Length > 0 ) ;
118+ this . methods = ReflectionUtility . GetAllMethodsEfficiently (
119+ this . target , m => m . GetCustomAttribute < DrawerAttribute > ( true ) != null , methods ) ;
93120 }
94121
95122 private void OnDisable ( )
0 commit comments