-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathpb_ComponentExtension.cs
More file actions
46 lines (39 loc) · 1.29 KB
/
pb_ComponentExtension.cs
File metadata and controls
46 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
using System.Collections;
using GILES.Serialization;
namespace GILES
{
/**
* Extension classes that make working with serialized types interchangeably with unity types easier
*/
public static class pb_ComponentExtension
{
///<summary>
/// Attempts to add a component from a pb_ISerializable object. If `component` is not a type
/// inheriting `UnityEngine.Component`, a null value is returned. Otherwise the component is
/// added to the gameObject `go` and it's fields are populated using the values stored in the
/// dictionary as set by pb_ISerializable.PopulateDictionaryValues().
///</summary>
public static Component AddComponent(this GameObject go, pb_ISerializable component)
{
if( !typeof(Component).IsAssignableFrom(component.type) )
{
Debug.LogError(component.type + " does not inherit UnityEngine.Component!");
return null;
}
Component c = go.AddComponent(component.type);
component.ApplyProperties(c);
return c;
}
///<summary>
/// Shortcut for if(!GetComponent<T>) AddComponent<T>.
///</summary>
public static T DemandComponent<T>(this GameObject go) where T : UnityEngine.Component
{
T component = go.GetComponent<T>();
if(component == null)
component = go.AddComponent<T>();
return component;
}
}
}