From 2e24a9d44ce6d0d296f00b046db5c0e96a510653 Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Fri, 19 Apr 2024 21:28:35 +0200 Subject: [PATCH] Must have extensions --- Assets/Scripts.meta | 8 ++ Assets/Scripts/Extensions.meta | 8 ++ .../Extensions/AsyncOperationExtensions.cs | 15 +++ .../AsyncOperationExtensions.cs.meta | 3 + Assets/Scripts/Extensions/CameraExtensions.cs | 22 ++++ .../Extensions/CameraExtensions.cs.meta | 3 + .../Extensions/EnumerableExtensions.cs | 16 +++ .../Extensions/EnumerableExtensions.cs.meta | 3 + .../Extensions/EnumeratorExtensions.cs | 16 +++ .../Extensions/EnumeratorExtensions.cs.meta | 3 + .../Extensions/GameObjectExtensions.cs | 119 ++++++++++++++++++ .../Extensions/GameObjectExtensions.cs.meta | 3 + .../Scripts/Extensions/LayerMaskExtensions.cs | 13 ++ .../Extensions/LayerMaskExtensions.cs.meta | 3 + Assets/Scripts/Extensions/ListExtensions.cs | 42 +++++++ .../Scripts/Extensions/ListExtensions.cs.meta | 3 + Assets/Scripts/Extensions/MathfExtension.cs | 99 +++++++++++++++ .../Scripts/Extensions/MathfExtension.cs.meta | 11 ++ Assets/Scripts/Extensions/NumberExtensions.cs | 27 ++++ .../Extensions/NumberExtensions.cs.meta | 3 + .../Scripts/Extensions/RendererExtensions.cs | 33 +++++ .../Extensions/RendererExtensions.cs.meta | 3 + Assets/Scripts/Extensions/ResourcesUtils.cs | 15 +++ .../Scripts/Extensions/ResourcesUtils.cs.meta | 3 + Assets/Scripts/Extensions/TaskExtensions.cs | 34 +++++ .../Scripts/Extensions/TaskExtensions.cs.meta | 3 + .../Scripts/Extensions/TransformExtensions.cs | 86 +++++++++++++ .../Extensions/TransformExtensions.cs.meta | 3 + .../Scripts/Extensions/Vector2Extensions.cs | 30 +++++ .../Extensions/Vector2Extensions.cs.meta | 11 ++ .../Scripts/Extensions/Vector3Extensions.cs | 54 ++++++++ .../Extensions/Vector3Extensions.cs.meta | 3 + 32 files changed, 698 insertions(+) create mode 100644 Assets/Scripts.meta create mode 100644 Assets/Scripts/Extensions.meta create mode 100644 Assets/Scripts/Extensions/AsyncOperationExtensions.cs create mode 100644 Assets/Scripts/Extensions/AsyncOperationExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/CameraExtensions.cs create mode 100644 Assets/Scripts/Extensions/CameraExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/EnumerableExtensions.cs create mode 100644 Assets/Scripts/Extensions/EnumerableExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/EnumeratorExtensions.cs create mode 100644 Assets/Scripts/Extensions/EnumeratorExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/GameObjectExtensions.cs create mode 100644 Assets/Scripts/Extensions/GameObjectExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/LayerMaskExtensions.cs create mode 100644 Assets/Scripts/Extensions/LayerMaskExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/ListExtensions.cs create mode 100644 Assets/Scripts/Extensions/ListExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/MathfExtension.cs create mode 100644 Assets/Scripts/Extensions/MathfExtension.cs.meta create mode 100644 Assets/Scripts/Extensions/NumberExtensions.cs create mode 100644 Assets/Scripts/Extensions/NumberExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/RendererExtensions.cs create mode 100644 Assets/Scripts/Extensions/RendererExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/ResourcesUtils.cs create mode 100644 Assets/Scripts/Extensions/ResourcesUtils.cs.meta create mode 100644 Assets/Scripts/Extensions/TaskExtensions.cs create mode 100644 Assets/Scripts/Extensions/TaskExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/TransformExtensions.cs create mode 100644 Assets/Scripts/Extensions/TransformExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/Vector2Extensions.cs create mode 100644 Assets/Scripts/Extensions/Vector2Extensions.cs.meta create mode 100644 Assets/Scripts/Extensions/Vector3Extensions.cs create mode 100644 Assets/Scripts/Extensions/Vector3Extensions.cs.meta diff --git a/Assets/Scripts.meta b/Assets/Scripts.meta new file mode 100644 index 0000000..e6980cd --- /dev/null +++ b/Assets/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 17bd7bd0a37b9c3e09a999e7f60b4d8d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions.meta b/Assets/Scripts/Extensions.meta new file mode 100644 index 0000000..ce0352d --- /dev/null +++ b/Assets/Scripts/Extensions.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7549b0b6b6e85d5cb95ce277994540a0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions/AsyncOperationExtensions.cs b/Assets/Scripts/Extensions/AsyncOperationExtensions.cs new file mode 100644 index 0000000..9d36eb7 --- /dev/null +++ b/Assets/Scripts/Extensions/AsyncOperationExtensions.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using UnityEngine; + +public static class AsyncOperationExtensions { + /// + /// Extension method that converts an AsyncOperation into a Task. + /// + /// The AsyncOperation to convert. + /// A Task that represents the completion of the AsyncOperation. + public static Task AsTask(this AsyncOperation asyncOperation) { + var tcs = new TaskCompletionSource(); + asyncOperation.completed += _ => tcs.SetResult(true); + return tcs.Task; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/AsyncOperationExtensions.cs.meta b/Assets/Scripts/Extensions/AsyncOperationExtensions.cs.meta new file mode 100644 index 0000000..4681434 --- /dev/null +++ b/Assets/Scripts/Extensions/AsyncOperationExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b16a49fe5b0b49ddb2bf53acbe1494d4 +timeCreated: 1711810252 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/CameraExtensions.cs b/Assets/Scripts/Extensions/CameraExtensions.cs new file mode 100644 index 0000000..4627e38 --- /dev/null +++ b/Assets/Scripts/Extensions/CameraExtensions.cs @@ -0,0 +1,22 @@ +using UnityEngine; + +namespace UnityUtils { + public static class CameraExtensions { + /// + /// Calculates and returns viewport extents with an optional margin. Useful for calculating a frustum for culling. + /// + /// The camera object this method extends. + /// Optional margin to be applied to viewport extents. Default is 0.2, 0.2. + /// Viewport extents as a Vector2 after applying the margin. + 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; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/CameraExtensions.cs.meta b/Assets/Scripts/Extensions/CameraExtensions.cs.meta new file mode 100644 index 0000000..72914e3 --- /dev/null +++ b/Assets/Scripts/Extensions/CameraExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7ce5eef7fd4846f1b6172d1d7f1caa0a +timeCreated: 1702609324 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/EnumerableExtensions.cs b/Assets/Scripts/Extensions/EnumerableExtensions.cs new file mode 100644 index 0000000..4a1bcd0 --- /dev/null +++ b/Assets/Scripts/Extensions/EnumerableExtensions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; + +public static class EnumerableExtensions { + /// + /// Performs an action on each element in the sequence. + /// + /// The type of elements in the sequence. + /// The sequence to iterate over. + /// The action to perform on each element. + public static void ForEach(this IEnumerable sequence, Action action) { + foreach (var item in sequence) { + action(item); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/EnumerableExtensions.cs.meta b/Assets/Scripts/Extensions/EnumerableExtensions.cs.meta new file mode 100644 index 0000000..95364c9 --- /dev/null +++ b/Assets/Scripts/Extensions/EnumerableExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cda815a18f7a4788a8089273618b1277 +timeCreated: 1711809697 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/EnumeratorExtensions.cs b/Assets/Scripts/Extensions/EnumeratorExtensions.cs new file mode 100644 index 0000000..94c453f --- /dev/null +++ b/Assets/Scripts/Extensions/EnumeratorExtensions.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace UnityUtils { + public static class EnumeratorExtensions { + /// + /// Converts an IEnumerator to an IEnumerable. + /// + /// An instance of IEnumerator. + /// An IEnumerable with the same elements as the input instance. + public static IEnumerable ToEnumerable(this IEnumerator e) { + while (e.MoveNext()) { + yield return e.Current; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/EnumeratorExtensions.cs.meta b/Assets/Scripts/Extensions/EnumeratorExtensions.cs.meta new file mode 100644 index 0000000..fbcd1ea --- /dev/null +++ b/Assets/Scripts/Extensions/EnumeratorExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 577bd3be68ba4662a8494a32d675700f +timeCreated: 1700337433 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/GameObjectExtensions.cs b/Assets/Scripts/Extensions/GameObjectExtensions.cs new file mode 100644 index 0000000..a4a068b --- /dev/null +++ b/Assets/Scripts/Extensions/GameObjectExtensions.cs @@ -0,0 +1,119 @@ +using UnityEngine; +using System.Linq; + +namespace UnityUtils { + public static class GameObjectExtensions { + /// + /// This method is used to hide the GameObject in the Hierarchy view. + /// + /// + public static void HideInHierarchy(this GameObject gameObject) { + gameObject.hideFlags = HideFlags.HideInHierarchy; + } + + /// + /// Gets a component of the given type attached to the GameObject. If that type of component does not exist, it adds one. + /// + /// + /// 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. + /// + /// The type of the component to get or add. + /// The GameObject to get the component from or add the component to. + /// The existing component of the given type, or a new one if no such component exists. + public static T GetOrAdd(this GameObject gameObject) where T : Component { + T component = gameObject.GetComponent(); + if (!component) component = gameObject.AddComponent(); + + return component; + } + + /// + /// Returns the object itself if it exists, null otherwise. + /// + /// + /// 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. + /// + /// The type of the object. + /// The object being checked. + /// The object itself if it exists and not destroyed, null otherwise. + public static T OrNull(this T obj) where T : Object => obj ? obj : null; + + /// + /// Destroys all children of the game object + /// + /// GameObject whose children are to be destroyed. + public static void DestroyChildren(this GameObject gameObject) { + gameObject.transform.DestroyChildren(); + } + + /// + /// Immediately destroys all children of the given GameObject. + /// + /// GameObject whose children are to be destroyed. + public static void DestroyChildrenImmediate(this GameObject gameObject) { + gameObject.transform.DestroyChildrenImmediate(); + } + + /// + /// Enables all child GameObjects associated with the given GameObject. + /// + /// GameObject whose child GameObjects are to be enabled. + public static void EnableChildren(this GameObject gameObject) { + gameObject.transform.EnableChildren(); + } + + /// + /// Disables all child GameObjects associated with the given GameObject. + /// + /// GameObject whose child GameObjects are to be disabled. + public static void DisableChildren(this GameObject gameObject) { + gameObject.transform.DisableChildren(); + } + + /// + /// Resets the GameObject's transform's position, rotation, and scale to their default values. + /// + /// GameObject whose transformation is to be reset. + public static void ResetTransformation(this GameObject gameObject) { + gameObject.transform.Reset(); + } + + /// + /// Returns the hierarchical path in the Unity scene hierarchy for this GameObject. + /// + /// The GameObject to get the path for. + /// 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. + public static string Path(this GameObject gameObject) { + return "/" + string.Join("/", + gameObject.GetComponentsInParent().Select(t => t.name).Reverse().ToArray()); + } + + /// + /// Returns the full hierarchical path in the Unity scene hierarchy for this GameObject. + /// + /// The GameObject to get the path for. + /// 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. + public static string PathFull(this GameObject gameObject) { + return gameObject.Path() + "/" + gameObject.name; + } + + /// + /// Recursively sets the provided layer for this GameObject and all of its descendants in the Unity scene hierarchy. + /// + /// The GameObject to set layers for. + /// The layer number to set for GameObject and all of its descendants. + public static void SetLayersRecursively(this GameObject gameObject, int layer) { + gameObject.layer = layer; + gameObject.transform.ForEveryChild(child => child.gameObject.SetLayersRecursively(layer)); + } + } +} diff --git a/Assets/Scripts/Extensions/GameObjectExtensions.cs.meta b/Assets/Scripts/Extensions/GameObjectExtensions.cs.meta new file mode 100644 index 0000000..73333a9 --- /dev/null +++ b/Assets/Scripts/Extensions/GameObjectExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6f6df6afd73b477ba52959a419427d8f +timeCreated: 1700333454 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/LayerMaskExtensions.cs b/Assets/Scripts/Extensions/LayerMaskExtensions.cs new file mode 100644 index 0000000..799e093 --- /dev/null +++ b/Assets/Scripts/Extensions/LayerMaskExtensions.cs @@ -0,0 +1,13 @@ +using UnityEngine; + +public static class LayerMaskExtensions { + /// + /// Checks if the given layer number is contained in the LayerMask. + /// + /// The LayerMask to check. + /// The layer number to check if it is contained in the LayerMask. + /// True if the layer number is contained in the LayerMask, otherwise false. + public static bool Contains(this LayerMask mask, int layerNumber) { + return mask == (mask | (1 << layerNumber)); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/LayerMaskExtensions.cs.meta b/Assets/Scripts/Extensions/LayerMaskExtensions.cs.meta new file mode 100644 index 0000000..5e633ae --- /dev/null +++ b/Assets/Scripts/Extensions/LayerMaskExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7aed4fcf15a146a09b429d985c7b900c +timeCreated: 1710728611 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/ListExtensions.cs b/Assets/Scripts/Extensions/ListExtensions.cs new file mode 100644 index 0000000..99d23e7 --- /dev/null +++ b/Assets/Scripts/Extensions/ListExtensions.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using System.Linq; + +namespace UnityUtils { + public static class ListExtensions { + /// + /// 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. + /// + /// List to evaluate + public static bool IsNullOrEmpty(this IList list) { + return list == null || !list.Any(); + } + + /// + /// Creates a new list that is a copy of the original list. + /// + /// The original list to be copied. + /// A new list that is a copy of the original list. + public static List Clone(this IList list) { + List newList = new List(); + foreach (T item in list) { + newList.Add(item); + } + + return newList; + } + + /// + /// Swaps two elements in the list at the specified indices. + /// + /// The list. + /// The index of the first element. + /// The index of the second element. + public static void Swap(this IList list, int indexA, int indexB) { + (list[indexA], list[indexB]) = (list[indexB], list[indexA]); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/ListExtensions.cs.meta b/Assets/Scripts/Extensions/ListExtensions.cs.meta new file mode 100644 index 0000000..44872c2 --- /dev/null +++ b/Assets/Scripts/Extensions/ListExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0b8552bd31f34ac6a67611d44b19c4d4 +timeCreated: 1668216868 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/MathfExtension.cs b/Assets/Scripts/Extensions/MathfExtension.cs new file mode 100644 index 0000000..33db1eb --- /dev/null +++ b/Assets/Scripts/Extensions/MathfExtension.cs @@ -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 + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/MathfExtension.cs.meta b/Assets/Scripts/Extensions/MathfExtension.cs.meta new file mode 100644 index 0000000..cc23886 --- /dev/null +++ b/Assets/Scripts/Extensions/MathfExtension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a6d5ce3e4e8ff4b4b9a09b67865696bf +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions/NumberExtensions.cs b/Assets/Scripts/Extensions/NumberExtensions.cs new file mode 100644 index 0000000..c043780 --- /dev/null +++ b/Assets/Scripts/Extensions/NumberExtensions.cs @@ -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); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/NumberExtensions.cs.meta b/Assets/Scripts/Extensions/NumberExtensions.cs.meta new file mode 100644 index 0000000..bc9a935 --- /dev/null +++ b/Assets/Scripts/Extensions/NumberExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2554ca5ec06e428496d8ba3f0d9b4cca +timeCreated: 1703742184 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/RendererExtensions.cs b/Assets/Scripts/Extensions/RendererExtensions.cs new file mode 100644 index 0000000..e9e12bd --- /dev/null +++ b/Assets/Scripts/Extensions/RendererExtensions.cs @@ -0,0 +1,33 @@ +using UnityEngine; + +namespace UnityUtils { + public static class RendererExtensions { + /// + /// 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. + /// + 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; + } + } + } + + /// + /// 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. + /// + 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; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/RendererExtensions.cs.meta b/Assets/Scripts/Extensions/RendererExtensions.cs.meta new file mode 100644 index 0000000..17bcaef --- /dev/null +++ b/Assets/Scripts/Extensions/RendererExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1f1c942e745649e98f9999a13624f630 +timeCreated: 1702609333 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/ResourcesUtils.cs b/Assets/Scripts/Extensions/ResourcesUtils.cs new file mode 100644 index 0000000..ba34ae4 --- /dev/null +++ b/Assets/Scripts/Extensions/ResourcesUtils.cs @@ -0,0 +1,15 @@ +using UnityEngine; +using UnityEngine.Rendering; + +namespace UnityUtils { + public static class ResourcesUtils { + /// + /// Load volume profile from given path. + /// + /// Path from where volume profile should be loaded. + public static void LoadVolumeProfile(this Volume volume, string path) { + var profile = Resources.Load(path); + volume.profile = profile; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/ResourcesUtils.cs.meta b/Assets/Scripts/Extensions/ResourcesUtils.cs.meta new file mode 100644 index 0000000..1461046 --- /dev/null +++ b/Assets/Scripts/Extensions/ResourcesUtils.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fae0394afb914840bc32fd1dda091d79 +timeCreated: 1702084997 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/TaskExtensions.cs b/Assets/Scripts/Extensions/TaskExtensions.cs new file mode 100644 index 0000000..60fa08d --- /dev/null +++ b/Assets/Scripts/Extensions/TaskExtensions.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections; +using System.Threading.Tasks; + +public static class TaskExtensions { + /// + /// Converts the Task into an IEnumerator for Unity coroutine usage. + /// + /// The Task to convert. + /// An IEnumerator representation of the Task. + 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(); + } + + /// + /// Marks a task to be forgotten, meaning any exceptions thrown by the task will be caught and handled. + /// + /// The task to be forgotten. + /// The optional action to execute when an exception is caught. If provided, the exception will not be rethrown. + public static async void Forget(this Task task, Action onException = null) { + try { + await task; + } + catch (Exception exception) { + if (onException == null) + throw exception; + + onException(exception); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/TaskExtensions.cs.meta b/Assets/Scripts/Extensions/TaskExtensions.cs.meta new file mode 100644 index 0000000..c351bec --- /dev/null +++ b/Assets/Scripts/Extensions/TaskExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 396899c3fd3d4a9389fc6b181a52f274 +timeCreated: 1711810275 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/TransformExtensions.cs b/Assets/Scripts/Extensions/TransformExtensions.cs new file mode 100644 index 0000000..27617d3 --- /dev/null +++ b/Assets/Scripts/Extensions/TransformExtensions.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace UnityUtils { + public static class TransformExtensions { + /// + /// Retrieves all the children of a given Transform. + /// + /// + /// 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. + /// + /// The Transform to retrieve children from. + /// An IEnumerable<Transform> containing all the child Transforms of the parent. + public static IEnumerable Children(this Transform parent) { + foreach (Transform child in parent) { + yield return child; + } + } + + /// + /// Resets transform's position, scale and rotation + /// + /// Transform to use + public static void Reset(this Transform transform) { + transform.position = Vector3.zero; + transform.localRotation = Quaternion.identity; + transform.localScale = Vector3.one; + } + + /// + /// Destroys all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be destroyed. + public static void DestroyChildren(this Transform parent) { + parent.ForEveryChild(child => Object.Destroy(child.gameObject)); + } + + /// + /// Immediately destroys all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be immediately destroyed. + public static void DestroyChildrenImmediate(this Transform parent) { + parent.ForEveryChild(child => Object.DestroyImmediate(child.gameObject)); + } + + /// + /// Enables all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be enabled. + public static void EnableChildren(this Transform parent) { + parent.ForEveryChild(child => child.gameObject.SetActive(true)); + } + + /// + /// Disables all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be disabled. + public static void DisableChildren(this Transform parent) { + parent.ForEveryChild(child => child.gameObject.SetActive(false)); + } + + /// + /// Executes a specified action for each child of a given transform. + /// + /// The parent transform. + /// The action to be performed on each child. + /// + /// 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. + /// + public static void ForEveryChild(this Transform parent, System.Action 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 action) { + parent.ForEveryChild(action); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/TransformExtensions.cs.meta b/Assets/Scripts/Extensions/TransformExtensions.cs.meta new file mode 100644 index 0000000..78b2216 --- /dev/null +++ b/Assets/Scripts/Extensions/TransformExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 24de5cc001ff41af9ea0860884ba9590 +timeCreated: 1700336364 \ No newline at end of file diff --git a/Assets/Scripts/Extensions/Vector2Extensions.cs b/Assets/Scripts/Extensions/Vector2Extensions.cs new file mode 100644 index 0000000..ac1a551 --- /dev/null +++ b/Assets/Scripts/Extensions/Vector2Extensions.cs @@ -0,0 +1,30 @@ +using UnityEngine; + +namespace UnityUtils { + public static class Vector2Extensions { + /// + /// Adds to any x y values of a Vector2 + /// + public static Vector2 Add(this Vector2 vector2, float x = 0, float y = 0) { + return new Vector2(vector2.x + x, vector2.y + y); + } + + /// + /// Sets any x y values of a Vector2 + /// + public static Vector2 With(this Vector2 vector2, float? x = null, float? y = null) { + return new Vector2(x ?? vector2.x, y ?? vector2.y); + } + + /// + /// Returns a Boolean indicating whether the current Vector2 is in a given range from another Vector2 + /// + /// The current Vector2 position + /// The Vector2 position to compare against + /// The range value to compare against + /// True if the current Vector2 is in the given range from the target Vector2, false otherwise + public static bool InRangeOf(this Vector2 current, Vector2 target, float range) { + return (current - target).sqrMagnitude <= range * range; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/Vector2Extensions.cs.meta b/Assets/Scripts/Extensions/Vector2Extensions.cs.meta new file mode 100644 index 0000000..216862f --- /dev/null +++ b/Assets/Scripts/Extensions/Vector2Extensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1b6f048853fe7a34cab34bfd22a294fe +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions/Vector3Extensions.cs b/Assets/Scripts/Extensions/Vector3Extensions.cs new file mode 100644 index 0000000..a6dcba5 --- /dev/null +++ b/Assets/Scripts/Extensions/Vector3Extensions.cs @@ -0,0 +1,54 @@ +using UnityEngine; + +namespace UnityUtils { + public static class Vector3Extensions { + /// + /// Sets any x y z values of a Vector3 + /// + 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); + } + + /// + /// Adds to any x y z values of a Vector3 + /// + 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); + } + + /// + /// Returns a Boolean indicating whether the current Vector3 is in a given range from another Vector3 + /// + /// The current Vector3 position + /// The Vector3 position to compare against + /// The range value to compare against + /// True if the current Vector3 is in the given range from the target Vector3, false otherwise + public static bool InRangeOf(this Vector3 current, Vector3 target, float range) { + return (current - target).sqrMagnitude <= range * range; + } + + /// + /// Divides two Vector3 objects component-wise. + /// + /// + /// 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. + /// + /// + /// Use 'ComponentDivide' to scale a game object proportionally: + /// + /// myObject.transform.localScale = originalScale.ComponentDivide(targetDimensions); + /// + /// This scales the object size to fit within the target dimensions while maintaining its original proportions. + /// + /// The Vector3 object that this method extends. + /// The Vector3 object by which v0 is divided. + /// A new Vector3 object resulting from the component-wise division. + 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); + } + } +} diff --git a/Assets/Scripts/Extensions/Vector3Extensions.cs.meta b/Assets/Scripts/Extensions/Vector3Extensions.cs.meta new file mode 100644 index 0000000..3d0c172 --- /dev/null +++ b/Assets/Scripts/Extensions/Vector3Extensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2cc35c2a774d4d95bf644b728a750192 +timeCreated: 1700329942 \ No newline at end of file