Skip to content

Commit

Permalink
Implemented GetComponentOrThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylanters committed Oct 18, 2021
1 parent 3e7ea62 commit 6b94dfe
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Runtime/EntityComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,27 @@ public void SetActive (bool value) =>
/// Destroys the game object of the entity.
public void Destroy () =>
UnityEngine.Object.Destroy (this.gameObject);

/// Gets a component on an enity and sets it's reference to a property.
public void GetComponentToProperty<UnityComponentType> (ref UnityComponentType entityProperty, bool includeChildren = false, bool includeInactive = false) =>
entityProperty = includeChildren == true
? this.GetComponentInChildren<UnityComponentType> (includeInactive)
: this.GetComponent<UnityComponentType> ();

/// <summary>
/// Tries to get a component on the entity and throws an exception if it's
/// not found.
/// </summary>
/// <typeparam name="UnityComponentType">The Component type.</typeparam>
/// <returns>A reference to the Component.</returns>
/// <exception cref="System.Exception"></exception>
private UnityComponentType GetComponentOrThrow<UnityComponentType> () {
var _component = this.GetComponent<UnityComponentType> ();
if (_component != null)
return _component;
throw new System.Exception ($"Unable to get Component of type {typeof (UnityComponentType)} on entity");
}

/// Adds an asset to the entity.
public UnityEngine.Object AddAsset (UnityEngine.Object asset) =>
UnityEngine.Object.Instantiate (asset, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity, this.transform);
Expand Down

0 comments on commit 6b94dfe

Please sign in to comment.