chore: initial commit
This commit is contained in:
341
Assets/Feel/MMTools/Core/MMExtensions/MMAnimatorExtensions.cs
Normal file
341
Assets/Feel/MMTools/Core/MMExtensions/MMAnimatorExtensions.cs
Normal file
@@ -0,0 +1,341 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fbe34950474788247ae05de95fa199f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
43
Assets/Feel/MMTools/Core/MMExtensions/MMArrayExtensions.cs
Normal file
43
Assets/Feel/MMTools/Core/MMExtensions/MMArrayExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6e5aa704bd68fb42a198fccd658dbfb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
111
Assets/Feel/MMTools/Core/MMExtensions/MMBoundsExtensions.cs
Normal file
111
Assets/Feel/MMTools/Core/MMExtensions/MMBoundsExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6df521b0f2978740a4c18dddde873a8
|
||||
timeCreated: 1455815409
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
48
Assets/Feel/MMTools/Core/MMExtensions/MMCameraExtensions.cs
Normal file
48
Assets/Feel/MMTools/Core/MMExtensions/MMCameraExtensions.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 285d55c3a8191984c848a8ecaee30c9d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
40
Assets/Feel/MMTools/Core/MMExtensions/MMColorExtensions.cs
Normal file
40
Assets/Feel/MMTools/Core/MMExtensions/MMColorExtensions.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ea7a938d4b9d584db335667287955f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 122b2359ffbe5164db4f3af49fcd7c1a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
36
Assets/Feel/MMTools/Core/MMExtensions/MMFloatExtensions.cs
Normal file
36
Assets/Feel/MMTools/Core/MMExtensions/MMFloatExtensions.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 368b8371d6c95b34fae5ced0725180b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,98 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the specified component on the object, or adds it and returns it if there isn't already one
|
||||
/// </summary>
|
||||
/// <param name="gameObject"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T MMGetOrAddComponent<T>(this GameObject @this) where T : Component
|
||||
{
|
||||
T component = @this.GetComponent<T>();
|
||||
if (component == null)
|
||||
{
|
||||
component = @this.AddComponent<T>();
|
||||
}
|
||||
return component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the specified component on the object, or adds it and returns it if there isn't already one
|
||||
/// </summary>
|
||||
/// <param name="gameObject"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static (T newComponent, bool createdNew) MMFindOrCreateObjectOfType<T>(this GameObject @this, string newObjectName, Transform parent, bool forceNewCreation = false) where T : Component
|
||||
{
|
||||
T searchedObject = (T)Object.FindObjectOfType(typeof(T));
|
||||
if ((searchedObject == null) || forceNewCreation)
|
||||
{
|
||||
GameObject newGo = new GameObject(newObjectName);
|
||||
newGo.transform.SetParent(parent);
|
||||
return (newGo.AddComponent<T>(), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (searchedObject, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2625daa927689341bf30fbd51782d48
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f5786eaa162a9da408b32f063e56886d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
38
Assets/Feel/MMTools/Core/MMExtensions/MMListExtensions.cs
Normal file
38
Assets/Feel/MMTools/Core/MMExtensions/MMListExtensions.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 273f85de13f3ea84a9fe0139daf01c37
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Assets/Feel/MMTools/Core/MMExtensions/MMRectExtensions.cs
Normal file
21
Assets/Feel/MMTools/Core/MMExtensions/MMRectExtensions.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: caa7c923e7ca83849a4da9421e095559
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,50 @@
|
||||
using UnityEngine;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 766d979625785dc48aecf8835de77661
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
using UnityEngine;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5e41d6cfe6c233c40a2bf4ddc63a71dc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
#if MM_UI
|
||||
using UnityEngine;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff41eb325c823854b878fbc622f8d9fe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8c0f2ffbd055e44497199bd1a209d9f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
127
Assets/Feel/MMTools/Core/MMExtensions/MMTransformExtensions.cs
Normal file
127
Assets/Feel/MMTools/Core/MMExtensions/MMTransformExtensions.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Enumerates all parents of a transform
|
||||
/// </summary>
|
||||
/// <param name="targetTransform"></param>
|
||||
/// <param name="includeSelf"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Transform> MMEnumerateAllParents(this Transform targetTransform, bool includeSelf = false)
|
||||
{
|
||||
if (!includeSelf)
|
||||
{
|
||||
targetTransform = targetTransform?.parent;
|
||||
}
|
||||
while (targetTransform)
|
||||
{
|
||||
yield return targetTransform;
|
||||
targetTransform = targetTransform.parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aaf470b8c3a10a941998800be2e324cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
51
Assets/Feel/MMTools/Core/MMExtensions/MMVector2Extensions.cs
Normal file
51
Assets/Feel/MMTools/Core/MMExtensions/MMVector2Extensions.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using UnityEngine;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32341f6d89b7cd14bbe2102075e9e0cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
97
Assets/Feel/MMTools/Core/MMExtensions/MMVector3Extensions.cs
Normal file
97
Assets/Feel/MMTools/Core/MMExtensions/MMVector3Extensions.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using UnityEngine;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40f518105f6f40f459c8d96790772f07
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
121
Assets/Feel/MMTools/Core/MMExtensions/MMVector4Extensions.cs
Normal file
121
Assets/Feel/MMTools/Core/MMExtensions/MMVector4Extensions.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Vector4 Extensions
|
||||
/// </summary>
|
||||
public static class MMVector4Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the sum of all components of a vector4
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
/// <returns></returns>
|
||||
public static float SumComponents(this Vector4 vector)
|
||||
{
|
||||
return vector.x + vector.y + vector.z + vector.w;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the x value of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
/// <param name="newValue"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector4 MMSetX(this Vector4 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 Vector4 MMSetY(this Vector4 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 Vector4 MMSetZ(this Vector4 vector, float newValue)
|
||||
{
|
||||
vector.z = newValue;
|
||||
return vector;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the z value of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
/// <param name="newValue"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector4 MMSetW(this Vector4 vector, float newValue)
|
||||
{
|
||||
vector.w = newValue;
|
||||
return vector;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inverts a vector
|
||||
/// </summary>
|
||||
/// <param name="newValue"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector4 MMInvert(this Vector4 newValue)
|
||||
{
|
||||
return new Vector4
|
||||
(
|
||||
1.0f / newValue.x,
|
||||
1.0f / newValue.y,
|
||||
1.0f / newValue.z,
|
||||
1.0f / newValue.w
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Projects a vector on another
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
/// <param name="projectedVector"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector4 MMProject(this Vector4 vector, Vector4 projectedVector)
|
||||
{
|
||||
float _dot = Vector4.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 Vector4 MMReject(this Vector4 vector, Vector4 rejectedVector)
|
||||
{
|
||||
return vector - vector.MMProject(rejectedVector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds all components of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector4 MMRound(this Vector4 vector)
|
||||
{
|
||||
vector.x = Mathf.Round(vector.x);
|
||||
vector.y = Mathf.Round(vector.y);
|
||||
vector.z = Mathf.Round(vector.z);
|
||||
vector.w = Mathf.Round(vector.w);
|
||||
return vector;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75c82ca66be53244b90a3a4794a07e63
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user