Skip to content

Utilities to improve the C# experience in Unity: ranging from extension methods to shorter code length.

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

AugustDG/Unity-Utilities

Repository files navigation

Unity Utilities

openupm

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!

OpenUPM

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!

C# Version

Currently frozen at #8.0 for backwards compability with Unity 2020.3 and above!

Patterns

A series of popular programming patterns, implemented in easy to use classes!

Singletons

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.

Coroutines

  • 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.

Extensions

Numerals

  • 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).

Movement

  • 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);

Vectors

  • 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.

Quaternions

  • 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.

GameObject

  • Print → Cleaner way of Debug.Log() with log type support
  • DelayAction → 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).

CustomBehaviour

  • 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.

Miscelleanous

  • Flip → Flips a 2-big float array, so that the values swap indexes.

Changelog

v0.4.4

  • Fixed CustomBehaviour logging methods.

v0.4.3

  • Duplicated CustomBehaviour logging methods and made them static (for non-MonoBehaviours).

v0.4.2

  • Made CustomBehaviour logging methods public.

v0.4.1

  • Added Inverse method to Quaternion extensions.
  • Added CustomBehaviour class, extending the basic MonoBehaviour.
    • Added Log

    • Added LogWarning

    • Added LogError

    • Changed all Singleton classes extending CustomBehaviour classes.

v0.4.0

  • Fixed namespace for Coroutines extensions
  • Added Quaternion extension methods, more specifally:
    • AverageQuaternions
    • AverageQuaternionsFast
    • Normalize
    • InverseSign
    • IsClose
    • Scale

v0.3.2

  • Fixed Coroutine-related classes
  • Will implement testing soon to avoid fixes...

v0.3.1

  • Moved DelayAction to its own file

v0.3.0

  • Added the following Coroutine-related classes:
    • WaitUntilAndForSecondsRealtime
    • WaitUntilOrForSecondsRealtime
    • WaitWhileAndForSecondsRealtime
    • WaitWhileOrForSecondsRealtime

v0.2.0

  • Fixed all Singleton implementations.

v0.1.9

  • Fixed package structure!

v0.1.5

  • 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!

v0.1.4

  • Submitted package to open upm: it's now approved!
  • Added required .meta files!

v0.1.3

  • Submitted package to open upm!
  • Some refactoring too!

v0.1.2

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!

11-06-2021

  • Added MiscellaneousExtensions for... miscellaneous extensions like GeoDistance!
  • Moved miscellaneous section into its own script ^^!

19-05-2021

  • 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

26-04-2021

  • 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

25-04-2021

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 and FollowObject2D
  • Replaced specific value types for var

1-01-2021

  • Added RoundToInt
  • Added GoUp
  • Added GoDown
  • Added GoLeft
  • Added GoRight
  • Added GoForward
  • Added GoBackward
  • Added FollowMouse
  • Added FollowObject2D
  • Expanded Print
  • Cleaned up README

1-12-2020

  • Changed solution and project name
  • Added some instructions to the README =D

About

Utilities to improve the C# experience in Unity: ranging from extension methods to shorter code length.

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

No packages published

Languages