Skip to content

API.Unity.Util Extensions

JuDelCo edited this page Mar 8, 2021 · 28 revisions

Important

Add first using Ju.Extensions; so you can use these extension methods.

Behaviour Extensions

You can use these on all classes inherited from Behaviour like MonoBehaviour or UIBehaviour.

Vector3 GetPosition();
Vector3 GetLocalPosition();
Quaternion GetRotation();
Quaternion GetLocalRotation();
Vector3 GetScale();

void EventSubscribe<T>(Action<T> action, int priority = 0, bool alwaysActive = false);
void EventSubscribe<T>(Action action, int priority = 0, bool alwaysActive = false);
void EventSubscribe<T>(Action<T> action, Func<T, bool> filter, int priority = 0, bool alwaysActive = false);
void EventSubscribe<T>(Action action, Func<T, bool> filter, int priority = 0, bool alwaysActive = false);
void EventSubscribe<T>(byte channel, Action<T> action, int priority = 0, bool alwaysActive = false);
void EventSubscribe<T>(byte channel, Action action, int priority = 0, bool alwaysActive = false);
void EventSubscribe<T>(byte channel, Action<T> action, Func<T, bool> filter, int priority = 0, bool alwaysActive = false);
void EventSubscribe<T>(byte channel, Action action, Func<T, bool> filter, int priority = 0, bool alwaysActive = false);

Coroutine CoroutineStart(IEnumerator routine, bool alwaysActive = true);

IPromise WaitUntil(Func<bool> condition, bool alwaysActive = false);
IPromise WaitWhile(Func<bool> condition, bool alwaysActive = false);
IPromise WaitForSeconds<T>(float seconds, bool alwaysActive = false) where T : ILoopTimeEvent;
IPromise WaitForSeconds(float seconds, bool alwaysActive = false);
IPromise WaitForTicks<T>(int ticks, bool alwaysActive = false) where T : ILoopEvent;
IPromise WaitForNextUpdate(bool alwaysActive = false);
IPromise WaitForNextFixedUpdate(bool alwaysActive = false);

IClock NewClock<T>(bool alwaysActive = false) where T : ILoopTimeEvent;
IClock NewClock<T>(float elapsedSeconds, bool alwaysActive = false) where T : ILoopTimeEvent;
ITimer NewTimer<T>(float seconds, Action onCompleted, bool alwaysActive = false) where T : ILoopTimeEvent;
IFrameTimer NewFrameTimer<T>(int frames, Action onCompleted, bool alwaysActive = false) where T : ILoopEvent;

LayerMask Extensions

// For GameObject, Collision and Collider classes
bool IsInLayerMask(LayerMask layerMask);

Color & Color32

Allows implicit and explicit conversion of Color & Color32 structs to and from the Unity ones.

Observable Extensions

This method will use a BehaviourLinkHandler to subscribe the Behaviour to the Observable.

void Subscribe<T>(Behaviour actor, Action<T> action, bool alwaysActive = false);

Random Extensions

Vector2Int Vector2i(Vector2Int min, Vector2Int max);
Vector2 Direction2D();
Vector2 Vector2f();
Vector2 Vector2f(Vector2 min, Vector2 max);

Vector3Int Vector3i(Vector3Int min, Vector3Int max);
Vector3 Direction3D();
Vector3 Vector3f();
Vector3 Vector3f(Vector3 min, Vector3 max);

Vector4 Vector4f();
Vector4 Vector4f(Vector4 min, Vector4 max);

Quaternion DirectionQuat();
Quaternion Quat();
Clone this wiki locally