@@ -80,24 +80,31 @@ public static class CAExtensions {
80
80
private static Dictionary < Type , List < MemberInfo > > TypeMembers = new Dictionary < Type , List < MemberInfo > > ( ) ;
81
81
82
82
private const string MISSING = "Component Loader: Unable to load {0} on {1}" ;
83
- private const string MISSING_ADD = "Component Loader: Unable to load {0}, adding it on {1}" ;
83
+ private const string MISSING_ADD = "Component Loader: Adding {0} on {1}" ;
84
84
private const string MISSING_ERROR = "Component Loader: Unable to load {0}, disabling {1} on {2}" ;
85
+
86
+ private const string MISSING_OBJECT = "Component Loader: Unable to find a GameObject named {0}" ;
87
+ private const string MISSING_OBJECT_ADD = "Component Loader: Adding {0} on {1} for {2}" ;
88
+ private const string MISSING_OBJECT_ERROR = "Component Loader: Unable to find a GameObject named {0}, disabling {1} on {2}" ;
89
+ private const string MISSING_OBJECT_CERROR = "Component Loader: Unable to load {0} on {1}, disabling {2} on {3}" ;
90
+
85
91
private const string NO_WRITE = "Component Loader: Unable to write {0} on {1}" ;
86
92
private const string NO_WRITE_ERROR = "Component Loader: Unable to write {0} on {1}, disabling it on {2}" ;
87
93
88
94
public static void LoadComponents ( this MonoBehaviour behaviour ) {
89
95
var bGameObject = behaviour . gameObject ;
90
96
var bType = behaviour . GetType ( ) ;
91
97
var cType = typeof ( ComponentAttribute ) ;
92
- var mType = typeof ( MonoBehaviour ) ;
98
+ var mType = typeof ( Component ) ;
93
99
List < MemberInfo > members ;
94
100
95
101
if ( TypeMembers . ContainsKey ( bType ) ) {
96
102
members = TypeMembers [ bType ] ;
97
103
} else {
98
104
members = bType . GetMembers ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic )
99
105
. Where ( m =>
100
- m . GetMemberType ( ) . IsSubclassOf ( mType )
106
+ ( m . MemberType == MemberTypes . Field || m . MemberType == MemberTypes . Property )
107
+ && m . GetMemberType ( ) . IsSubclassOf ( mType )
101
108
&& m . GetCustomAttributes ( cType , true ) . Length == 1 ) . ToList ( ) ;
102
109
103
110
members . OrderBy ( m => m . MemberType ) . ThenBy ( m => m . Name ) ;
@@ -108,7 +115,39 @@ public static void LoadComponents( this MonoBehaviour behaviour ) {
108
115
var attribute = item . GetCustomAttributes ( cType , true ) [ 0 ] as ComponentAttribute ;
109
116
var memberType = item . GetMemberType ( ) ;
110
117
111
- var component = behaviour . GetComponent ( memberType ) ;
118
+ Component component = null ;
119
+
120
+ if ( string . IsNullOrEmpty ( attribute . GameObject ) ) {
121
+ component = behaviour . GetComponent ( memberType ) ;
122
+ } else {
123
+ var gObj = GameObject . Find ( attribute . GameObject ) ;
124
+ if ( gObj != null ) {
125
+ component = gObj . GetComponent ( memberType ) ;
126
+ } else {
127
+ if ( attribute . DisableComponentOnError ) {
128
+ Debug . LogErrorFormat ( bGameObject , MISSING_OBJECT_ERROR , attribute . GameObject , bType . Name , behaviour . name ) ;
129
+ return ;
130
+ } else {
131
+ Debug . LogWarningFormat ( bGameObject , MISSING_OBJECT , attribute . GameObject ) ;
132
+ continue ;
133
+ }
134
+ }
135
+
136
+ if ( component == null ) {
137
+ if ( attribute . AddComponentIfMissing ) {
138
+ Debug . LogWarningFormat ( bGameObject , MISSING_OBJECT_ADD , memberType . Name , gObj . name , behaviour . name ) ;
139
+ component = gObj . AddComponent ( memberType ) ;
140
+ } else if ( attribute . DisableComponentOnError ) {
141
+ Debug . LogErrorFormat ( bGameObject , MISSING_OBJECT_CERROR , memberType . Name , gObj . name , bType . Name , behaviour . name ) ;
142
+ behaviour . enabled = false ;
143
+ return ;
144
+ } else {
145
+ Debug . LogWarningFormat ( bGameObject , MISSING , memberType . Name , gObj . name ) ;
146
+ continue ;
147
+ }
148
+ }
149
+ }
150
+
112
151
if ( component == null ) {
113
152
if ( attribute . AddComponentIfMissing ) {
114
153
Debug . LogWarningFormat ( bGameObject , MISSING_ADD , memberType . Name , behaviour . name ) ;
@@ -154,14 +193,35 @@ sealed class ComponentAttribute : Attribute {
154
193
155
194
public readonly bool AddComponentIfMissing ;
156
195
public readonly bool DisableComponentOnError ;
196
+ public readonly string GameObject ;
157
197
158
198
public ComponentAttribute ( ) {
159
199
AddComponentIfMissing = false ;
160
200
DisableComponentOnError = false ;
201
+ GameObject = "" ;
202
+ }
203
+
204
+ public ComponentAttribute ( bool addComponentIfMissing ) {
205
+ AddComponentIfMissing = addComponentIfMissing ;
206
+ DisableComponentOnError = false ;
207
+ GameObject = "" ;
161
208
}
162
209
163
210
public ComponentAttribute ( bool addComponentIfMissing , bool disableComponentOnError ) {
164
211
AddComponentIfMissing = addComponentIfMissing ;
165
212
DisableComponentOnError = disableComponentOnError ;
213
+ GameObject = "" ;
214
+ }
215
+
216
+ public ComponentAttribute ( string gameObject ) {
217
+ AddComponentIfMissing = false ;
218
+ DisableComponentOnError = false ;
219
+ GameObject = gameObject ;
220
+ }
221
+
222
+ public ComponentAttribute ( string gameObject , bool addComponentIfMissing , bool disableComponentOnError ) {
223
+ AddComponentIfMissing = addComponentIfMissing ;
224
+ DisableComponentOnError = disableComponentOnError ;
225
+ GameObject = gameObject ;
166
226
}
167
227
}
0 commit comments