Skip to content

Instantly share code, notes, and snippets.

@ysalihtuncel
Created June 12, 2025 14:22
Show Gist options
  • Save ysalihtuncel/df96c63f8be3cb0a148fecbe80d917a2 to your computer and use it in GitHub Desktop.
Save ysalihtuncel/df96c63f8be3cb0a148fecbe80d917a2 to your computer and use it in GitHub Desktop.
Unity Extensions
using UnityEngine;
public static class BehaviourExtensions
{
/// <summary>
/// Gets the component of type T if it exists, otherwise adds a new component of type T to the Behaviour.
/// </summary>
public static void Enabled(this Behaviour behaviour) => behaviour.enabled = true;
/// <summary>
/// Disables the Behaviour.
/// </summary>
public static void Disabled(this Behaviour behaviour) => behaviour.enabled = false;
}
using UnityEngine;
public static class GameObjectExtensions
{
/// <summary>
/// Gets the component of type T if it exists, otherwise adds a new component of type T to the GameObject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gameObject"></param>
/// <returns>T</returns> <summary>
public static T GetOrAdd<T>(this GameObject gameObject) where T : Component
{
T component = gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>();
return component;
}
/// <summary>
/// Returns the GameObject if it is not null, otherwise returns null.
/// </summary>
public static T OrNull<T>(this T obj) where T : Object => obj ? obj : null;
/// <summary>
/// Destroys all children of the GameObject.
/// </summary>
/// <param name="gameObject"></param> <summary>
public static void DestoryChildren(this GameObject gameObject) => gameObject.transform.DestoryChildren();
public static void Enabled(this GameObject gameObject) => gameObject.SetActive(true);
public static void Disabled(this GameObject gameObject) => gameObject.SetActive(false);
}
using System.Collections.Generic;
using UnityEngine;
public static class TransformExtensions
{
/// <summary>
/// Gets the component of type T if it exists, otherwise adds a new component of type T to the Transform.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="transform"></param>
/// <returns>T</returns>
public static IEnumerable<Transform> Children(this Transform parent)
{
foreach (Transform child in parent)
{
yield return child;
}
}
/// <summary>
/// Gets the component of type T if it exists, otherwise adds a new component of type T to the Transform.
/// </summary>
public static void DestoryChildren(this Transform parent) => parent.PerformActionOnChildren(child => Object.Destroy(child.gameObject));
/// <summary>
/// Enables all children of the Transform.
/// </summary>
public static void EnableChildren(this Transform parent) => parent.PerformActionOnChildren(child => child.gameObject.SetActive(true));
/// <summary>
/// Disables all children of the Transform.
/// </summary>
public static void DisableChildren(this Transform parent) => parent.PerformActionOnChildren(child => child.gameObject.SetActive(false));
static void PerformActionOnChildren(this Transform parent, System.Action<Transform> action)
{
foreach (Transform child in parent.Children())
{
action(child);
}
}
}
using UnityEngine;
public static class Vector3Extensions
{
/// <summary>
/// Creates a new Vector3 with the specified x, y, and z values, using the current values if not provided.
/// </summary>
/// <param name="vector">The original Vector3.</param>
/// <param name="x">Optional new x value.</param>
/// <param name="y">Optional new y value.</param>
/// <param name="z">Optional new z value.</param>
/// <returns>A new Vector3 with the specified or current values.</returns>
public static Vector3 With(this Vector3 vector, float? x = null, float? y = null, float? z = null)
{
return new Vector3(
x ?? vector.x,
y ?? vector.y,
z ?? vector.z
);
}
/// <summary>
/// Adds the specified x, y, and z values to the current Vector3, using 0 if not provided.
/// </summary>
/// <param name="vector">The original Vector3.</param>
/// <param name="x">Optional value to add to the x component.</param>
/// <param name="y">Optional value to add to the y component.</param>
/// <param name="z">Optional value to add to the z component.</param>
/// <returns>A new Vector3 with the added values.</returns>
public static Vector3 Add(this Vector3 vector, float? x = null, float? y = null, float? z = null)
{
return new Vector3(
vector.x + (x ?? 0),
vector.y + (y ?? 0),
vector.z + (z ?? 0)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment