Skip to content

Instantly share code, notes, and snippets.

@aleverdes
Last active October 6, 2021 10:15
Show Gist options
  • Save aleverdes/0a1770948c77d48c90da8f1858831b0f to your computer and use it in GitHub Desktop.
Save aleverdes/0a1770948c77d48c90da8f1858831b0f to your computer and use it in GitHub Desktop.
A set of extension methods for declaring, getting, and checking for the existence of components in a single step.
using UnityEngine;
public static class TryGetComponentExtensions
{
#region GameObject Extensions
#if !UNITY_2019_1_OR_NEWER
public static bool TryGetComponent<T>(this GameObject gameObject, out T component) where T : Component
{
component = gameObject.GetComponent<T>();
return component;
}
#endif
public static bool TryGetComponentInChildren<T>(this GameObject gameObject, out T component) where T : Component
{
component = gameObject.GetComponentInChildren<T>();
return component;
}
public static bool TryGetComponentInChildren<T>(this GameObject gameObject, bool includeInactive, out T component) where T : Component
{
component = gameObject.GetComponentInChildren<T>(includeInactive);
return component;
}
public static bool TryGetComponentInParent<T>(this GameObject gameObject, out T component) where T : Component
{
component = gameObject.GetComponentInParent<T>();
return component;
}
#endregion
#region Component Extensions
#if !UNITY_2019_1_OR_NEWER
public static bool TryGetComponent<T>(this Component source, out T component) where T : Component
{
component = source.GetComponent<T>();
return component;
}
#endif
public static bool TryGetComponentInChildren<T>(this Component source, out T component) where T : Component
{
component = source.GetComponentInChildren<T>();
return component;
}
public static bool TryGetComponentInChildren<T>(this Component source, bool includeInactive, out T component) where T : Component
{
component = source.GetComponentInChildren<T>(includeInactive);
return component;
}
public static bool TryGetComponentInParent<T>(this Component source, out T component) where T : Component
{
component = source.GetComponentInParent<T>();
return component;
}
#endregion
#region Transform Extensions
#if !UNITY_2019_1_OR_NEWER
public static bool TryGetComponent<T>(this Transform transform, out T component) where T : Component
{
component = transform.GetComponent<T>();
return component;
}
#endif
public static bool TryGetComponentInChildren<T>(this Transform transform, out T component) where T : Component
{
component = transform.GetComponentInChildren<T>();
return component;
}
public static bool TryGetComponentInChildren<T>(this Transform transform, bool includeInactive, out T component) where T : Component
{
component = transform.GetComponentInChildren<T>(includeInactive);
return component;
}
public static bool TryGetComponentInParent<T>(this Transform transform, out T component) where T : Component
{
component = transform.GetComponentInParent<T>();
return component;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment