Utilities to improve the C# experience in Unity: ranging from more capabilities to shorter code length!
For now, to use this library, you need to copy the repo (or download it) and build a .dll file in Visual Studio,
Jetbrains Rider or any .NET IDE, to then add it to your Unity project! Alternatively, you can also copy all the scripts
in your project :) Don't forget to add the UnityUtilities
namespace!
You can now install this as an UPM packge through OpenUPM! Here's the package page :D
Just open a terminal within your project and type:
openupm add com.amp.unityutilities
or, manually install it through the package manager like this!
Currently frozen at #8.0 for backwards compability
with Unity 2020.3
and above!
A series of popular programming patterns, implemented in easy to use classes!
Through the controversy of using them, here are generic, base Singleton classes to get you up and going if you need them!
Since Unity is singlethreaded, none of the following are thread safe!
Singleton
→ Generic singleton class, made for MonoBehaviours.SingletonPersistent
→ Generic persistent singleton class, made for MonoBehaviours. Persists through scene reloads.SingletonLazy
→ Generic lazy singleton class, made for MonoBehaviours. Creates itself, if it doesn't exist.SingletonService
→ A newer kind of a C# (doesn't interact with Unity) Singleton, only accessible through method calls. Mainly used for services.
WaitUntilAndForSecondsRealtime
→ Suspends the coroutine execution for the given amount of seconds using unscaled time and until the passed condition is true.WaitUntilOrForSecondsRealtime
→ Suspends the coroutine execution for the given amount of seconds using unscaled time or until the passed condition is true.WaitWhileAndForSecondsRealtime
→ Suspends the coroutine execution for the given amount of seconds using unscaled time and while the passed condition is true.WaitWhileOrForSecondsRealtime
→ Suspends the coroutine execution for the given amount of seconds using unscaled time or while the passed condition is true.
Round
→ Rounds float and returns float; number of decimals can be specified.RoundToInt
→ Rounds float and returns int.Abs
→ Returns the absolute value (of ints and floats).Clamp
→ Clamps float or int to the supplied range.Map
→ Maps a float from the supplied range to the supplied range (for floats and ints).
GoUp
→ Smoothly translates the gameobject (or transform) up at the desired speed (float).GoDown
→ Smoothly translates the gameobject (or transform) down at the desired speed (float).GoLeft
→ Smoothly translates the gameobject (or transform) left at the desired speed (float).GoRight
→ Smoothly translates the gameobject (or transform) right at the desired speed (float).GoForward
→ Smoothly translates the gameobject (or transform) forward at the desired speed (float).GoBackward
→ Smoothly translates the gameobject (or transform) backward at the desired speed (float).FollowMouse2D
→ Smoothly makes the Rigidbody2D follow the mouse position at the desired speeds (float, float).FollowObject2D
→ Smoothly makes the Rigidbody2D follow the specified object (or transform) at the desired speeds (float, float);
Round
→ Rounds Vector2, Vector3 and returns a rounded vector with the specified number of decimals.RoundToInt
→ Rounds Vector2, Vector3 and returns Vector2Int.Clamp
→ Clamps the components of the Vector3 between supplied values.VectorFromAngle
→ Returns the corresponding vector to the given angle (in degrees or radians).RotateByAngle
→ Rotates the Vector2 by the given angle (in degress or radians). This method assumes +X axis is 0 degrees/radians.AngleFromVector
→ Returns the positive angle in degrees (or radians) of given Vector2. This method assumes +X axis is 0 degrees/radians.TransformTo2DVector3
→ Transforms Vector2 into Vector3 respecting Unity's coordinate system (so x = x, y = 0, z = y).ChopTo2DVector3
→ Takes and returns a Vector3 respecting Unity's coordinate system (so x = x, y = 0 (or specified), z = y).ToVector2
→ Transforms a 2-big float array to a Vector2.ToVector3
→ Transforms a 3-big float array to a Vector3.ToArray
→ Transforms a Vector3 or Vector2 to a 3-big or 2-big float array, respectively.GeoDistanceTo
→ Calculates distance between two coordinates.
AverageQuaternions
→ Returns the average of the Quaternions.AverageQuaternionsFast
→ Returns the average of the Quaternions, without normalizing to save on speed.Normalize
→ Normalizes the Quaternion.Inverse
→ Inverses the Quaternion.InverseSign
→ Inverses the sign of the Quaternion.IsClose
→ Checks if the Quaternion is close to the given one.Scale
→ Scales the Quaternion.
Print
→ Cleaner way of Debug.Log() with log type supportDelayAction
→ Delays supplied action by specified number of seconds (float).IsAnimatorPlaying
→ Checks if the animator is currently playing an animation.Destroy
→ More elegant way of writing Destroy(gameObject).DestroyImmediate
→ More elegant way of writing DestroyImmediate(gameObject).
-
Log
→ Logs a message to the console with the object's name. -
LogWarning
→ Logs a warning to the console with the object's name. -
LogError
→ Logs an error to the console with the object's name. -
static Log
→ Logs a message to the console with the passed object's name. -
static LogWarning
→ Logs a warning to the console with the passed object's name. -
static LogError
→ Logs an error to the console with the passed object's name.
Flip
→ Flips a 2-big float array, so that the values swap indexes.
- Fixed
CustomBehaviour
logging methods.
- Duplicated
CustomBehaviour
logging methods and made them static (for non-MonoBehaviours).
- Made
CustomBehaviour
logging methods public.
- Added
Inverse
method toQuaternion
extensions. - Added
CustomBehaviour
class, extending the basic MonoBehaviour.-
Added
Log
-
Added
LogWarning
-
Added
LogError
-
Changed all
Singleton
classes extendingCustomBehaviour
classes.
-
- Fixed namespace for
Coroutines
extensions - Added
Quaternion
extension methods, more specifally:AverageQuaternions
AverageQuaternionsFast
Normalize
InverseSign
IsClose
Scale
- Fixed
Coroutine
-related classes - Will implement testing soon to avoid fixes...
- Moved
DelayAction
to its own file
- Added the following
Coroutine
-related classes:WaitUntilAndForSecondsRealtime
WaitUntilOrForSecondsRealtime
WaitWhileAndForSecondsRealtime
WaitWhileOrForSecondsRealtime
- Fixed all
Singleton
implementations.
- Fixed package structure!
- Added
ToVector3
to transform a 3-big float array to a Vector3. - Added
Flip
to flip a 2-big float array, so that the values swap indexes. - Added
ToArray
to transform a Vector3 or Vector2 to a 3-big or 2-big float array, respectively. - Some refactoring!
- Submitted package to open upm: it's now approved!
- Added required
.meta
files!
- Submitted package to open upm!
- Some refactoring too!
Long time no see... Sorry, about that!
- Renamed files for a clearer project
- Added
IsAnimatorPlaying
to check whether an Animator is currently playing an Animation - Added Patterns folder for useful programming patterns!
- Started setup for upm hosting!
- Added
MiscellaneousExtensions
for... miscellaneous extensions likeGeoDistance
! - Moved miscellaneous section into its own script ^^!
- Removed
ref
keyword (accident, sorry!) - Changed all namespaces to
UnityUtilities
- Added
ToVector2
to transform float arrays (of size 2) into a vector 2 - Added
GeoDistanceTo
to calculate distance between two geocoordinates
- Removed
ValueType
method - Changed all angle related methods to a single one for each use case, i.e. you now have to specify what unit of measure you are using :)
- All of the
GoUp
,GoDown
, etc. methods can now be directly used for Transforms as well - Separated each extension-related methods in their respective files (Numerals, GameObject, Vector, Unity (= Misc.))
- Removed
double
methods as it's rarely used in Unity
Will probably remove the ValueType function as it can be used with a variety of things and can therefore introduce bugs 😅
- Added
DelayAction
- Added
DestroyImmediate
- Added
Clamp
(for vectors and floats) - Added
TransformTo2DVector3
- Added
ChopTo2DVector3
- Added
Map
- Moved all
CsExtensions.cs
functions into UnityExtensions as this is a Unity only extension "pack" Round
now returns Vector3Int and Vector2Int when paramaterless- Simplified use
FollowMouse2D
andFollowObject2D
- Replaced specific value types for
var
- Added
RoundToInt
- Added
GoUp
- Added
GoDown
- Added
GoLeft
- Added
GoRight
- Added
GoForward
- Added
GoBackward
- Added
FollowMouse
- Added
FollowObject2D
- Expanded
Print
- Cleaned up README
- Changed solution and project name
- Added some instructions to the README =D