Tanoda
pb_ComponentExtension.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections;
4
5namespace GILES
6{
10 public static class pb_ComponentExtension
11 {
18 public static Component AddComponent(this GameObject go, pb_ISerializable component)
19 {
20 if( !typeof(Component).IsAssignableFrom(component.type) )
21 {
22 Debug.LogError(component.type + " does not inherit UnityEngine.Component!");
23 return null;
24 }
25
26 Component c = go.AddComponent(component.type);
27
28 component.ApplyProperties(c);
29
30 return c;
31 }
32
36 public static T DemandComponent<T>(this GameObject go) where T : UnityEngine.Component
37 {
38 T component = go?.GetComponent<T>();
39
40 if(component == null)
41 component = go?.AddComponent<T>();
42
43 return component;
44 }
45 }
46}
UnityEngine.Component Component
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void ApplyProperties(object obj)
Called after an object is deserialized and constructed to it's base type.
System.Type type
The type of component stored.