Skip to content

Commit 6f3b289

Browse files
author
Christiaan Bloemendaal
committed
Added caching
1 parent 1a35bad commit 6f3b289

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

ComponentAttribute/Assets/ComponentAttribute/ExtendedBehaviour.cs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45
using UnityEngine;
56

67
public class ExtendedBehaviour : MonoBehaviour {
78

89
protected virtual void Awake() {
10+
var bType = GetType();
911
var cType = typeof( ComponentAttribute );
10-
var fields = GetType().GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
11-
.Where( f => f.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
12+
List<FieldInfo> fields;
13+
List<PropertyInfo> properties;
14+
15+
if ( MonoBehaviourExtensions.TypeMembers.ContainsKey( bType ) ) {
16+
var members = MonoBehaviourExtensions.TypeMembers[bType];
17+
fields = members.Fields;
18+
properties = members.Properties;
19+
} else {
20+
fields = GetType().GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
21+
.Where( f => f.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
22+
properties = GetType().GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
23+
.Where( p => p.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
24+
25+
MonoBehaviourExtensions.TypeMembers.Add( bType,
26+
new MonoBehaviourExtensions.Members() {
27+
Fields = fields,
28+
Properties = properties
29+
} );
30+
}
1231

1332
foreach ( var item in fields ) {
1433
var component = GetComponent( item.FieldType );
@@ -21,9 +40,6 @@ protected virtual void Awake() {
2140
}
2241
}
2342

24-
var properties = GetType().GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
25-
.Where( p => p.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
26-
2743
foreach ( var item in properties ) {
2844
var component = GetComponent( item.PropertyType );
2945
if ( component != null ) {

ComponentAttribute/Assets/ComponentAttribute/MonoBehaviourExtensions.cs

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45
using UnityEngine;
56

67
public static class MonoBehaviourExtensions {
78

9+
public class Members {
10+
public List<FieldInfo> Fields;
11+
public List<PropertyInfo> Properties;
12+
}
13+
14+
public static Dictionary<Type, Members> TypeMembers = new Dictionary<Type, Members>();
15+
816
public static void LoadComponents( this MonoBehaviour behaviour ) {
17+
var bType = behaviour.GetType();
918
var cType = typeof( ComponentAttribute );
10-
var fields = behaviour.GetType().GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
11-
.Where( f => f.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
19+
List<FieldInfo> fields;
20+
List<PropertyInfo> properties;
21+
22+
if ( TypeMembers.ContainsKey( bType ) ) {
23+
var members = TypeMembers[bType];
24+
fields = members.Fields;
25+
properties = members.Properties;
26+
} else {
27+
fields = behaviour.GetType().GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
28+
.Where( f => f.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
29+
properties = behaviour.GetType().GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
30+
.Where( p => p.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
31+
32+
TypeMembers.Add( bType, new Members() {
33+
Fields = fields,
34+
Properties = properties
35+
} );
36+
}
1237

1338
foreach ( var item in fields ) {
1439
var component = behaviour.GetComponent( item.FieldType );
@@ -21,9 +46,6 @@ public static void LoadComponents( this MonoBehaviour behaviour ) {
2146
}
2247
}
2348

24-
var properties = behaviour.GetType().GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
25-
.Where( p => p.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
26-
2749
foreach ( var item in properties ) {
2850
var component = behaviour.GetComponent( item.PropertyType );
2951
if ( component != null ) {

0 commit comments

Comments
 (0)