Insanely huge initial commit

This commit is contained in:
2026-02-21 16:40:15 -08:00
parent 2ba1c94b88
commit ee9aee0a1b
33825 changed files with 5213498 additions and 0 deletions

View File

@@ -0,0 +1,343 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Animator extensions
/// </summary>
public static class MMAnimatorExtensions
{
/// <summary>
/// Determines if an animator contains a certain parameter, based on a type and a name
/// </summary>
/// <returns><c>true</c> if has parameter of type the specified self name type; otherwise, <c>false</c>.</returns>
/// <param name="self">Self.</param>
/// <param name="name">Name.</param>
/// <param name="type">Type.</param>
public static bool MMHasParameterOfType(this Animator self, string name, AnimatorControllerParameterType type)
{
if (string.IsNullOrEmpty(name)) { return false; }
AnimatorControllerParameter[] parameters = self.parameters;
foreach (AnimatorControllerParameter currParam in parameters)
{
if (currParam.type == type && currParam.name == name)
{
return true;
}
}
return false;
}
/// <summary>
/// Adds an animator parameter name to a parameter list if that parameter exists.
/// </summary>
/// <param name="animator"></param>
/// <param name="parameterName"></param>
/// <param name="parameter"></param>
/// <param name="type"></param>
/// <param name="parameterList"></param>
public static void AddAnimatorParameterIfExists(Animator animator, string parameterName, out int parameter, AnimatorControllerParameterType type, HashSet<int> parameterList)
{
if (string.IsNullOrEmpty(parameterName))
{
parameter = -1;
return;
}
parameter = Animator.StringToHash(parameterName);
if (animator.MMHasParameterOfType(parameterName, type))
{
parameterList.Add(parameter);
}
}
/// <summary>
/// Adds an animator parameter name to a parameter list if that parameter exists.
/// </summary>
/// <param name="animator"></param>
/// <param name="parameterName"></param>
/// <param name="type"></param>
/// <param name="parameterList"></param>
public static void AddAnimatorParameterIfExists(Animator animator, string parameterName, AnimatorControllerParameterType type, HashSet<string> parameterList)
{
if (animator.MMHasParameterOfType(parameterName, type))
{
parameterList.Add(parameterName);
}
}
// SIMPLE METHODS -------------------------------------------------------------------------------------------------------------------------------------------------------------
#region SimpleMethods
// <summary>
/// Updates the animator bool.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">If set to <c>true</c> value.</param>
public static void UpdateAnimatorBool(Animator animator, string parameterName, bool value)
{
animator.SetBool(parameterName, value);
}
/// <summary>
/// Updates the animator integer.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameter">Parameter name.</param>
/// <param name="value">Value.</param>
public static void UpdateAnimatorInteger(Animator animator, string parameterName, int value)
{
animator.SetInteger(parameterName, value);
}
/// <summary>
/// Updates the animator's float
/// </summary>
/// <param name="animator"></param>
/// <param name="parameterName"></param>
/// <param name="value"></param>
public static void UpdateAnimatorFloat(Animator animator, string parameterName, float value, bool performSanityCheck = true)
{
animator.SetFloat(parameterName, value);
}
#endregion
// INT PARAMETER METHODS -------------------------------------------------------------------------------------------------------------------------------------------------------------
// <summary>
/// Updates the animator bool.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">If set to <c>true</c> value.</param>
public static bool UpdateAnimatorBool(Animator animator, int parameter, bool value, HashSet<int> parameterList, bool performSanityCheck = true)
{
if (performSanityCheck && !parameterList.Contains(parameter))
{
return false;
}
animator.SetBool(parameter, value);
return true;
}
/// <summary>
/// Sets an animator's trigger of the int parameter specified
/// </summary>
/// <param name="animator"></param>
/// <param name="parameter"></param>
/// <param name="parameterList"></param>
public static bool UpdateAnimatorTrigger(Animator animator, int parameter, HashSet<int> parameterList, bool performSanityCheck = true)
{
if (performSanityCheck && !parameterList.Contains(parameter))
{
return false;
}
animator.SetTrigger(parameter);
return true;
}
/// <summary>
/// Triggers an animator trigger.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameter">Parameter name.</param>
/// <param name="value">If set to <c>true</c> value.</param>
public static bool SetAnimatorTrigger(Animator animator, int parameter, HashSet<int> parameterList, bool performSanityCheck = true)
{
if (performSanityCheck && !parameterList.Contains(parameter))
{
return false;
}
animator.SetTrigger(parameter);
return true;
}
/// <summary>
/// Updates the animator float.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameter">Parameter name.</param>
/// <param name="value">Value.</param>
public static bool UpdateAnimatorFloat(Animator animator, int parameter, float value, HashSet<int> parameterList, bool performSanityCheck = true)
{
if (performSanityCheck && !parameterList.Contains(parameter))
{
return false;
}
animator.SetFloat(parameter, value);
return true;
}
/// <summary>
/// Updates the animator integer.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameter">Parameter name.</param>
/// <param name="value">Value.</param>
public static bool UpdateAnimatorInteger(Animator animator, int parameter, int value, HashSet<int> parameterList, bool performSanityCheck = true)
{
if (performSanityCheck && !parameterList.Contains(parameter))
{
return false;
}
animator.SetInteger(parameter, value);
return true;
}
// STRING PARAMETER METHODS -------------------------------------------------------------------------------------------------------------------------------------------------------------
#region StringParameterMethods
// <summary>
/// Updates the animator bool.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">If set to <c>true</c> value.</param>
public static void UpdateAnimatorBool(Animator animator, string parameterName, bool value, HashSet<string> parameterList, bool performSanityCheck = true)
{
if (parameterList.Contains(parameterName))
{
animator.SetBool(parameterName, value);
}
}
/// <summary>
/// Sets an animator's trigger of the string parameter name specified
/// </summary>
/// <param name="animator"></param>
/// <param name="parameterName"></param>
/// <param name="parameterList"></param>
public static void UpdateAnimatorTrigger(Animator animator, string parameterName, HashSet<string> parameterList, bool performSanityCheck = true)
{
if (parameterList.Contains(parameterName))
{
animator.SetTrigger(parameterName);
}
}
/// <summary>
/// Triggers an animator trigger.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">If set to <c>true</c> value.</param>
public static void SetAnimatorTrigger(Animator animator, string parameterName, HashSet<string> parameterList, bool performSanityCheck = true)
{
if (parameterList.Contains(parameterName))
{
animator.SetTrigger(parameterName);
}
}
/// <summary>
/// Updates the animator float.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">Value.</param>
public static void UpdateAnimatorFloat(Animator animator, string parameterName, float value, HashSet<string> parameterList, bool performSanityCheck = true)
{
if (parameterList.Contains(parameterName))
{
animator.SetFloat(parameterName, value);
}
}
/// <summary>
/// Updates the animator integer.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">Value.</param>
public static void UpdateAnimatorInteger(Animator animator, string parameterName, int value, HashSet<string> parameterList, bool performSanityCheck = true)
{
if (parameterList.Contains(parameterName))
{
animator.SetInteger(parameterName, value);
}
}
// <summary>
/// Updates the animator bool after checking the parameter's existence.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">If set to <c>true</c> value.</param>
public static void UpdateAnimatorBoolIfExists(Animator animator, string parameterName, bool value, bool performSanityCheck = true)
{
if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Bool))
{
animator.SetBool(parameterName, value);
}
}
/// <summary>
/// Updates an animator trigger if it exists
/// </summary>
/// <param name="animator"></param>
/// <param name="parameterName"></param>
public static void UpdateAnimatorTriggerIfExists(Animator animator, string parameterName, bool performSanityCheck = true)
{
if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger))
{
animator.SetTrigger(parameterName);
}
}
/// <summary>
/// Triggers an animator trigger after checking for the parameter's existence.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">If set to <c>true</c> value.</param>
public static void SetAnimatorTriggerIfExists(Animator animator, string parameterName, bool performSanityCheck = true)
{
if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger))
{
animator.SetTrigger(parameterName);
}
}
/// <summary>
/// Updates the animator float after checking for the parameter's existence.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">Value.</param>
public static void UpdateAnimatorFloatIfExists(Animator animator, string parameterName, float value, bool performSanityCheck = true)
{
if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Float))
{
animator.SetFloat(parameterName, value);
}
}
/// <summary>
/// Updates the animator integer after checking for the parameter's existence.
/// </summary>
/// <param name="animator">Animator.</param>
/// <param name="parameterName">Parameter name.</param>
/// <param name="value">Value.</param>
public static void UpdateAnimatorIntegerIfExists(Animator animator, string parameterName, int value, bool performSanityCheck = true)
{
if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Int))
{
animator.SetInteger(parameterName, value);
}
}
#endregion
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fbe34950474788247ae05de95fa199f3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MoreMountains.Tools
{
/// <summary>
/// Array extensions
/// </summary>
public static class MMArrayExtensions
{
/// <summary>
/// Returns a random value inside the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static T MMRandomValue<T>(this T[] array)
{
int newIndex = Random.Range(0, array.Length);
return array[newIndex];
}
/// <summary>
/// Shuffles an array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static T[] MMShuffle<T>(this T[] array)
{
// Fisher Yates shuffle algorithm, see https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for (int t = 0; t < array.Length; t++)
{
T tmp = array[t];
int randomIndex = Random.Range(t, array.Length);
array[t] = array[randomIndex];
array[randomIndex] = tmp;
}
return array;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a6e5aa704bd68fb42a198fccd658dbfb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,111 @@
using UnityEngine;
using System.Collections;
namespace MoreMountains.Tools
{
/// <summary>
/// Bounds helpers
/// </summary>
public class MMBoundsExtensions : MonoBehaviour
{
/// <summary>
/// Returns a random point within the bounds set as parameter
/// </summary>
/// <param name="bounds"></param>
/// <returns></returns>
public static Vector3 MMRandomPointInBounds(Bounds bounds)
{
return new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
Random.Range(bounds.min.y, bounds.max.y),
Random.Range(bounds.min.z, bounds.max.z)
);
}
/// <summary>
/// Gets collider bounds for an object (from Collider2D)
/// </summary>
/// <param name="theObject"></param>
/// <returns></returns>
public static Bounds GetColliderBounds(GameObject theObject)
{
Bounds returnBounds;
// if the object has a collider at root level, we base our calculations on that
if (theObject.GetComponent<Collider>()!=null)
{
returnBounds = theObject.GetComponent<Collider>().bounds;
return returnBounds;
}
// if the object has a collider2D at root level, we base our calculations on that
if (theObject.GetComponent<Collider2D>()!=null)
{
returnBounds = theObject.GetComponent<Collider2D>().bounds;
return returnBounds;
}
// if the object contains at least one Collider we'll add all its children's Colliders bounds
if (theObject.GetComponentInChildren<Collider>()!=null)
{
Bounds totalBounds = theObject.GetComponentInChildren<Collider>().bounds;
Collider[] colliders = theObject.GetComponentsInChildren<Collider>();
foreach (Collider col in colliders)
{
totalBounds.Encapsulate(col.bounds);
}
returnBounds = totalBounds;
return returnBounds;
}
// if the object contains at least one Collider2D we'll add all its children's Collider2Ds bounds
if (theObject.GetComponentInChildren<Collider2D>()!=null)
{
Bounds totalBounds = theObject.GetComponentInChildren<Collider2D>().bounds;
Collider2D[] colliders = theObject.GetComponentsInChildren<Collider2D>();
foreach (Collider2D col in colliders)
{
totalBounds.Encapsulate(col.bounds);
}
returnBounds = totalBounds;
return returnBounds;
}
returnBounds = new Bounds(Vector3.zero, Vector3.zero);
return returnBounds;
}
/// <summary>
/// Gets bounds of a renderer
/// </summary>
/// <param name="theObject"></param>
/// <returns></returns>
public static Bounds GetRendererBounds(GameObject theObject)
{
Bounds returnBounds;
// if the object has a renderer at root level, we base our calculations on that
if (theObject.GetComponent<Renderer>()!=null)
{
returnBounds = theObject.GetComponent<Renderer>().bounds;
return returnBounds;
}
// if the object contains at least one renderer we'll add all its children's renderer bounds
if (theObject.GetComponentInChildren<Renderer>()!=null)
{
Bounds totalBounds = theObject.GetComponentInChildren<Renderer>().bounds;
Renderer[] renderers = theObject.GetComponentsInChildren<Renderer>();
foreach (Renderer renderer in renderers)
{
totalBounds.Encapsulate(renderer.bounds);
}
returnBounds = totalBounds;
return returnBounds;
}
returnBounds = new Bounds(Vector3.zero, Vector3.zero);
return returnBounds;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b6df521b0f2978740a4c18dddde873a8
timeCreated: 1455815409
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,51 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Camera extensions
/// </summary>
public static class MMCameraExtensions
{
/// <summary>
/// Returns the width of the camera in world space units, at the specified depths for perspective cameras, everywhere for orthographic ones
/// </summary>
/// <param name="camera"></param>
/// <param name="depth"></param>
/// <returns></returns>
public static float MMCameraWorldSpaceWidth(this Camera camera, float depth = 0f)
{
if (camera.orthographic)
{
return camera.aspect * camera.orthographicSize * 2f;
}
else
{
float fieldOfView = camera.fieldOfView * Mathf.Deg2Rad;
return camera.aspect * depth * Mathf.Tan(fieldOfView);
}
}
/// <summary>
/// Returns the height of the camera in world space units, at the specified depths for perspective cameras, everywhere for orthographic ones
/// </summary>
/// <param name="camera"></param>
/// <param name="depth"></param>
/// <returns></returns>
public static float MMCameraWorldSpaceHeight(this Camera camera, float depth = 0f)
{
if (camera.orthographic)
{
return camera.orthographicSize * 2f;
}
else
{
float fieldOfView = camera.fieldOfView * Mathf.Deg2Rad;
return depth * Mathf.Tan(fieldOfView);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 285d55c3a8191984c848a8ecaee30c9d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,43 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Color extensions
/// </summary>
public static class MMColorExtensions
{
/// <summary>
/// Adds all parts of the color and returns a float
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static float Sum(this Color color)
{
return color.r + color.g + color.b + color.a;
}
/// <summary>
/// Returns a mean value between r, g and b
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static float MeanRGB(this Color color)
{
return (color.r + color.g + color.b) / 3f;
}
/// <summary>
/// Computes the color's luminance value
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static float Luminance(this Color color)
{
return 0.2126f * color.r + 0.7152f * color.g + 0.0722f * color.b;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9ea7a938d4b9d584db335667287955f2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,34 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace MoreMountains.Tools
{
/// <summary>
/// Dictionary extensions
/// </summary>
public static class MMDictionaryExtensions
{
/// <summary>
/// Finds a key (if there's one) that matches the value set in parameters
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="W"></typeparam>
/// <param name="dictionary"></param>
/// <param name="value"></param>
/// <returns></returns>
public static T KeyByValue<T, W>(this Dictionary<T, W> dictionary, T value)
{
T key = default;
foreach (KeyValuePair<T, W> pair in dictionary)
{
if (pair.Value.Equals(value))
{
key = pair.Key;
break;
}
}
return key;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 122b2359ffbe5164db4f3af49fcd7c1a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,39 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Float extensions
/// </summary>
public static class MMFloatExtensions
{
/// <summary>
/// Normalizes an angle in degrees
/// </summary>
/// <param name="angleInDegrees"></param>
/// <returns></returns>
public static float MMNormalizeAngle(this float angleInDegrees)
{
angleInDegrees = angleInDegrees % 360f;
if (angleInDegrees < 0)
{
angleInDegrees += 360f;
}
return angleInDegrees;
}
/// <summary>
/// Rounds a float down
/// </summary>
/// <param name="number"></param>
/// <param name="decimalPlaces"></param>
/// <returns></returns>
public static float RoundDown(this float number, int decimalPlaces)
{
return Mathf.Floor(number * Mathf.Pow(10, decimalPlaces)) / Mathf.Pow(10, decimalPlaces);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 368b8371d6c95b34fae5ced0725180b1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Game object extensions
/// </summary>
public static class GameObjectExtensions
{
static List<Component> m_ComponentCache = new List<Component>();
/// <summary>
/// Grabs a component without allocating memory uselessly
/// </summary>
/// <param name="this"></param>
/// <param name="componentType"></param>
/// <returns></returns>
public static Component MMGetComponentNoAlloc(this GameObject @this, System.Type componentType)
{
@this.GetComponents(componentType, m_ComponentCache);
Component component = m_ComponentCache.Count > 0 ? m_ComponentCache[0] : null;
m_ComponentCache.Clear();
return component;
}
/// <summary>
/// Grabs a component without allocating memory uselessly
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="this"></param>
/// <returns></returns>
public static T MMGetComponentNoAlloc<T>(this GameObject @this) where T : Component
{
@this.GetComponents(typeof(T), m_ComponentCache);
Component component = m_ComponentCache.Count > 0 ? m_ComponentCache[0] : null;
m_ComponentCache.Clear();
return component as T;
}
/// <summary>
/// Grabs a component on the object, or on its children objects, or on a parent, or adds it to the object if none were found
/// </summary>
/// <param name="this"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T MMGetComponentAroundOrAdd<T>(this GameObject @this) where T : Component
{
T component = @this.GetComponentInChildren<T>(true);
if (component == null)
{
component = @this.GetComponentInParent<T>();
}
if (component == null)
{
component = @this.AddComponent<T>();
}
return component;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b2625daa927689341bf30fbd51782d48
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,35 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Layermask Extensions
/// </summary>
public static class LayermaskExtensions
{
/// <summary>
/// Returns bool if layer is within layermask
/// </summary>
/// <param name="mask"></param>
/// <param name="layer"></param>
/// <returns></returns>
public static bool MMContains(this LayerMask mask, int layer)
{
return ((mask.value & (1 << layer)) > 0);
}
/// <summary>
/// Returns true if gameObject is within layermask
/// </summary>
/// <param name="mask"></param>
/// <param name="gameobject"></param>
/// <returns></returns>
public static bool MMContains(this LayerMask mask, GameObject gameobject)
{
return ((mask.value & (1 << gameobject.layer)) > 0);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f5786eaa162a9da408b32f063e56886d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,40 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// List extensions
/// </summary>
public static class ListExtensions
{
/// <summary>
/// Swaps two items in a list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="i"></param>
/// <param name="j"></param>
public static void MMSwap<T>(this IList<T> list, int i, int j)
{
T temporary = list[i];
list[i] = list[j];
list[j] = temporary;
}
/// <summary>
/// Shuffles a list randomly
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
public static void MMShuffle<T>(this IList<T> list)
{
for (int i = 0; i < list.Count; i++)
{
list.MMSwap(i, Random.Range(i, list.Count));
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 273f85de13f3ea84a9fe0139daf01c37
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
using UnityEngine;
namespace MoreMountains.Tools
{
/// <summary>
/// Rect extensions
/// </summary>
public static class RectExtensions
{
/// <summary>
/// Returns true if this rectangle intersects the other specified rectangle
/// </summary>
/// <param name="thisRectangle"></param>
/// <param name="otherRectangle"></param>
/// <returns></returns>
public static bool MMIntersects(this Rect thisRectangle, Rect otherRectangle)
{
return !((thisRectangle.x > otherRectangle.xMax) || (thisRectangle.xMax < otherRectangle.x) || (thisRectangle.y > otherRectangle.yMax) || (thisRectangle.yMax < otherRectangle.y));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: caa7c923e7ca83849a4da9421e095559
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,53 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// RectTransform extensions
/// </summary>
public static class MMRectTransformExtensions
{
/// <summary>
/// Sets the left offset of a rect transform to the specified value
/// </summary>
/// <param name="rt"></param>
/// <param name="left"></param>
public static void MMSetLeft(this RectTransform rt, float left)
{
rt.offsetMin = new Vector2(left, rt.offsetMin.y);
}
/// <summary>
/// Sets the right offset of a rect transform to the specified value
/// </summary>
/// <param name="rt"></param>
/// <param name="right"></param>
public static void MMSetRight(this RectTransform rt, float right)
{
rt.offsetMax = new Vector2(-right, rt.offsetMax.y);
}
/// <summary>
/// Sets the top offset of a rect transform to the specified value
/// </summary>
/// <param name="rt"></param>
/// <param name="top"></param>
public static void MMSetTop(this RectTransform rt, float top)
{
rt.offsetMax = new Vector2(rt.offsetMax.x, -top);
}
/// <summary>
/// Sets the bottom offset of a rect transform to the specified value
/// </summary>
/// <param name="rt"></param>
/// <param name="bottom"></param>
public static void MMSetBottom(this RectTransform rt, float bottom)
{
rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 766d979625785dc48aecf8835de77661
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,25 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Renderer extensions
/// </summary>
public static class RendererExtensions
{
/// <summary>
/// Returns true if a renderer is visible from a camera
/// </summary>
/// <param name="renderer"></param>
/// <param name="camera"></param>
/// <returns></returns>
public static bool MMIsVisibleFrom(this Renderer renderer, Camera camera)
{
Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(frustumPlanes, renderer.bounds);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5e41d6cfe6c233c40a2bf4ddc63a71dc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Scrollrect extensions
/// </summary>
public static class ScrollRectExtensions
{
/// <summary>
/// Scrolls a scroll rect to the top
/// </summary>
/// <param name="scrollRect"></param>
public static void MMScrollToTop(this ScrollRect scrollRect)
{
scrollRect.normalizedPosition = new Vector2(0, 1);
}
/// <summary>
/// Scrolls a scroll rect to the bottom
/// </summary>
public static void MMScrollToBottom(this ScrollRect scrollRect)
{
scrollRect.normalizedPosition = new Vector2(0, 0);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ff41eb325c823854b878fbc622f8d9fe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace MoreMountains.Tools
{
/// <summary>
/// Serialized property extensions
/// </summary>
public static class MMSerializedPropertyExtensions
{
#if UNITY_EDITOR
/// <summary>
/// Returns the object value of a target serialized property
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
public static object MMGetObjectValue(this SerializedProperty property)
{
if (property == null)
{
return null;
}
string propertyPath = property.propertyPath.Replace(".Array.data[", "[");
object targetObject = property.serializedObject.targetObject;
var elements = propertyPath.Split('.');
foreach (var element in elements)
{
if (!element.Contains("["))
{
targetObject = GetPropertyValue(targetObject, element);
}
else
{
string elementName = element.Substring(0, element.IndexOf("["));
int elementIndex = System.Convert.ToInt32(element.Substring(element.IndexOf("[")).Replace("[", "").Replace("]", ""));
targetObject = GetPropertyValue(targetObject, elementName, elementIndex);
}
}
return targetObject;
}
private static object GetPropertyValue(object source, string propertyName)
{
if (source == null)
{
return null;
}
Type propertyType = source.GetType();
while (propertyType != null)
{
FieldInfo fieldInfo = propertyType.GetField(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (fieldInfo != null)
{
return fieldInfo.GetValue(source);
}
PropertyInfo propertyInfo = propertyType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
if (propertyInfo != null)
{
return propertyInfo.GetValue(source, null);
}
propertyType = propertyType.BaseType;
}
return null;
}
private static object GetPropertyValue(object source, string propertyName, int index)
{
var enumerable = GetPropertyValue(source, propertyName) as System.Collections.IEnumerable;
if (enumerable == null)
{
return null;
}
var enumerator = enumerable.GetEnumerator();
for (int i = 0; i <= index; i++)
{
if (!enumerator.MoveNext())
{
return null;
}
}
return enumerator.Current;
}
#endif
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8c0f2ffbd055e44497199bd1a209d9f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,109 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Transform extensions
/// </summary>
public static class TransformExtensions
{
/// <summary>
/// Destroys a transform's children
/// </summary>
/// <param name="transform"></param>
public static void MMDestroyAllChildren(this Transform transform)
{
for (int t = transform.childCount - 1; t >= 0; t--)
{
if (Application.isPlaying)
{
UnityEngine.Object.Destroy(transform.GetChild(t).gameObject);
}
else
{
UnityEngine.Object.DestroyImmediate(transform.GetChild(t).gameObject);
}
}
}
/// <summary>
/// Finds children by name, breadth first
/// </summary>
/// <param name="parent"></param>
/// <param name="transformName"></param>
/// <returns></returns>
public static Transform MMFindDeepChildBreadthFirst(this Transform parent, string transformName)
{
Queue<Transform> queue = new Queue<Transform>();
queue.Enqueue(parent);
while (queue.Count > 0)
{
Transform child = queue.Dequeue();
if (child.name == transformName)
{
return child;
}
foreach (Transform t in child)
{
queue.Enqueue(t);
}
}
return null;
}
/// <summary>
/// Finds children by name, depth first
/// </summary>
/// <param name="parent"></param>
/// <param name="transformName"></param>
/// <returns></returns>
public static Transform MMFindDeepChildDepthFirst(this Transform parent, string transformName)
{
foreach (Transform child in parent)
{
if (child.name == transformName)
{
return child;
}
Transform result = child.MMFindDeepChildDepthFirst(transformName);
if (result != null)
{
return result;
}
}
return null;
}
/// <summary>
/// Changes the layer of a transform and all its children to the new one
/// </summary>
/// <param name="transform"></param>
/// <param name="layerName"></param>
public static void ChangeLayersRecursively(this Transform transform, string layerName)
{
transform.gameObject.layer = LayerMask.NameToLayer(layerName);
foreach (Transform child in transform)
{
child.ChangeLayersRecursively(layerName);
}
}
/// <summary>
/// Changes the layer of a transform and all its children to the new one
/// </summary>
/// <param name="transform"></param>
/// <param name="layerIndex"></param>
public static void ChangeLayersRecursively(this Transform transform, int layerIndex)
{
transform.gameObject.layer = layerIndex;
foreach (Transform child in transform)
{
child.ChangeLayersRecursively(layerIndex);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aaf470b8c3a10a941998800be2e324cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Vector2 extensions
/// </summary>
public static class MMVector2Extensions
{
/// <summary>
/// Rotates a vector2 by angleInDegrees
/// </summary>
/// <param name="vector"></param>
/// <param name="angleInDegrees"></param>
/// <returns></returns>
public static Vector2 MMRotate(this Vector2 vector, float angleInDegrees)
{
float sin = Mathf.Sin(angleInDegrees * Mathf.Deg2Rad);
float cos = Mathf.Cos(angleInDegrees * Mathf.Deg2Rad);
float tx = vector.x;
float ty = vector.y;
vector.x = (cos * tx) - (sin * ty);
vector.y = (sin * tx) + (cos * ty);
return vector;
}
/// <summary>
/// Sets the X part of a Vector2
/// </summary>
/// <param name="vector"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public static Vector2 MMSetX(this Vector2 vector, float newValue)
{
vector.x = newValue;
return vector;
}
/// <summary>
/// Sets the Y part of a Vector2
/// </summary>
/// <param name="vector"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public static Vector2 MMSetY(this Vector2 vector, float newValue)
{
vector.y = newValue;
return vector;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 32341f6d89b7cd14bbe2102075e9e0cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,100 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// Vector3 Extensions
/// </summary>
public static class MMVector3Extensions
{
/// <summary>
/// Sets the x value of a vector
/// </summary>
/// <param name="vector"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public static Vector3 MMSetX(this Vector3 vector, float newValue)
{
vector.x = newValue;
return vector;
}
/// <summary>
/// Sets the y value of a vector
/// </summary>
/// <param name="vector"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public static Vector3 MMSetY(this Vector3 vector, float newValue)
{
vector.y = newValue;
return vector;
}
/// <summary>
/// Sets the z value of a vector
/// </summary>
/// <param name="vector"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public static Vector3 MMSetZ(this Vector3 vector, float newValue)
{
vector.z = newValue;
return vector;
}
/// <summary>
/// Inverts a vector
/// </summary>
/// <param name="newValue"></param>
/// <returns></returns>
public static Vector3 MMInvert(this Vector3 newValue)
{
return new Vector3
(
1.0f / newValue.x,
1.0f / newValue.y,
1.0f / newValue.z
);
}
/// <summary>
/// Projects a vector on another
/// </summary>
/// <param name="vector"></param>
/// <param name="projectedVector"></param>
/// <returns></returns>
public static Vector3 MMProject(this Vector3 vector, Vector3 projectedVector)
{
float _dot = Vector3.Dot(vector, projectedVector);
return _dot * projectedVector;
}
/// <summary>
/// Rejects a vector on another
/// </summary>
/// <param name="vector"></param>
/// <param name="rejectedVector"></param>
/// <returns></returns>
public static Vector3 MMReject(this Vector3 vector, Vector3 rejectedVector)
{
return vector - vector.MMProject(rejectedVector);
}
/// <summary>
/// Rounds all components of a vector
/// </summary>
/// <param name="vector"></param>
/// <returns></returns>
public static Vector3 MMRound(this Vector3 vector)
{
vector.x = Mathf.Round(vector.x);
vector.y = Mathf.Round(vector.y);
vector.z = Mathf.Round(vector.z);
return vector;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 40f518105f6f40f459c8d96790772f07
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: