Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Components Class Reference

Additional static functions and constants used to extend existing Component support inside of Unity. More...

Static Public Member Functions

static T AddComponent< T > (this GameObject targetObject, T cachedReference)
 Adds a component to a GameObject, if that component is not already present. More...
 
static T GetComponentIfNull< T > (this Component targetObject, T cachedReference)
 Get a component reference, checking if its already referenced. More...
 
static T GetComponentIfNull< T > (this GameObject targetObject, T cachedReference)
 Get a component reference, checking if its already referenced. More...
 
static T GetComponentInParents< T > (this GameObject targetObject, T cachedReference)
 Get a component reference from a gameObjects parents. More...
 
static bool HasComponent< T > (this GameObject targetObject, T cachedReference)
 Does the GameObject have the component? More...
 

Public Attributes

const string DefaultSingletonName = "Hydrogen"
 The GameObject name used when creating singletons automatically via any of the Hydrogen components. More...
 

Detailed Description

Additional static functions and constants used to extend existing Component support inside of Unity.

Definition at line 37 of file Components.cs.

Member Function Documentation

static T Hydrogen.Components.AddComponent< T > ( this GameObject  targetObject,
cachedReference 
)
static

Adds a component to a GameObject, if that component is not already present.

Returns
The desired component.
Parameters
targetObjectThe root object to add the component to.
cachedReferencePossible pre-existing reference to component.
Template Parameters
TObject Type.

private AudioSource _localAudioSource = null; public void Start() { _localAudioSource = gameObject.AddComponent<AudioSource>(_localAudioSource); }

Type Constraints
T :Component 

Definition at line 58 of file Components.cs.

58  : Component
59  {
60  if (cachedReference != null) {
61  return cachedReference;
62  }
63 
64  T component = (T)targetObject.GetComponent (typeof(T)) ?? (T)targetObject.AddComponent (typeof(T));
65 
66  return component;
67  }
static T Hydrogen.Components.GetComponentIfNull< T > ( this Component  targetObject,
cachedReference 
)
static

Get a component reference, checking if its already referenced.

Returns
The desired component.
Parameters
targetObjectThe object to look on for the component.
cachedReferencePossible pre-existing reference to component.
Template Parameters
TObject Type.

private AudioSource _localAudioSource = null; public AudioSource LocalAudioSource { get { _localAudioSource = Hydrogen.Components.GetComponentIfNull( this, _localAudioSource ); return _localAudioSource; } }

Type Constraints
T :Component 

Definition at line 87 of file Components.cs.

87  : Component
88  {
89  if (cachedReference == null) {
90  cachedReference = (T)targetObject.GetComponent (typeof(T));
91  if (cachedReference == null) {
92  Debug.LogWarning ("GetComponent of type " + typeof(T) + " failed on " + targetObject.name, targetObject);
93  }
94  }
95 
96  return cachedReference;
97  }
static T Hydrogen.Components.GetComponentIfNull< T > ( this GameObject  targetObject,
cachedReference 
)
static

Get a component reference, checking if its already referenced.

Returns
The desired component.
Parameters
targetObjectThe object to look on for the component.
cachedReferencePossible pre-existing reference to component.
Template Parameters
TObject Type.

private AudioSource _localAudioSource = null; public AudioSource LocalAudioSource { get { _localAudioSource = Hydrogen.Components.GetComponentIfNull( this, _localAudioSource ); return _localAudioSource; } }

Type Constraints
T :Component 

Definition at line 117 of file Components.cs.

117  : Component
118  {
119  if (cachedReference == null) {
120  cachedReference = (T)targetObject.GetComponent (typeof(T));
121  if (cachedReference == null) {
122  Debug.LogWarning ("GetComponent of type " + typeof(T) + " failed on " + targetObject.name, targetObject);
123  }
124  }
125 
126  return cachedReference;
127  }
static T Hydrogen.Components.GetComponentInParents< T > ( this GameObject  targetObject,
cachedReference 
)
static

Get a component reference from a gameObjects parents.

Returns
The desired component.
Parameters
targetObjectThe root object to look on for the component (backwards).
cachedReferencePossible pre-existing reference to component.
Template Parameters
TObject Type.

private AudioSource _localAudioSource = null; public AudioSource LocalAudioSource { get { _localAudioSource = Hydrogen.Components.GetComponentInParents( this, _localAudioSource ); return _localAudioSource; } }

Type Constraints
T :Component 

Definition at line 147 of file Components.cs.

147  : Component
148  {
149  if (cachedReference == null) {
150  Transform p = targetObject.transform.parent;
151 
152  while (p != null) {
153  var t = (T)targetObject.GetComponent (typeof(T));
154 
155  // Return as soon as we find the component
156  if (t != null) {
157  cachedReference = t;
158  return cachedReference;
159  }
160 
161  // Next parent to search
162  p = p.parent;
163  }
164 
165  if (cachedReference == null) {
166  Debug.LogWarning ("GetComponentInParents of type " + typeof(T) + " failed on " + targetObject.name, targetObject);
167  }
168  }
169 
170  return cachedReference;
171  }
static bool Hydrogen.Components.HasComponent< T > ( this GameObject  targetObject,
cachedReference 
)
static

Does the GameObject have the component?

Returns
If the component is added to the GameObject
Parameters
targetObjectThe root object to look on for the component.
cachedReferencePossible pre-existing reference to component.
Template Parameters
TObject Type.

private AudioSource _localAudioSource = null; public void Start() { if (gameObject.HasComponent(_localAudioSource) == false) { _localAudioSource = gameObject.AddComponent<AudioSource>(); } }

Type Constraints
T :Component 

Definition at line 190 of file Components.cs.

190  : Component
191  {
192  return cachedReference != null || targetObject.GetComponent (typeof(T)) != null;
193  }

Member Data Documentation

const string Hydrogen.Components.DefaultSingletonName = "Hydrogen"

The GameObject name used when creating singletons automatically via any of the Hydrogen components.

Definition at line 42 of file Components.cs.