Skip to content

Instantly share code, notes, and snippets.

@AsyncOperator
Last active July 15, 2022 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsyncOperator/06bd5f17c40f127e6901d27f87550bd9 to your computer and use it in GitHub Desktop.
Save AsyncOperator/06bd5f17c40f127e6901d27f87550bd9 to your computer and use it in GitHub Desktop.
Extension methods for Unity game development
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public static class ExtensionMethods
{
#region Transform Extensions
public static void ResetTransformation(this Transform original)
{
original.position = Vector3.zero;
original.localRotation = Quaternion.identity;
original.localScale = Vector3.one;
}
public static void DestroyChildren(this Transform original)
{
foreach (Transform child in original)
{
UnityEngine.Object.Destroy(child.gameObject);
}
}
/// <summary>
/// Won't look up or down
/// </summary>
/// <param name="transform"></param>
/// <param name="point"></param>
public static void LookAtXZ(this Transform transform, Vector3 point)
{
Vector3 direction = DirectionTo(transform.position, point);
direction.y = 0;
transform.rotation = Quaternion.LookRotation(direction);
}
#endregion
#region RectTransform Extensions
public static Vector2 WorldPositionOfRectTransform(this RectTransform original, Camera camera)
{
RectTransformUtility.ScreenPointToWorldPointInRectangle(original, original.position, camera, out var result);
return result;
}
#endregion
#region Vector3 Extensions
public static Vector3 With(this Vector3 original, float? x = null, float? y = null, float? z = null)
{
return new Vector3(x ?? original.x, y ?? original.y, z ?? original.z);
}
public static Vector3 Flattened(this Vector3 vector3)
{
return new Vector3(vector3.x, 0f, vector3.z);
}
/// <summary>
/// Returns the normalized vector from source to destination
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
/// <returns></returns>
public static Vector3 DirectionTo(this Vector3 source, Vector3 destination)
{
return Vector3.Normalize(destination - source);
}
#endregion
#region Float Extensions
public static float LinearRemap(this float value, float valueRangeMin, float valueRangeMax, float newRangeMin, float newRangeMax)
{
float newValue = (value - valueRangeMin) / (valueRangeMax - valueRangeMin) * (newRangeMax - newRangeMin) + newRangeMin;
return newValue;
}
#endregion
#region Color Extensions
public static string ToHex(this Color color)
{
return "#" + ColorUtility.ToHtmlStringRGBA(color);
}
#endregion
#region Button Extensions
public static void RegisterCallback(this Button button, Action callbackAction)
{
button.onClick.AddListener(() => callbackAction());
}
#endregion
#region IList Extensions
/// <summary>
/// Shuffle the list in place using the Fisher-Yates method.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
public static void Shuffle<T>(this IList<T> list)
{
System.Random random = new System.Random();
int listCount = list.Count;
while (listCount > 1)
{
listCount--;
int k = random.Next(listCount + 1);
T value = list[k];
list[k] = list[listCount];
list[listCount] = value;
}
}
/// <summary>
/// Return a random item from the list.
/// Sampling with replacement.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static T RandomItem<T>(this IList<T> list)
{
if (list.Count == 0) throw new System.IndexOutOfRangeException("Cannot select a random item from an empty list");
return list[UnityEngine.Random.Range(0, list.Count)];
}
/// <summary>
/// Removes a random item from the list, returning that item.
/// Sampling without replacement.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static T RemoveRandom<T>(this IList<T> list)
{
if (list.Count == 0) throw new System.IndexOutOfRangeException("Cannot remove a random item from an empty list");
int index = UnityEngine.Random.Range(0, list.Count);
T item = list[index];
list.RemoveAt(index);
return item;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment