Skip to content

Commit 23037ac

Browse files
committed
Added option to get component from other GameObject
Fixed member type issue Fixed subclass issue Updated example
1 parent 5965d4f commit 23037ac

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

ComponentAttribute/Assets/ComponentAttribute/ComponentAttribute.cs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,31 @@ public static class CAExtensions {
8080
private static Dictionary<Type, List<MemberInfo>> TypeMembers = new Dictionary<Type, List<MemberInfo>>();
8181

8282
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}";
8484
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+
8591
private const string NO_WRITE = "Component Loader: Unable to write {0} on {1}";
8692
private const string NO_WRITE_ERROR = "Component Loader: Unable to write {0} on {1}, disabling it on {2}";
8793

8894
public static void LoadComponents( this MonoBehaviour behaviour ) {
8995
var bGameObject = behaviour.gameObject;
9096
var bType = behaviour.GetType();
9197
var cType = typeof( ComponentAttribute );
92-
var mType = typeof( MonoBehaviour );
98+
var mType = typeof( Component );
9399
List<MemberInfo> members;
94100

95101
if ( TypeMembers.ContainsKey( bType ) ) {
96102
members = TypeMembers[bType];
97103
} else {
98104
members = bType.GetMembers( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )
99105
.Where( m =>
100-
m.GetMemberType().IsSubclassOf( mType )
106+
( m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property )
107+
&& m.GetMemberType().IsSubclassOf( mType )
101108
&& m.GetCustomAttributes( cType, true ).Length == 1 ).ToList();
102109

103110
members.OrderBy( m => m.MemberType ).ThenBy( m => m.Name );
@@ -108,7 +115,39 @@ public static void LoadComponents( this MonoBehaviour behaviour ) {
108115
var attribute = item.GetCustomAttributes( cType, true )[0] as ComponentAttribute;
109116
var memberType = item.GetMemberType();
110117

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+
112151
if ( component == null ) {
113152
if ( attribute.AddComponentIfMissing ) {
114153
Debug.LogWarningFormat( bGameObject, MISSING_ADD, memberType.Name, behaviour.name );
@@ -154,14 +193,35 @@ sealed class ComponentAttribute : Attribute {
154193

155194
public readonly bool AddComponentIfMissing;
156195
public readonly bool DisableComponentOnError;
196+
public readonly string GameObject;
157197

158198
public ComponentAttribute() {
159199
AddComponentIfMissing = false;
160200
DisableComponentOnError = false;
201+
GameObject = "";
202+
}
203+
204+
public ComponentAttribute( bool addComponentIfMissing ) {
205+
AddComponentIfMissing = addComponentIfMissing;
206+
DisableComponentOnError = false;
207+
GameObject = "";
161208
}
162209

163210
public ComponentAttribute( bool addComponentIfMissing, bool disableComponentOnError ) {
164211
AddComponentIfMissing = addComponentIfMissing;
165212
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;
166226
}
167227
}

ComponentAttribute/Assets/Testing/FieldsExample.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
public class FieldsExample : MonoBehaviour {
55

6+
[Component( "Main Camera" )]
7+
public Camera Camera1;
8+
[Component( "Light" )]
9+
public Camera Camera2;
10+
[Component( "NotExisting GameObject" )]
11+
public Camera Camera3;
12+
613
[Component]
714
public BoxCollider2D Collider1;
815
[Component( true, false )]

ComponentAttribute/Assets/Testing/PropertiesExample.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
public class PropertiesExample : MonoBehaviour {
55

6+
[Component( "Main Camera" )]
7+
public Camera Camera1 { get; set; }
8+
[Component( "Light" )]
9+
public Camera Camera2 { get; set; }
10+
[Component( "NotExisting GameObject" )]
11+
public Camera Camera3 { get; set; }
12+
613
[Component]
714
public BoxCollider2D Collider1 { get; set; }
815
[Component( true, false )]
276 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)