Skip to content

Instantly share code, notes, and snippets.

@dogfuntom
Last active February 6, 2022 12:30
Show Gist options
  • Save dogfuntom/322ac77b88cf8c6fd84b to your computer and use it in GitHub Desktop.
Save dogfuntom/322ac77b88cf8c6fd84b to your computer and use it in GitHub Desktop.
NOTE that this gist is old (2015). Some stuff is obsolete. Some code I don't find wise to use anymore.
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using System.Reflection;
using System.Linq;
#endif
public static class Extensions
{
#region float
public static float ToUnsignedAngle(this float degrees)
{
return degrees >= 0 ? degrees : 360 + degrees;
}
public static bool IsCloseToZero(this float value, float epsilon)
{
if (epsilon <= 0)
throw new ArgumentException("Epsilon should be greater then 0.", "epsilon");
return Math.Abs(value) < epsilon;
}
public static bool IsCloseToZero(this float value)
{
return value.IsCloseToZero(1e-6f);
}
#endregion
public static string Format(this string format, params object[] args)
{
return string.Format(format, args);
}
public static bool IsNullOrWhitespace(this string self)
{
return (self == null || self.Trim() == String.Empty);
}
public static void AddRange<T>(this IList<T> @this, params T[] items)
{
for (int i = 0; i < items.Length; i++)
{
@this.Add(items[i]);
}
}
#if UNITY_EDITOR
public static bool HasAttribute<T>(this MemberInfo @this)
where T : Attribute
{
return @this.HasAttribute(typeof(T));
}
public static bool HasAttribute(this MemberInfo @this, Type attributeType)
{
return @this.GetCustomAttributes(attributeType, true).Any();
}
#endif
}
using System.Collections.Generic;
using UnityEngine;
namespace System.Linq
{
public static class LinqExtensions
{
public static Vector2 Sum(this IEnumerable<Vector2> source)
{
return source.Aggregate((x, y) => x + y);
}
public static Vector2 Sum<T>(this IEnumerable<T> source, Func<T, Vector2> selector)
{
return source.Select(selector).Aggregate((x, y) => x + y);
}
public static bool IsNullOrEmpty<T>(this IEnumerable<T> @self)
{
return @self == null || !@self.Any();
}
public static bool IsNullOrEmpty<T>(this ICollection<T> @self)
{
return @self == null || @self.Count == 0;
}
public static bool IsNullOrEmptyOrContainsNull<T>(this ICollection<T> @self)
where T : class
{
if (@self == null)
return true;
return @self.Contains(null);
}
}
}
#if UNITY_EDITOR
using System.Reflection;
#endif
namespace UnityEngine
{
using System;
using System.Collections.Generic;
public static class UnityEngineExtensions
{
#region Single
public static Vector2 ToVector2(this float value)
{
return new Vector2(value, value);
}
public static float RadiansToDegrees(this float radians)
{
return radians * Mathf.Rad2Deg;
}
public static float DegreesToRadians(this float degrees)
{
return degrees * Mathf.Deg2Rad;
}
#endregion
#region Transform
public static float Get2DLocalRotation(this Transform @this)
{
return @this.localRotation.eulerAngles.z;
}
public static float Get2DRotation(this Transform @this)
{
return @this.rotation.eulerAngles.z;
}
/// <summary>
/// Sets local rotation around Z axis.
/// </summary>
/// <param name="degrees">Rotation in degrees.</param>
public static void Set2DLocalRotation(this Transform @this, float degrees)
{
var euler = @this.localRotation.eulerAngles;
@this.localRotation = Quaternion.Euler(euler.x, euler.y, degrees);
}
/// <summary>
/// Sets global rotation around Z axis.
/// </summary>
/// <param name="degrees">Rotation in degrees.</param>
public static void Set2DRotation(this Transform @this, float degrees)
{
var euler = @this.rotation.eulerAngles;
@this.rotation = Quaternion.Euler(euler.x, euler.y, degrees);
}
public static void Set2DPosition(this Transform @this, Vector2 position)
{
@this.position = new Vector3(
position.x,
position.y,
@this.position.z);
}
public static void Set2DLocalPosition(this Transform @this, Vector2 position)
{
@this.localPosition = new Vector3(
position.x,
position.y,
@this.localPosition.z);
}
public static void Set2DLocalScale(this Transform @this, float uniform)
{
@this.localScale = new Vector2(uniform, uniform);
}
public static void IfNullFindChild(this Transform @this, ref Transform value, string name)
{
if (value == null)
value = @this.Find(name);
}
public static IEnumerable<Transform> GetChilds(this Transform @self)
{
var childs = new List<Transform>(@self.childCount);
foreach (Transform child in @self)
{
childs.Add(child);
}
return childs;
}
#endregion
#region Camera
public static Vector2 ScreenToWorldPoint2D(this Camera @this, Vector2 position)
{
return (Vector2)@this.ScreenToWorldPoint(new Vector3(position.x, position.y, 10));
}
public static Vector2 GetMouseWorldPosition(this Camera @this)
{
var mousePosition = Input.mousePosition;
mousePosition.z = 10;
return (Vector2)@this.ScreenToWorldPoint(mousePosition);
}
#endregion
#region Rigidbody2D
public static void AddForce(this Rigidbody2D rigidbody2D, Vector2 force, ForceMode mode = ForceMode.Force)
{
switch (mode)
{
case ForceMode.Force:
rigidbody2D.AddForce(force);
break;
case ForceMode.Impulse:
rigidbody2D.AddForce(force / Time.fixedDeltaTime);
break;
case ForceMode.Acceleration:
rigidbody2D.AddForce(force * rigidbody2D.mass);
break;
case ForceMode.VelocityChange:
rigidbody2D.AddForce(force * rigidbody2D.mass / Time.fixedDeltaTime);
break;
}
}
public static void AddForce(this Rigidbody2D rigidbody2D, float x, float y, ForceMode mode = ForceMode.Force)
{
rigidbody2D.AddForce(new Vector2(x, y), mode);
}
#endregion
#region Vector2
public static Vector2 ProjectOn(this Vector2 self, Vector2 onNormal)
{
return (Vector2)Vector3.Project(self, onNormal);
}
public static Vector2 GetLeftPerpendicular(this Vector2 @this)
{
return new Vector2(-@this.y, @this.x);
}
public static Vector2 Do(this Vector2 @this, Func<float, float> func)
{
return new Vector2(func(@this.x), func(@this.y));
}
public static Vector3 ToXZ(this Vector2 @this)
{
return new Vector3(
@this.x,
0,
@this.y);
}
#endregion
#region Components
/// <summary>
/// Gets or add a component. Usage example:
/// BoxCollider boxCollider = transform.GetOrAddComponent@lt;BoxCollider@gt;();
/// </summary>
public static T GetOrAddComponent<T>(this Component child) where T : Component
{
T result = child.GetComponent<T>();
if (result == null)
{
result = child.gameObject.AddComponent<T>();
}
return result;
}
public static IList<T> ExceptDisabled<T>(this T[] @this) where T : Behaviour
{
var result = new List<T>(@this.Length);
for (int i = 0; i < @this.Length; i++)
{
var component = @this[i];
if (component.enabled)
result.Add(component);
}
return result;
}
#endregion
public static void ResetLocalTransform(this GameObject @this, Component parent, Vector3 position)
{
var transform = @this.transform;
transform.parent = parent.transform;
transform.localPosition = position;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
}
public static bool HasComponent<T>(this GameObject @this) where T : Component
{
return @this.GetComponent<T>() != null;
}
#if UNITY_EDITOR
public static bool IsVisibleForInspector(this FieldInfo @this)
{
if (!@this.DeclaringType.IsSubclassOf(typeof(Behaviour)))
return false;
if (@this.IsPublic && !@this.HasAttribute<HideInInspector>())
return true;
if (@this.HasAttribute<SerializeField>())
return true;
return false;
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment