Must have extensions
This commit is contained in:
parent
d7efb3beda
commit
2e24a9d44c
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 17bd7bd0a37b9c3e09a999e7f60b4d8d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7549b0b6b6e85d5cb95ce277994540a0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,15 @@
|
|||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
public static class AsyncOperationExtensions {
|
||||
/// <summary>
|
||||
/// Extension method that converts an AsyncOperation into a Task.
|
||||
/// </summary>
|
||||
/// <param name="asyncOperation">The AsyncOperation to convert.</param>
|
||||
/// <returns>A Task that represents the completion of the AsyncOperation.</returns>
|
||||
public static Task AsTask(this AsyncOperation asyncOperation) {
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
asyncOperation.completed += _ => tcs.SetResult(true);
|
||||
return tcs.Task;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b16a49fe5b0b49ddb2bf53acbe1494d4
|
||||
timeCreated: 1711810252
|
|
@ -0,0 +1,22 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class CameraExtensions {
|
||||
/// <summary>
|
||||
/// Calculates and returns viewport extents with an optional margin. Useful for calculating a frustum for culling.
|
||||
/// </summary>
|
||||
/// <param name="camera">The camera object this method extends.</param>
|
||||
/// <param name="viewportMargin">Optional margin to be applied to viewport extents. Default is 0.2, 0.2.</param>
|
||||
/// <returns>Viewport extents as a Vector2 after applying the margin.</returns>
|
||||
public static Vector2 GetViewportExtentsWithMargin(this Camera camera, Vector2? viewportMargin = null) {
|
||||
Vector2 margin = viewportMargin ?? new Vector2(0.2f, 0.2f);
|
||||
|
||||
Vector2 result;
|
||||
float halfFieldOfView = camera.fieldOfView * 0.5f * Mathf.Deg2Rad;
|
||||
result.y = camera.nearClipPlane * Mathf.Tan(halfFieldOfView);
|
||||
result.x = result.y * camera.aspect + margin.x;
|
||||
result.y += margin.y;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7ce5eef7fd4846f1b6172d1d7f1caa0a
|
||||
timeCreated: 1702609324
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class EnumerableExtensions {
|
||||
/// <summary>
|
||||
/// Performs an action on each element in the sequence.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the sequence.</typeparam>
|
||||
/// <param name="sequence">The sequence to iterate over.</param>
|
||||
/// <param name="action">The action to perform on each element.</param>
|
||||
public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action) {
|
||||
foreach (var item in sequence) {
|
||||
action(item);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cda815a18f7a4788a8089273618b1277
|
||||
timeCreated: 1711809697
|
|
@ -0,0 +1,16 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class EnumeratorExtensions {
|
||||
/// <summary>
|
||||
/// Converts an IEnumerator<T> to an IEnumerable<T>.
|
||||
/// </summary>
|
||||
/// <param name="e">An instance of IEnumerator<T>.</param>
|
||||
/// <returns>An IEnumerable<T> with the same elements as the input instance.</returns>
|
||||
public static IEnumerable<T> ToEnumerable<T>(this IEnumerator<T> e) {
|
||||
while (e.MoveNext()) {
|
||||
yield return e.Current;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 577bd3be68ba4662a8494a32d675700f
|
||||
timeCreated: 1700337433
|
|
@ -0,0 +1,119 @@
|
|||
using UnityEngine;
|
||||
using System.Linq;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class GameObjectExtensions {
|
||||
/// <summary>
|
||||
/// This method is used to hide the GameObject in the Hierarchy view.
|
||||
/// </summary>
|
||||
/// <param name="gameObject"></param>
|
||||
public static void HideInHierarchy(this GameObject gameObject) {
|
||||
gameObject.hideFlags = HideFlags.HideInHierarchy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a component of the given type attached to the GameObject. If that type of component does not exist, it adds one.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method is useful when you don't know if a GameObject has a specific type of component,
|
||||
/// but you want to work with that component regardless. Instead of checking and adding the component manually,
|
||||
/// you can use this method to do both operations in one line.
|
||||
/// </remarks>
|
||||
/// <typeparam name="T">The type of the component to get or add.</typeparam>
|
||||
/// <param name="gameObject">The GameObject to get the component from or add the component to.</param>
|
||||
/// <returns>The existing component of the given type, or a new one if no such component exists.</returns>
|
||||
public static T GetOrAdd<T>(this GameObject gameObject) where T : Component {
|
||||
T component = gameObject.GetComponent<T>();
|
||||
if (!component) component = gameObject.AddComponent<T>();
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the object itself if it exists, null otherwise.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method helps differentiate between a null reference and a destroyed Unity object. Unity's "== null" check
|
||||
/// can incorrectly return true for destroyed objects, leading to misleading behaviour. The OrNull method use
|
||||
/// Unity's "null check", and if the object has been marked for destruction, it ensures an actual null reference is returned,
|
||||
/// aiding in correctly chaining operations and preventing NullReferenceExceptions.
|
||||
/// </remarks>
|
||||
/// <typeparam name="T">The type of the object.</typeparam>
|
||||
/// <param name="obj">The object being checked.</param>
|
||||
/// <returns>The object itself if it exists and not destroyed, null otherwise.</returns>
|
||||
public static T OrNull<T>(this T obj) where T : Object => obj ? obj : null;
|
||||
|
||||
/// <summary>
|
||||
/// Destroys all children of the game object
|
||||
/// </summary>
|
||||
/// <param name="gameObject">GameObject whose children are to be destroyed.</param>
|
||||
public static void DestroyChildren(this GameObject gameObject) {
|
||||
gameObject.transform.DestroyChildren();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Immediately destroys all children of the given GameObject.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">GameObject whose children are to be destroyed.</param>
|
||||
public static void DestroyChildrenImmediate(this GameObject gameObject) {
|
||||
gameObject.transform.DestroyChildrenImmediate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables all child GameObjects associated with the given GameObject.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">GameObject whose child GameObjects are to be enabled.</param>
|
||||
public static void EnableChildren(this GameObject gameObject) {
|
||||
gameObject.transform.EnableChildren();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disables all child GameObjects associated with the given GameObject.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">GameObject whose child GameObjects are to be disabled.</param>
|
||||
public static void DisableChildren(this GameObject gameObject) {
|
||||
gameObject.transform.DisableChildren();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the GameObject's transform's position, rotation, and scale to their default values.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">GameObject whose transformation is to be reset.</param>
|
||||
public static void ResetTransformation(this GameObject gameObject) {
|
||||
gameObject.transform.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the hierarchical path in the Unity scene hierarchy for this GameObject.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">The GameObject to get the path for.</param>
|
||||
/// <returns>A string representing the full hierarchical path of this GameObject in the Unity scene.
|
||||
/// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending
|
||||
/// with the name of the specified GameObjects parent.</returns>
|
||||
public static string Path(this GameObject gameObject) {
|
||||
return "/" + string.Join("/",
|
||||
gameObject.GetComponentsInParent<Transform>().Select(t => t.name).Reverse().ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the full hierarchical path in the Unity scene hierarchy for this GameObject.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">The GameObject to get the path for.</param>
|
||||
/// <returns>A string representing the full hierarchical path of this GameObject in the Unity scene.
|
||||
/// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending
|
||||
/// with the name of the specified GameObject itself.</returns>
|
||||
public static string PathFull(this GameObject gameObject) {
|
||||
return gameObject.Path() + "/" + gameObject.name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively sets the provided layer for this GameObject and all of its descendants in the Unity scene hierarchy.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">The GameObject to set layers for.</param>
|
||||
/// <param name="layer">The layer number to set for GameObject and all of its descendants.</param>
|
||||
public static void SetLayersRecursively(this GameObject gameObject, int layer) {
|
||||
gameObject.layer = layer;
|
||||
gameObject.transform.ForEveryChild(child => child.gameObject.SetLayersRecursively(layer));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6f6df6afd73b477ba52959a419427d8f
|
||||
timeCreated: 1700333454
|
|
@ -0,0 +1,13 @@
|
|||
using UnityEngine;
|
||||
|
||||
public static class LayerMaskExtensions {
|
||||
/// <summary>
|
||||
/// Checks if the given layer number is contained in the LayerMask.
|
||||
/// </summary>
|
||||
/// <param name="mask">The LayerMask to check.</param>
|
||||
/// <param name="layerNumber">The layer number to check if it is contained in the LayerMask.</param>
|
||||
/// <returns>True if the layer number is contained in the LayerMask, otherwise false.</returns>
|
||||
public static bool Contains(this LayerMask mask, int layerNumber) {
|
||||
return mask == (mask | (1 << layerNumber));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7aed4fcf15a146a09b429d985c7b900c
|
||||
timeCreated: 1710728611
|
|
@ -0,0 +1,42 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class ListExtensions {
|
||||
/// <summary>
|
||||
/// Determines whether a collection is null or has no elements
|
||||
/// without having to enumerate the entire collection to get a count.
|
||||
///
|
||||
/// Uses LINQ's Any() method to determine if the collection is empty,
|
||||
/// so there is some GC overhead.
|
||||
/// </summary>
|
||||
/// <param name="list">List to evaluate</param>
|
||||
public static bool IsNullOrEmpty<T>(this IList<T> list) {
|
||||
return list == null || !list.Any();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new list that is a copy of the original list.
|
||||
/// </summary>
|
||||
/// <param name="list">The original list to be copied.</param>
|
||||
/// <returns>A new list that is a copy of the original list.</returns>
|
||||
public static List<T> Clone<T>(this IList<T> list) {
|
||||
List<T> newList = new List<T>();
|
||||
foreach (T item in list) {
|
||||
newList.Add(item);
|
||||
}
|
||||
|
||||
return newList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Swaps two elements in the list at the specified indices.
|
||||
/// </summary>
|
||||
/// <param name="list">The list.</param>
|
||||
/// <param name="indexA">The index of the first element.</param>
|
||||
/// <param name="indexB">The index of the second element.</param>
|
||||
public static void Swap<T>(this IList<T> list, int indexA, int indexB) {
|
||||
(list[indexA], list[indexB]) = (list[indexB], list[indexA]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0b8552bd31f34ac6a67611d44b19c4d4
|
||||
timeCreated: 1668216868
|
|
@ -0,0 +1,99 @@
|
|||
#if ENABLED_UNITY_MATHEMATICS
|
||||
using Unity.Mathematics;
|
||||
#endif
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class MathfExtension {
|
||||
#region Min
|
||||
|
||||
#if ENABLED_UNITY_MATHEMATICS
|
||||
public static half Min(half a, half b) {
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
public static half Min(params half[] values) {
|
||||
int num = values.Length;
|
||||
if (num == 0) {
|
||||
return (half) 0;
|
||||
}
|
||||
|
||||
half num2 = values[0];
|
||||
for (int i = 1; i < num; i++) {
|
||||
if (values[i] < num2) {
|
||||
num2 = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
return num2;
|
||||
}
|
||||
#endif
|
||||
|
||||
public static double Min(double a, double b) {
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
public static double Min(params double[] values) {
|
||||
int num = values.Length;
|
||||
if (num == 0) {
|
||||
return 0f;
|
||||
}
|
||||
|
||||
double num2 = values[0];
|
||||
for (int i = 1; i < num; i++) {
|
||||
if (values[i] < num2) {
|
||||
num2 = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
return num2;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Max
|
||||
|
||||
#if ENABLED_UNITY_MATHEMATICS
|
||||
public static half Max(half a, half b) {
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
public static half Max(params half[] values) {
|
||||
int num = values.Length;
|
||||
if (num == 0) {
|
||||
return (half) 0;
|
||||
}
|
||||
|
||||
half num2 = values[0];
|
||||
for (int i = 1; i < num; i++) {
|
||||
if (values[i] > num2) {
|
||||
num2 = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
return num2;
|
||||
}
|
||||
#endif
|
||||
|
||||
public static double Max(double a, double b) {
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
public static double Max(params double[] values) {
|
||||
int num = values.Length;
|
||||
if (num == 0) {
|
||||
return 0f;
|
||||
}
|
||||
|
||||
double num2 = values[0];
|
||||
for (int i = 1; i < num; i++) {
|
||||
if (values[i] > num2) {
|
||||
num2 = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
return num2;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a6d5ce3e4e8ff4b4b9a09b67865696bf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,27 @@
|
|||
using UnityEngine;
|
||||
#if ENABLED_UNITY_MATHEMATICS
|
||||
using Unity.Mathematics;
|
||||
#endif
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class NumberExtensions {
|
||||
public static float PercentageOf(this int part, int whole) {
|
||||
if (whole == 0) return 0; // Handling division by zero
|
||||
return (float) part / whole;
|
||||
}
|
||||
|
||||
public static int AtLeast(this int value, int min) => Mathf.Max(value, min);
|
||||
public static int AtMost(this int value, int max) => Mathf.Min(value, max);
|
||||
|
||||
#if ENABLED_UNITY_MATHEMATICS
|
||||
public static half AtLeast(this half value, half max) => MathfExtension.Max(value, max);
|
||||
public static half AtMost(this half value, half max) => MathfExtension.Min(value, max);
|
||||
#endif
|
||||
|
||||
public static float AtLeast(this float value, float min) => Mathf.Max(value, min);
|
||||
public static float AtMost(this float value, float max) => Mathf.Min(value, max);
|
||||
|
||||
public static double AtLeast(this double value, double min) => MathfExtension.Max(value, min);
|
||||
public static double AtMost(this double value, double min) => MathfExtension.Min(value, min);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2554ca5ec06e428496d8ba3f0d9b4cca
|
||||
timeCreated: 1703742184
|
|
@ -0,0 +1,33 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class RendererExtensions {
|
||||
/// <summary>
|
||||
/// Enables ZWrite for materials in this Renderer that have a '_Color' property. This will allow the materials
|
||||
/// to write to the Z buffer, which could be used to affect how subsequent rendering is handled,
|
||||
/// for instance, ensuring correct layering of transparent objects.
|
||||
/// </summary>
|
||||
public static void EnableZWrite(this Renderer renderer) {
|
||||
foreach (Material material in renderer.materials) {
|
||||
if (material.HasProperty("_Color")) {
|
||||
material.SetInt("_ZWrite", 1);
|
||||
material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disables ZWrite for materials in this Renderer that have a '_Color' property. This would stop
|
||||
/// the materials from writing to the Z buffer, which may be desirable in some cases to prevent subsequent
|
||||
/// rendering from being occluded, like in rendering of semi-transparent or layered objects.
|
||||
/// </summary>
|
||||
public static void DisableZWrite(this Renderer renderer) {
|
||||
foreach (Material material in renderer.materials) {
|
||||
if (material.HasProperty("_Color")) {
|
||||
material.SetInt("_ZWrite", 0);
|
||||
material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent + 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1f1c942e745649e98f9999a13624f630
|
||||
timeCreated: 1702609333
|
|
@ -0,0 +1,15 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class ResourcesUtils {
|
||||
/// <summary>
|
||||
/// Load volume profile from given path.
|
||||
/// </summary>
|
||||
/// <param name="path">Path from where volume profile should be loaded.</param>
|
||||
public static void LoadVolumeProfile(this Volume volume, string path) {
|
||||
var profile = Resources.Load<VolumeProfile>(path);
|
||||
volume.profile = profile;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fae0394afb914840bc32fd1dda091d79
|
||||
timeCreated: 1702084997
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public static class TaskExtensions {
|
||||
/// <summary>
|
||||
/// Converts the Task into an IEnumerator for Unity coroutine usage.
|
||||
/// </summary>
|
||||
/// <param name="task">The Task to convert.</param>
|
||||
/// <returns>An IEnumerator representation of the Task.</returns>
|
||||
public static IEnumerator AsCoroutine(this Task task) {
|
||||
while (!task.IsCompleted) yield return null;
|
||||
// When used on a faulted Task, GetResult() will propagate the original exception.
|
||||
// see: https://devblogs.microsoft.com/pfxteam/task-exception-handling-in-net-4-5/
|
||||
task.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks a task to be forgotten, meaning any exceptions thrown by the task will be caught and handled.
|
||||
/// </summary>
|
||||
/// <param name="task">The task to be forgotten.</param>
|
||||
/// <param name="onException">The optional action to execute when an exception is caught. If provided, the exception will not be rethrown.</param>
|
||||
public static async void Forget(this Task task, Action<Exception> onException = null) {
|
||||
try {
|
||||
await task;
|
||||
}
|
||||
catch (Exception exception) {
|
||||
if (onException == null)
|
||||
throw exception;
|
||||
|
||||
onException(exception);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 396899c3fd3d4a9389fc6b181a52f274
|
||||
timeCreated: 1711810275
|
|
@ -0,0 +1,86 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class TransformExtensions {
|
||||
/// <summary>
|
||||
/// Retrieves all the children of a given Transform.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method can be used with LINQ to perform operations on all child Transforms. For example,
|
||||
/// you could use it to find all children with a specific tag, to disable all children, etc.
|
||||
/// Transform implements IEnumerable and the GetEnumerator method which returns an IEnumerator of all its children.
|
||||
/// </remarks>
|
||||
/// <param name="parent">The Transform to retrieve children from.</param>
|
||||
/// <returns>An IEnumerable<Transform> containing all the child Transforms of the parent.</returns>
|
||||
public static IEnumerable<Transform> Children(this Transform parent) {
|
||||
foreach (Transform child in parent) {
|
||||
yield return child;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets transform's position, scale and rotation
|
||||
/// </summary>
|
||||
/// <param name="transform">Transform to use</param>
|
||||
public static void Reset(this Transform transform) {
|
||||
transform.position = Vector3.zero;
|
||||
transform.localRotation = Quaternion.identity;
|
||||
transform.localScale = Vector3.one;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destroys all child game objects of the given transform.
|
||||
/// </summary>
|
||||
/// <param name="parent">The Transform whose child game objects are to be destroyed.</param>
|
||||
public static void DestroyChildren(this Transform parent) {
|
||||
parent.ForEveryChild(child => Object.Destroy(child.gameObject));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Immediately destroys all child game objects of the given transform.
|
||||
/// </summary>
|
||||
/// <param name="parent">The Transform whose child game objects are to be immediately destroyed.</param>
|
||||
public static void DestroyChildrenImmediate(this Transform parent) {
|
||||
parent.ForEveryChild(child => Object.DestroyImmediate(child.gameObject));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables all child game objects of the given transform.
|
||||
/// </summary>
|
||||
/// <param name="parent">The Transform whose child game objects are to be enabled.</param>
|
||||
public static void EnableChildren(this Transform parent) {
|
||||
parent.ForEveryChild(child => child.gameObject.SetActive(true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disables all child game objects of the given transform.
|
||||
/// </summary>
|
||||
/// <param name="parent">The Transform whose child game objects are to be disabled.</param>
|
||||
public static void DisableChildren(this Transform parent) {
|
||||
parent.ForEveryChild(child => child.gameObject.SetActive(false));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a specified action for each child of a given transform.
|
||||
/// </summary>
|
||||
/// <param name="parent">The parent transform.</param>
|
||||
/// <param name="action">The action to be performed on each child.</param>
|
||||
/// <remarks>
|
||||
/// This method iterates over all child transforms in reverse order and executes a given action on them.
|
||||
/// The action is a delegate that takes a Transform as parameter.
|
||||
/// </remarks>
|
||||
public static void ForEveryChild(this Transform parent, System.Action<Transform> action) {
|
||||
for (var i = parent.childCount - 1; i >= 0; i--) {
|
||||
action(parent.GetChild(i));
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Renamed to ForEveryChild")]
|
||||
static void PerformActionOnChildren(this Transform parent, System.Action<Transform> action) {
|
||||
parent.ForEveryChild(action);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 24de5cc001ff41af9ea0860884ba9590
|
||||
timeCreated: 1700336364
|
|
@ -0,0 +1,30 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class Vector2Extensions {
|
||||
/// <summary>
|
||||
/// Adds to any x y values of a Vector2
|
||||
/// </summary>
|
||||
public static Vector2 Add(this Vector2 vector2, float x = 0, float y = 0) {
|
||||
return new Vector2(vector2.x + x, vector2.y + y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets any x y values of a Vector2
|
||||
/// </summary>
|
||||
public static Vector2 With(this Vector2 vector2, float? x = null, float? y = null) {
|
||||
return new Vector2(x ?? vector2.x, y ?? vector2.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a Boolean indicating whether the current Vector2 is in a given range from another Vector2
|
||||
/// </summary>
|
||||
/// <param name="current">The current Vector2 position</param>
|
||||
/// <param name="target">The Vector2 position to compare against</param>
|
||||
/// <param name="range">The range value to compare against</param>
|
||||
/// <returns>True if the current Vector2 is in the given range from the target Vector2, false otherwise</returns>
|
||||
public static bool InRangeOf(this Vector2 current, Vector2 target, float range) {
|
||||
return (current - target).sqrMagnitude <= range * range;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1b6f048853fe7a34cab34bfd22a294fe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,54 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace UnityUtils {
|
||||
public static class Vector3Extensions {
|
||||
/// <summary>
|
||||
/// Sets any x y z values of a Vector3
|
||||
/// </summary>
|
||||
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 to any x y z values of a Vector3
|
||||
/// </summary>
|
||||
public static Vector3 Add(this Vector3 vector, float x = 0, float y = 0, float z = 0) {
|
||||
return new Vector3(vector.x + x, vector.y + y, vector.z + z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a Boolean indicating whether the current Vector3 is in a given range from another Vector3
|
||||
/// </summary>
|
||||
/// <param name="current">The current Vector3 position</param>
|
||||
/// <param name="target">The Vector3 position to compare against</param>
|
||||
/// <param name="range">The range value to compare against</param>
|
||||
/// <returns>True if the current Vector3 is in the given range from the target Vector3, false otherwise</returns>
|
||||
public static bool InRangeOf(this Vector3 current, Vector3 target, float range) {
|
||||
return (current - target).sqrMagnitude <= range * range;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Divides two Vector3 objects component-wise.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// For each component in v0 (x, y, z), it is divided by the corresponding component in v1 if the component in v1 is not zero.
|
||||
/// Otherwise, the component in v0 remains unchanged.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// Use 'ComponentDivide' to scale a game object proportionally:
|
||||
/// <code>
|
||||
/// myObject.transform.localScale = originalScale.ComponentDivide(targetDimensions);
|
||||
/// </code>
|
||||
/// This scales the object size to fit within the target dimensions while maintaining its original proportions.
|
||||
///</example>
|
||||
/// <param name="v0">The Vector3 object that this method extends.</param>
|
||||
/// <param name="v1">The Vector3 object by which v0 is divided.</param>
|
||||
/// <returns>A new Vector3 object resulting from the component-wise division.</returns>
|
||||
public static Vector3 ComponentDivide(this Vector3 v0, Vector3 v1){
|
||||
return new Vector3(
|
||||
v1.x != 0 ? v0.x / v1.x : v0.x,
|
||||
v1.y != 0 ? v0.y / v1.y : v0.y,
|
||||
v1.z != 0 ? v0.z / v1.z : v0.z);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2cc35c2a774d4d95bf644b728a750192
|
||||
timeCreated: 1700329942
|
Loading…
Reference in New Issue