Created
September 3, 2019 14:03
-
-
Save szethh/eefd022642d7d5b7e0c65ea44c4f78a3 to your computer and use it in GitHub Desktop.
ExtensionMethods #unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEngine; | |
using System.Reflection; | |
using UnityEditor; | |
static class ExtensionMethods | |
{ | |
/// <summary> | |
/// Rounds Vector3. | |
/// </summary> | |
/// <param name="vector3"></param> | |
/// <param name="decimalPlaces"></param> | |
/// <returns></returns> | |
public static Vector3 Round(this Vector3 vector3, int decimalPlaces = 2) | |
{ | |
float multiplier = 1; | |
for (int i = 0; i < decimalPlaces; i++) | |
{ | |
multiplier *= 10f; | |
} | |
return new Vector3( | |
Mathf.Round(vector3.x * multiplier) / multiplier, | |
Mathf.Round(vector3.y * multiplier) / multiplier, | |
Mathf.Round(vector3.z * multiplier) / multiplier); | |
} | |
/// <summary> | |
/// Calculates the direction between A and B. Ignores the Y component of both Vector3. | |
/// </summary> | |
/// <param name="A"></param> | |
/// <param name="B"></param> | |
/// <returns></returns> | |
public static Vector3 Direction(this Vector3 A, Vector3 B) | |
{ | |
Vector3 a = A, b = B; | |
a.y = b.y = 0; | |
return b - a; | |
} | |
/// <summary> | |
/// Finds nearest collider with the given component. | |
/// </summary> | |
/// <param name="type"></param> | |
/// <param name="center"></param> | |
/// <param name="radius"></param> | |
/// <returns></returns> | |
public static T FindNearestColliderOfType<T>(this T type, Vector3 center, float radius, | |
QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal) where T : Component | |
{ | |
Collider[] cols = Physics.OverlapSphere(center, radius, ~0, queryTriggerInteraction); | |
//if (typeof(T) == typeof(Rail)) | |
// Debug.Log(string.Format("[{0}]", string.Join(", ", cols.Select(x => x.name).ToArray()))); | |
T[] comps = cols.Select(c => c.GetComponent<T>()). // Select items with the component | |
Where(x => x != null). // that is not null | |
OrderBy(d => Vector3.Distance(center, d.transform.position)). // and sort them by distance | |
ToArray(); | |
if (comps.Length == 0) return null; | |
return comps[0]; | |
} | |
/// <summary> | |
/// Rounds Vector3. | |
/// </summary> | |
/// <param name="t"></param> | |
/// <param name="x"></param> | |
/// <param name="y"></param> | |
/// <param name="z"></param> | |
/// <returns></returns> | |
public static float AverageSize(this Transform t, bool x = true, bool y = false, bool z = true) | |
{ | |
float total = 0f; | |
int amt = 0; | |
if (x) { total += t.localScale.x; amt++; } | |
if (y) { total += t.localScale.y; amt++; } | |
if (z) { total += t.localScale.z; amt++; } | |
return (float)total / amt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment