Last active
August 8, 2017 00:29
-
-
Save keiranlovett/45a649322ae4ccdbb2165b45cf19c7fd to your computer and use it in GitHub Desktop.
Unity Utilities
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.Collections.Generic; | |
using UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class ActorUtililty | |
{ | |
/// <summary> | |
/// Caching because Actors should never be removed | |
/// </summary> | |
private static Dictionary<GameObject, Actor> actorCache = new Dictionary<GameObject, Actor>(); | |
public static Actor GetActor(this Component component) | |
{ | |
return GetActor<Actor>(component); | |
} | |
public static A GetActor<A>(this Component component) where A : Actor | |
{ | |
if(!component) | |
{ | |
return null; | |
} | |
return component.gameObject.GetActor<A>(); | |
} | |
public static Actor GetActor(this GameObject gameObject) | |
{ | |
return GetActor<Actor>(gameObject); | |
} | |
public static A GetActor<A>(this GameObject gameObject) where A : Actor | |
{ | |
if(!gameObject) | |
{ | |
return null; | |
} | |
Actor actor; | |
if(!actorCache.TryGetValue(gameObject, out actor)) | |
{ | |
actor = gameObject.GetComponentInParent<A>(); | |
actorCache.Add(gameObject, actor); | |
} | |
return actor as A; | |
} | |
public static void RemoveActorFromCache(Actor actor) | |
{ | |
actorCache.Remove(actor.gameObject); | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class BoundsUtility | |
{ | |
/// <summary> | |
/// Returns true if this bounds.size >= target.size in all axes. | |
/// </summary> | |
/// <param name="bounds"></param> | |
/// <param name="target"></param> | |
/// <returns></returns> | |
public static bool CouldContain(this Bounds bounds, Bounds target) | |
{ | |
var bSize = bounds.size; | |
var tSize = target.size; | |
return bSize.x >= tSize.x && | |
bSize.y >= tSize.y && | |
bSize.z >= tSize.z; | |
} | |
public static Vector3 RandomPointInside(this Bounds bounds) | |
{ | |
return new Vector3() | |
{ | |
x = Random.Range(bounds.min.x, bounds.max.x), | |
y = Random.Range(bounds.min.y, bounds.max.y), | |
z = Random.Range(bounds.min.z, bounds.max.z) | |
}; | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class CameraUtility | |
{ | |
public static Bounds OrthographicBounds(this Camera camera) | |
{ | |
var camHeight = camera.orthographicSize * 2f; | |
return new Bounds(camera.transform.position, new Vector3(camHeight * camera.aspect, camHeight, 0)); | |
} | |
} | |
} |
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using URandom = UnityEngine.Random; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class CollectionUtility | |
{ | |
public static void Shuffle<T>(this IList<T> list) | |
{ | |
var n = list.Count; | |
while (n > 1) | |
{ | |
n--; | |
var k = URandom.Range(0, n + 1); | |
var value = list[k]; | |
list[k] = list[n]; | |
list[n] = value; | |
} | |
} | |
public static T Random<T>(this IEnumerable<T> enumerable) | |
{ | |
if (enumerable == null) | |
{ | |
throw new ArgumentNullException(nameof(enumerable)); | |
} | |
var list = enumerable as IList<T>; | |
if (list == null) | |
{ | |
list = enumerable.ToList(); | |
} | |
return list.Count == 0 ? default(T) : list[URandom.Range(0, list.Count)]; | |
} | |
public static T[] Random<T>(this IEnumerable<T> enumerable, int count) | |
{ | |
if (enumerable == null) | |
{ | |
throw new ArgumentNullException(nameof(enumerable)); | |
} | |
var array = new T[count]; | |
for (int i = 0; i < count; i++) | |
{ | |
array[i] = enumerable.Random(); | |
} | |
return array; | |
} | |
public static void AddOrSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) | |
{ | |
if (dictionary.ContainsKey(key)) | |
{ | |
dictionary[key] = value; | |
} | |
else | |
{ | |
dictionary.Add(key, value); | |
} | |
} | |
} | |
} |
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; | |
using UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class ComponentUtility | |
{ | |
public static T Create<T>() where T : Component | |
{ | |
return Create<T>(typeof(T).Name); | |
} | |
public static T Create<T>(string name) where T : Component | |
{ | |
return new GameObject(name).AddComponent<T>(); | |
} | |
public static Component Create(Type t) | |
{ | |
return Create(t, t.Name); | |
} | |
public static Component Create(Type t, string name) | |
{ | |
return new GameObject(name).AddComponent(t); | |
} | |
public static void SetEnabledAll(bool enabled, params Behaviour[] behaviors) | |
{ | |
foreach (var b in behaviors) | |
{ | |
if (b != null) | |
{ | |
b.enabled = enabled; | |
} | |
} | |
} | |
public static bool TryDestroy<T>(Component c) where T : Component | |
{ | |
var t = c.GetComponent<T>(); | |
if (t) | |
{ | |
UnityEngine.Object.Destroy(t); | |
return true; | |
} | |
return false; | |
} | |
public static bool TryDestroyAll<T>(Component c) where T : Component | |
{ | |
var all = c.GetComponents<T>(); | |
for (int i = 0; i < all.Length; i++) | |
{ | |
UnityEngine.Object.Destroy(all[i]); | |
} | |
return all.Length > 0; | |
} | |
} | |
} |
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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class CoroutineUtility | |
{ | |
private static EmptyMonoBehaviour _surrogate; | |
private static EmptyMonoBehaviour surrogate { | |
get { | |
EnsureSurrogate(); | |
return _surrogate; | |
} | |
} | |
private static Dictionary<string, Coroutine> keyedRoutines = new Dictionary<string, Coroutine>(); | |
public static Coroutine StartCoroutine(IEnumerator routine) | |
{ | |
return surrogate.StartCoroutine(routine); | |
} | |
public static Coroutine StartCoroutine(string key, IEnumerator routine) | |
{ | |
var coroutine = surrogate.StartCoroutine(routine); | |
keyedRoutines.Add(key, coroutine); | |
return coroutine; | |
} | |
public static void StopAllCoroutines() | |
{ | |
surrogate.StopAllCoroutines(); | |
keyedRoutines.Clear(); | |
} | |
public static void StopKeyedCoroutines() | |
{ | |
foreach (var coroutine in keyedRoutines.Values) | |
{ | |
StopCoroutine(coroutine); | |
} | |
keyedRoutines.Clear(); | |
} | |
public static void StopCoroutine(IEnumerator routine) | |
{ | |
surrogate.StopCoroutine(routine); | |
} | |
public static void StopCoroutine(Coroutine routine) | |
{ | |
surrogate.StopCoroutine(routine); | |
} | |
public static void StopCoroutine(string key) | |
{ | |
Coroutine routine; | |
if (keyedRoutines.TryGetValue(key, out routine)) | |
{ | |
surrogate.StopCoroutine(routine); | |
} | |
} | |
private static void EnsureSurrogate() | |
{ | |
if (!_surrogate) | |
{ | |
_surrogate = ComponentUtility.Create<EmptyMonoBehaviour>("CoroutineUtilitySurrogate"); | |
_surrogate.hideFlags = HideFlags.NotEditable; | |
Object.DontDestroyOnLoad(_surrogate); | |
} | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class DebugUtility | |
{ | |
public static string GetDebugName(this Component cmp) | |
{ | |
return GetDebugName(cmp.gameObject); | |
} | |
public static string GetDebugName(this GameObject go) | |
{ | |
return string.Format("[{0}] {1}", go.GetInstanceID(), go.name); | |
} | |
public static void DrawArrow(Vector3 pos, Vector3 direction, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) | |
{ | |
DrawArrow(pos, direction, Color.white, arrowHeadLength, arrowHeadAngle); | |
} | |
public static void DrawArrow(Vector3 pos, Vector3 direction, Color color, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) | |
{ | |
Debug.DrawRay(pos, direction, color); | |
var right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) * new Vector3(0, 0, 1); | |
var left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) * new Vector3(0, 0, 1); | |
Debug.DrawRay(pos + direction, right * arrowHeadLength, color); | |
Debug.DrawRay(pos + direction, left * arrowHeadLength, color); | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class GizmosUtility | |
{ | |
private static Color storedColor; | |
public static void DrawBounds(Bounds b) | |
{ | |
Gizmos.DrawWireCube(b.center, b.size); | |
} | |
/// <summary> | |
/// Stores the current Gizmos color and switches to a new color. | |
/// Used in conjunction with <see cref="RestoreColor"/> | |
/// </summary> | |
/// <param name="color"></param> | |
public static void StoreColor(Color color) | |
{ | |
storedColor = Gizmos.color; | |
Gizmos.color = color; | |
} | |
/// <summary> | |
/// Restores the stored Gizmos color from <see cref="StoreColor(Color)"/> | |
/// </summary> | |
public static void RestoreColor() | |
{ | |
Gizmos.color = storedColor; | |
} | |
public static void DrawArrow(Vector3 pos, Vector3 direction, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) | |
{ | |
DrawArrow(pos, direction, Gizmos.color, arrowHeadLength, arrowHeadAngle); | |
} | |
public static void DrawArrow(Vector3 pos, Vector3 direction, Color color, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) | |
{ | |
Gizmos.color = color; | |
Gizmos.DrawRay(pos, direction); | |
var right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) * new Vector3(0, 0, 1); | |
var left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) * new Vector3(0, 0, 1); | |
Gizmos.DrawRay(pos + direction, right * arrowHeadLength); | |
Gizmos.DrawRay(pos + direction, left * arrowHeadLength); | |
} | |
} | |
} |
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 UnityEngine; | |
using UnityEngine.Assertions; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class GameObjectUtility | |
{ | |
public static T AddOrGetComponent<T>(this GameObject obj) where T : Component | |
{ | |
T component = obj.GetComponent<T>(); | |
if (component == null) | |
{ | |
return obj.AddComponent<T>(); | |
} | |
else | |
{ | |
return component; | |
} | |
} | |
public static T AssertComponent<T>(this GameObject obj) where T : Component | |
{ | |
Assert.IsTrue(obj); | |
Assert.IsNotNull(obj); | |
var component = obj.GetComponent<T>(); | |
Assert.IsTrue(component); | |
Assert.IsNotNull(component); | |
return obj.AddComponent<T>(); | |
} | |
public static void SetLayerRecursive(this GameObject obj, int layer) | |
{ | |
obj.layer = layer; | |
foreach (Transform child in obj.transform) | |
{ | |
child.gameObject.SetLayerRecursive(layer); | |
} | |
} | |
public static string GetPath(GameObject obj) | |
{ | |
var sb = new System.Text.StringBuilder("/" + obj.name); | |
while (obj.transform.parent != null) | |
{ | |
obj = obj.transform.parent.gameObject; | |
sb.Insert(0, "/" + obj.name); | |
} | |
return sb.ToString(); | |
} | |
public static void Toggle(bool enabled, params GameObject[] gameObjects) | |
{ | |
foreach (var go in gameObjects) | |
{ | |
if (go != null) | |
{ | |
go.SetActive(enabled); | |
} | |
} | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class GradientUtility | |
{ | |
public static Gradient MakeRainbow(this Gradient g) | |
{ | |
g.SetColors(new Color(1f, 0f, 0f), | |
new Color(1f, 1f, 0f), | |
new Color(0f, 1f, 0f), | |
new Color(0f, 1f, 1f), | |
new Color(1f, 0f, 1f)); | |
g.alphaKeys = new GradientAlphaKey[] { | |
new GradientAlphaKey(1f, 0f) | |
}; | |
return g; | |
} | |
public static Gradient MakeMeter(this Gradient g) | |
{ | |
g.SetColors(Color.red, Color.yellow, Color.green); | |
g.alphaKeys = new GradientAlphaKey[] { | |
new GradientAlphaKey(1f, 0f) | |
}; | |
return g; | |
} | |
public static Gradient SetColors(this Gradient g, params Color[] colors) | |
{ | |
var interval = 1f / (colors.Length - 1); | |
var keys = new GradientColorKey[colors.Length]; | |
for (int i = 0; i < colors.Length; i++) | |
{ | |
var t = Mathf.Clamp01(i * interval); | |
keys[i] = new GradientColorKey(colors[i], t); | |
} | |
g.colorKeys = keys; | |
return g; | |
} | |
public static Gradient SetAlphas(this Gradient g, params float[] alphas) | |
{ | |
var interval = 1f / (alphas.Length - 1); | |
var keys = new GradientAlphaKey[alphas.Length]; | |
for (int i = 0; i < alphas.Length; i++) | |
{ | |
var t = Mathf.Clamp01(i * interval); | |
keys[i] = new GradientAlphaKey(alphas[i], t); | |
} | |
g.alphaKeys = keys; | |
return g; | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class Math2DUtility | |
{ | |
public static float AngleRaw(Transform a, Transform b) | |
{ | |
return AngleRaw(a.position, b.position); | |
} | |
public static float Angle(Transform a, Transform b) | |
{ | |
return Angle(a.position, b.position); | |
} | |
public static float AngleRaw(Vector2 a, Vector2 b) | |
{ | |
var dir = b - a; | |
return Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; | |
} | |
public static float Angle(Vector2 a, Vector2 b) | |
{ | |
var angle = AngleRaw(a, b); | |
return ToNormalized(AngleRaw(a, b)); | |
} | |
/// <summary> | |
/// Returns an angle from 0...360 | |
/// </summary> | |
/// <param name="angle"></param> | |
/// <returns></returns> | |
public static float ToNormalized(float angle) | |
{ | |
if (Mathf.Sign(angle) == -1f) | |
{ | |
angle = 360f - Mathf.Abs(angle); | |
} | |
return angle; | |
} | |
public static bool IsFacingRight(float angle) | |
{ | |
angle = ToNormalized(angle); | |
return angle > 90 && angle < 270; | |
} | |
public static void LookAt2D(this Transform t, Transform other) | |
{ | |
LookAt2D(t, other.position); | |
} | |
public static void LookAt2D(this Transform t, Vector2 other) | |
{ | |
var angle = Angle(t.position, other); | |
t.rotation = Quaternion.AngleAxis(angle, Vector3.forward); | |
} | |
public static void SetForward2D(this Transform t, Vector2 forward) | |
{ | |
t.right = forward; | |
} | |
} | |
} |
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; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class MathUtility | |
{ | |
/// <summary> | |
/// Clamps a float to the -1...1 inclusive range | |
/// </summary> | |
/// <param name="f"></param> | |
/// <returns>A normalized float.</returns> | |
public static float Normalize(float f) | |
{ | |
if (f > 1f) | |
{ | |
return 1f; | |
} | |
else if (f < -1f) | |
{ | |
return -1f; | |
} | |
else | |
{ | |
return f; | |
} | |
} | |
public static float SignOrZero(float f) | |
{ | |
if (f == 0f) | |
{ | |
return 0f; | |
} | |
else | |
{ | |
return UnityEngine.Mathf.Sign(f); | |
} | |
} | |
public static byte ClampByte(byte current, byte min, byte max) | |
{ | |
if (current > max) | |
{ | |
return max; | |
} | |
else if (current < min) | |
{ | |
return min; | |
} | |
else | |
{ | |
return current; | |
} | |
} | |
public static sbyte ClampSByte(sbyte current, sbyte min, sbyte max) | |
{ | |
if (current > max) | |
{ | |
return max; | |
} | |
else if (current < min) | |
{ | |
return min; | |
} | |
else | |
{ | |
return current; | |
} | |
} | |
public static float RoundTo(float f, float nearest) | |
{ | |
var multiple = 1f / nearest; | |
return (float)Math.Round(f * multiple, MidpointRounding.AwayFromZero) / multiple; | |
} | |
public static float Map(float value, float oldMin, float oldMax, float newMin, float newMax) | |
{ | |
return (((value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin; | |
} | |
} | |
} |
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.IO; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class PathUtility | |
{ | |
public static string Combine(params string[] paths) | |
{ | |
var path = string.Empty; | |
foreach (var p in paths) | |
{ | |
path = Path.Combine(path, p); | |
} | |
return path; | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class RandomUtility | |
{ | |
public static Vector4 Vector4(float min, float max) | |
{ | |
return new Vector4(Random.Range(min, max), Random.Range(min, max), Random.Range(min, max), Random.Range(min, max)); | |
} | |
public static Vector3 Vector3(float min, float max) | |
{ | |
return new Vector3(Random.Range(min, max), Random.Range(min, max), Random.Range(min, max)); | |
} | |
public static Vector2 Vector2(float min, float max) | |
{ | |
return new Vector2(Random.Range(min, max), Random.Range(min, max)); | |
} | |
public static float NormalRange() | |
{ | |
return Random.Range(0f, 1f); | |
} | |
public static float FullRange() | |
{ | |
return Random.Range(-1f, 1f); | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class RectUtility | |
{ | |
public static Vector2 ClosestPointOnRect(this Rect rect, Vector2 point) | |
{ | |
Vector2 adjusted = Vector2.zero; | |
adjusted.x = Mathf.Clamp(point.x, rect.xMin, rect.xMax); | |
adjusted.y = Mathf.Clamp(point.y, rect.yMin, rect.yMax); | |
return adjusted; | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class RichTextUtility | |
{ | |
public static string MakeColored(this string s, Color c) | |
{ | |
return string.Format("<color=#{0}>{1}</color>", ColorUtility.ToHtmlStringRGB(c), s); | |
} | |
public static string MakeBold(this string s) | |
{ | |
return string.Format("<b>{0}</b>", s); | |
} | |
public static string MakeItalic(this string s) | |
{ | |
return string.Format("<i>{0}</i>", s); | |
} | |
} | |
} |
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
namespace UnityCommonLibrary.Utility | |
{ | |
public static class StringUtility | |
{ | |
public static bool IsNullOrWhitespace(string str) | |
{ | |
if (str == null) | |
{ | |
return true; | |
} | |
for (int i = 0; i < str.Length; i++) | |
{ | |
if (!char.IsWhiteSpace(str[i])) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public static bool ContainsIgnoreCase(this string str, string other) | |
{ | |
return str.IndexOf(other, System.StringComparison.OrdinalIgnoreCase) >= 0; | |
} | |
} | |
} |
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; | |
using UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class TextAssetUtility | |
{ | |
private static readonly string[] newline = new string[] { Environment.NewLine }; | |
public static string[] GetLines(this TextAsset asset) | |
{ | |
return asset.text.Split(newline, StringSplitOptions.RemoveEmptyEntries); | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class TextureUtility | |
{ | |
public static float GetAspect(this Texture texture) | |
{ | |
return (float)texture.width / texture.height; | |
} | |
} | |
} |
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; | |
using UnityCommonLibrary.Time; | |
using UTime = UnityEngine.Time; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class TimeUtility | |
{ | |
public static float GetCurrentTime(TimeMode mode) | |
{ | |
switch (mode) | |
{ | |
case TimeMode.Time: | |
return UTime.time; | |
case TimeMode.UnscaledTime: | |
return UTime.unscaledTime; | |
case TimeMode.RealtimeSinceStartup: | |
return UTime.realtimeSinceStartup; | |
case TimeMode.FixedTime: | |
return UTime.fixedTime; | |
case TimeMode.DeltaTime: | |
return UTime.deltaTime; | |
case TimeMode.UnscaledDeltaTime: | |
return UTime.unscaledDeltaTime; | |
case TimeMode.SmoothDeltaTime: | |
return UTime.smoothDeltaTime; | |
case TimeMode.FixedDeltaTime: | |
return UTime.fixedDeltaTime; | |
case TimeMode.TimeSinceLevelLoad: | |
return UTime.timeSinceLevelLoad; | |
case TimeMode.One: | |
return 1f; | |
default: | |
throw new Exception("Invalid TimeMode"); | |
} | |
} | |
} | |
} |
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.Collections.Generic; | |
using UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class TransformUtility | |
{ | |
private static Queue<Transform> bfsSearchQueue = new Queue<Transform>(); | |
public static void SetPosition(this Transform transform, Space space, float? x = null, float? y = null, float? z = null) | |
{ | |
var newV3 = transform.position; | |
newV3.SetXYZ(x, y, z); | |
if(space == Space.World) | |
{ | |
transform.position = newV3; | |
} | |
else | |
{ | |
transform.localPosition = newV3; | |
} | |
} | |
public static void SetLocalScale(this Transform transform, float? x = null, float? y = null, float? z = null) | |
{ | |
var newV3 = transform.localScale; | |
newV3.SetXYZ(x, y, z); | |
transform.localScale = newV3; | |
} | |
public static void SetEulerAngles(this Transform transform, Space space, float? x = null, float? y = null, float? z = null) | |
{ | |
var newV3 = transform.eulerAngles; | |
newV3.SetXYZ(x, y, z); | |
if(space == Space.World) | |
{ | |
transform.rotation = Quaternion.Euler(newV3); | |
} | |
else | |
{ | |
transform.localRotation = Quaternion.Euler(newV3); | |
} | |
} | |
public static void Reset(this Transform t) | |
{ | |
Reset(t, TransformElement.All, Space.World); | |
} | |
public static void Reset(this Transform t, TransformElement elements) | |
{ | |
Reset(t, elements, Space.World); | |
} | |
public static void Reset(this Transform t, Space space) | |
{ | |
Reset(t, TransformElement.All, space); | |
} | |
public static void Reset(this Transform t, TransformElement elements, Space space) | |
{ | |
if((elements & TransformElement.Position) != 0) | |
{ | |
if(space == Space.World) | |
{ | |
t.position = Vector3.zero; | |
} | |
else | |
{ | |
t.localPosition = Vector3.zero; | |
} | |
} | |
if((elements & TransformElement.Rotation) != 0) | |
{ | |
if(space == Space.World) | |
{ | |
t.rotation = Quaternion.identity; | |
} | |
else | |
{ | |
t.localRotation = Quaternion.identity; | |
} | |
} | |
if((elements & TransformElement.Scale) != 0) | |
{ | |
t.localScale = Vector3.one; | |
} | |
} | |
public static void Match(this Transform t, Transform other, TransformElement elements) | |
{ | |
if((elements & TransformElement.Position) != 0) | |
{ | |
t.position = other.position; | |
} | |
if((elements & TransformElement.Rotation) != 0) | |
{ | |
t.rotation = other.rotation; | |
} | |
if((elements & TransformElement.Scale) != 0) | |
{ | |
t.localScale = other.localScale; | |
} | |
} | |
public static Transform FindChildBFS(this Transform t, string search, bool tag = false) | |
{ | |
bfsSearchQueue.Clear(); | |
for(int i = 0; i < t.childCount; i++) | |
{ | |
bfsSearchQueue.Enqueue(t.GetChild(i)); | |
} | |
Transform found = null; | |
while(bfsSearchQueue.Count > 0) | |
{ | |
var child = bfsSearchQueue.Dequeue(); | |
if((!tag && child.name == search) || (tag && child.tag == search)) | |
{ | |
found = child; | |
break; | |
} | |
for(int i = 0; i < child.childCount; i++) | |
{ | |
bfsSearchQueue.Enqueue(child.GetChild(i)); | |
} | |
} | |
bfsSearchQueue.Clear(); | |
return found; | |
} | |
public static Transform FindChildDFS(this Transform t, string search, bool tag = false) | |
{ | |
for(int i = 0; i < t.childCount; i++) | |
{ | |
var child = t.GetChild(i); | |
if((!tag && child.name == search) || (tag && child.tag == search)) | |
{ | |
return child; | |
} | |
child = FindChildDFS(t.GetChild(i), search); | |
if(child) | |
{ | |
return child; | |
} | |
} | |
return null; | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class TrigUtility | |
{ | |
public static float NormalizeAngle(float x, float a, float b) | |
{ | |
var width = b - a; | |
var offsetValue = x - a; | |
return (offsetValue - (Mathf.Floor(offsetValue / width) * width)) + a; | |
} | |
public static float NormalizeAngle(float x) | |
{ | |
return NormalizeAngle(x, 0f, 360f); | |
} | |
public static bool IsBetweenAngles(float x, float a, float b) | |
{ | |
a = NormalizeAngle(a); | |
b = NormalizeAngle(b); | |
x = NormalizeAngle(x); | |
var aB = Mathf.Abs(Mathf.DeltaAngle(a, b)); | |
if (aB == 180f) | |
{ | |
return x <= 180f; | |
} | |
var xA = Mathf.Abs(Mathf.DeltaAngle(x, a)); | |
var xB = Mathf.Abs(Mathf.DeltaAngle(x, b)); | |
return Mathf.Max(xA, xB) <= aB; | |
} | |
public static Vector2 DirectionFromAngle2D(float degrees) | |
{ | |
return new Vector2(Mathf.Cos(degrees * Mathf.Deg2Rad), Mathf.Sin(degrees * Mathf.Deg2Rad)); | |
} | |
public static Vector3 DirectionFromAngle(float degrees) | |
{ | |
return new Vector3(Mathf.Sin(degrees * Mathf.Deg2Rad), 0f, Mathf.Cos(degrees * Mathf.Deg2Rad)); | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class UIUtility | |
{ | |
public static void SetPivot(this RectTransform rectTransform, Vector2 pivot) | |
{ | |
var center = rectTransform.GetWorldCenter(); | |
rectTransform.pivot = pivot; | |
rectTransform.position += (Vector3)(center - rectTransform.GetWorldCenter()); | |
} | |
public static Vector2 GetWorldCenter(this RectTransform rectTransform) | |
{ | |
return rectTransform.TransformPoint(rectTransform.rect.center); | |
} | |
public static Rect GetWorldRect(this RectTransform rectTransform) | |
{ | |
return new Rect(rectTransform.TransformPoint(rectTransform.rect.min), rectTransform.rect.size); | |
} | |
} | |
} |
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 UnityEngine; | |
namespace UnityCommonLibrary.Utility | |
{ | |
public static class VectorUtility | |
{ | |
public static Vector3 Round(Vector3 vec) | |
{ | |
return new Vector3() | |
{ | |
x = Mathf.RoundToInt(vec.x), | |
y = Mathf.RoundToInt(vec.y), | |
z = Mathf.RoundToInt(vec.z), | |
}; | |
} | |
public static Vector2 Round(Vector2 vec) | |
{ | |
return new Vector2() | |
{ | |
x = Mathf.RoundToInt(vec.x), | |
y = Mathf.RoundToInt(vec.y) | |
}; | |
} | |
public static Vector3 RoundTo(Vector3 vec, float nearest) | |
{ | |
return new Vector3() | |
{ | |
x = MathUtility.RoundTo(vec.x, nearest), | |
y = MathUtility.RoundTo(vec.y, nearest), | |
z = MathUtility.RoundTo(vec.z, nearest) | |
}; | |
} | |
public static Vector3 RoundTo(Vector2 vec, float nearest) | |
{ | |
return new Vector2() | |
{ | |
x = MathUtility.RoundTo(vec.x, nearest), | |
y = MathUtility.RoundTo(vec.y, nearest) | |
}; | |
} | |
public static Vector2 SmoothStep(Vector2 from, Vector2 to, float t) | |
{ | |
return new Vector2() | |
{ | |
x = Mathf.SmoothStep(from.x, to.x, t), | |
y = Mathf.SmoothStep(from.y, to.y, t), | |
}; | |
} | |
public static void SetXYZ(this Vector3 v3, float? x = null, float? y = null, float? z = null) | |
{ | |
if (x.HasValue) | |
{ | |
v3.x = x.Value; | |
} | |
if (y.HasValue) | |
{ | |
v3.y = y.Value; | |
} | |
if (z.HasValue) | |
{ | |
v3.z = z.Value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment