UnityUtilities
|
Provides a lot of convenience methods for handling Unity data types, either in the form of extension methods or as static helper methods. More...
Static Public Member Functions | |
static void | CopyPositionAndRotatationFrom (this Transform transform, Transform source) |
Copies the position and rotation from another transform to this transform. More... | |
static Transform | SetPosition (this Transform transform, float? x=null, float? y=null, float? z=null) |
Sets the x/y/z transform.position using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetPosition(x: 5, z: 10), for example, only changing transform.position.x and z. More... | |
static Transform | SetLocalPosition (this Transform transform, float? x=null, float? y=null, float? z=null) |
Sets the x/y/z transform.localPosition using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLocalPosition(x: 5, z: 10), for example, only changing transform.localPosition.x and z. More... | |
static Transform | SetLocalScale (this Transform transform, float? x=null, float? y=null, float? z=null) |
Sets the x/y/z transform.localScale using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLocalScale(x: 5, z: 10), for example, only changing transform.localScale.x and z. More... | |
static Transform | SetLossyScale (this Transform transform, float? x=null, float? y=null, float? z=null) |
Sets the x/y/z transform.lossyScale using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLossyScale(x: 5, z: 10), for example, only changing transform.lossyScale.x and z. More... | |
static Transform | SetEulerAngles (this Transform transform, float? x=null, float? y=null, float? z=null) |
Sets the x/y/z transform.eulerAngles using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetEulerAngles(x: 5, z: 10), for example, only changing transform.eulerAngles.x and z. More... | |
static Transform | SetLocalEulerAngles (this Transform transform, float? x=null, float? y=null, float? z=null) |
Sets the x/y/z transform.localEulerAngles using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLocalEulerAngles(x: 5, z: 10), for example, only changing transform.localEulerAngles.x and z. More... | |
static void | AssignLayerToHierarchy (this GameObject gameObject, int layer) |
Assigns a layer to this GameObject and all its children recursively. More... | |
static void | StripCloneFromName (this GameObject gameObject) |
When UnityEngine.Object.Instantiate(UnityEngine.Object) is called on a prefab named "Original", the resulting instance will be named "Original(Clone)". This method changes the name back to "Original" by stripping everything after and including the first "(Clone)" it finds. If no "(Clone)" is found, the name is left unchanged. More... | |
static string | GetNameWithoutClone (this GameObject gameObject) |
When UnityEngine.Object.Instantiate(UnityEngine.Object) is called on a prefab named "Original", the resulting instance will be named "Original(Clone)". This method returns the name without "(Clone)" by stripping everything after and including the first "(Clone)" it finds. If no "(Clone)" is found, the name is returned unchanged. More... | |
static Vector2 | Change2 (this Vector2 vector, float? x=null, float? y=null) |
Makes a copy of the Vector2 with changed x/y values, keeping all undefined values as they were before. Can be called with named parameters like vector.Change2(y: 5), for example, only changing the y component. More... | |
static Vector3 | Change3 (this Vector3 vector, float? x=null, float? y=null, float? z=null) |
Makes a copy of the Vector3 with changed x/y/z values, keeping all undefined values as they were before. Can be called with named parameters like vector.Change3(x: 5, z: 10), for example, only changing the x and z components. More... | |
static Vector4 | Change4 (this Vector4 vector, float? x=null, float? y=null, float? z=null, float? w=null) |
Makes a copy of the Vector4 with changed x/y/z/w values, keeping all undefined values as they were before. Can be called with named parameters like vector.Change4(x: 5, z: 10), for example, only changing the x and z components. More... | |
static Vector2 | RotateRad (this Vector2 v, float angleRad) |
Rotates a Vector2. More... | |
static Vector2 | RotateDeg (this Vector2 v, float angleDeg) |
Rotates a Vector2. More... | |
static Vector2 | CreateVector2AngleRad (float angleRad) |
Creates a Vector2 with a length of 1 pointing towards a certain angle. More... | |
static Vector2 | CreateVector2AngleDeg (float angleDeg) |
Creates a Vector2 with a length of 1 pointing towards a certain angle. More... | |
static float | GetAngleRad (this Vector2 vector) |
Gets the rotation of a Vector2. More... | |
static float | GetAngleDeg (this Vector2 vector) |
Gets the rotation of a Vector2. More... | |
static Vector2 | EasedLerpVector2 (Vector2 current, Vector2 target, float percentPerSecond, float deltaTime=0f) |
Framerate-independent eased lerping to a target value, slowing down the closer it is. More... | |
static Vector3 | EasedLerpVector3 (Vector3 current, Vector3 target, float percentPerSecond, float deltaTime=0f) |
Framerate-independent eased lerping to a target value, slowing down the closer it is. More... | |
static Vector4 | EasedLerpVector4 (Vector4 current, Vector4 target, float percentPerSecond, float deltaTime=0f) |
Framerate-independent eased lerping to a target value, slowing down the closer it is. More... | |
static Color | Change (this Color color, float? r=null, float? g=null, float? b=null, float? a=null) |
Makes a copy of the Color with changed r/g/b/a values, keeping all undefined values as they were before. Can be called with named parameters like color.Change(g: 0, a: 0.5), for example, only changing the g and a components. More... | |
static Color | ChangeAlpha (this Color color, float a) |
Makes a copy of the vector with a changed alpha value. More... | |
static Color | EasedLerpColor (Color current, Color target, float percentPerSecond, float deltaTime=0f) |
Framerate-independent eased lerping to a target value, slowing down the closer it is. More... | |
static Vector2 | CalculateCentroid (this Vector2[] array) |
Calculates the average position of an array of Vector2. More... | |
static Vector3 | CalculateCentroid (this Vector3[] array) |
Calculates the average position of an array of Vector3. More... | |
static Vector4 | CalculateCentroid (this Vector4[] array) |
Calculates the average position of an array of Vector4. More... | |
static Vector2 | CalculateCentroid (this List< Vector2 > list) |
Calculates the average position of a List of Vector2. More... | |
static Vector3 | CalculateCentroid (this List< Vector3 > list) |
Calculates the average position of a List of Vector3. More... | |
static Vector4 | CalculateCentroid (this List< Vector4 > list) |
Calculates the average position of a List of Vector4. More... | |
static Vector2 | RandomPosition (this Rect rect, float extendDistance=0f) |
Extends/shrinks the rect by extendDistance to each side and gets a random position from the resulting rect. More... | |
static Rect | RandomSubRect (this Rect rect, float width, float height) |
Gets a random subrect of the given width or height inside this rect. More... | |
static Vector2 | Clamp2 (this Rect rect, Vector2 position, float extendDistance=0f) |
Extends/shrinks the rect by extendDistance to each side and then restricts the given vector to the resulting rect. More... | |
static Vector3 | Clamp3 (this Rect rect, Vector3 position, float extendDistance=0f) |
Extends/shrinks the rect by extendDistance to each side and then restricts the given vector to the resulting rect. The z component is kept. More... | |
static Rect | Extend (this Rect rect, float extendDistance) |
Extends/shrinks the rect by extendDistance to each side. More... | |
static bool | Contains (this Rect rect, Vector2 position, float extendDistance) |
Extends/shrinks the rect by extendDistance to each side and then checks if a given point is inside the resulting rect. More... | |
static Vector2 [] | GetCornerPoints (this Rect rect) |
Creates an array containing the four corner points of a Rect. More... | |
static bool | PlayerPrefsGetBool (string key, bool defaultValue=false) |
Returns the value corresponding to the key in the preference file if it exists. If it doesn't exist, it will return defaultValue. (Internally, the value is stored as an int with either 0 or 1.) More... | |
static void | PlayerPrefsSetBool (string key, bool value) |
Sets the value of the preference entry identified by the key. (Internally, the value is stored as an int with either 0 or 1.) More... | |
static bool | ContainsLayer (this LayerMask mask, int layer) |
Is a specific layer actived in the given LayerMask? More... | |
static Bounds | CombineColliderBounds (Collider[] colliders) |
Creates a Bounds encapsulating all given colliders bounds. More... | |
static void | GetCapsuleCastData (CharacterController characterController, Vector3 origin, out Vector3 point1, out Vector3 point2, out float radius) |
Given a CharacterController and a point of origin (the lower point of the capsule), this returns the point1, point2 and radius needed to fill a CapsuleCast(). More... | |
static Vector2 | CalculateViewportWorldSizeAtDistance (this Camera camera, float distance, float aspectRatio=0) |
Calculates the size of the viewport at a given distance from a perspective camera. More... | |
Properties | |
static Vector2 | RandomOnUnitCircle [get] |
Gets a random Vector2 of length 1 pointing in a random direction. More... | |
static int | RandomSign [get] |
Returns -1 or 1 with equal change. More... | |
static bool | RandomBool [get] |
Returns true or false with equal chance. More... | |
Provides a lot of convenience methods for handling Unity data types, either in the form of extension methods or as static helper methods.
|
static |
Assigns a layer to this GameObject and all its children recursively.
gameObject | The GameObject to start at. |
layer | The layer to set. |
|
static |
Calculates the average position of an array of Vector2.
array | The input array. |
|
static |
Calculates the average position of an array of Vector3.
array | The input array. |
|
static |
Calculates the average position of an array of Vector4.
array | The input array. |
|
static |
Calculates the average position of a List of Vector2.
list | The input list. |
|
static |
Calculates the average position of a List of Vector3.
list | The input list. |
|
static |
Calculates the average position of a List of Vector4.
list | The input list. |
|
static |
Calculates the size of the viewport at a given distance from a perspective camera.
camera | The Camera. |
distance | The positive distance from the camera. |
aspectRatio | Optionally: An aspect ratio to use. If 0 is set, camera.aspect is used. |
|
static |
Makes a copy of the Color with changed r/g/b/a values, keeping all undefined values as they were before. Can be called with named parameters like color.Change(g: 0, a: 0.5), for example, only changing the g and a components.
color | The Color to be copied with changed values. |
r | If this is not null, the r component is set to this value. |
g | If this is not null, the g component is set to this value. |
b | If this is not null, the b component is set to this value. |
a | If this is not null, the a component is set to this value. |
|
static |
Makes a copy of the Vector2 with changed x/y values, keeping all undefined values as they were before. Can be called with named parameters like vector.Change2(y: 5), for example, only changing the y component.
vector | The Vector2 to be copied with changed values. |
x | If this is not null, the x component is set to this value. |
y | If this is not null, the y component is set to this value. |
|
static |
Makes a copy of the Vector3 with changed x/y/z values, keeping all undefined values as they were before. Can be called with named parameters like vector.Change3(x: 5, z: 10), for example, only changing the x and z components.
vector | The Vector3 to be copied with changed values. |
x | If this is not null, the x component is set to this value. |
y | If this is not null, the y component is set to this value. |
z | If this is not null, the z component is set to this value. |
|
static |
Makes a copy of the Vector4 with changed x/y/z/w values, keeping all undefined values as they were before. Can be called with named parameters like vector.Change4(x: 5, z: 10), for example, only changing the x and z components.
vector | The Vector4 to be copied with changed values. |
x | If this is not null, the x component is set to this value. |
y | If this is not null, the y component is set to this value. |
z | If this is not null, the z component is set to this value. |
w | If this is not null, the w component is set to this value. |
|
static |
Makes a copy of the vector with a changed alpha value.
color | The Color to copy. |
a | The new a component. |
|
static |
Extends/shrinks the rect by extendDistance to each side and then restricts the given vector to the resulting rect.
rect | The Rect. |
position | A position that should be restricted to the rect. |
extendDistance | The distance to extend/shrink the rect to each side. |
|
static |
Extends/shrinks the rect by extendDistance to each side and then restricts the given vector to the resulting rect. The z component is kept.
rect | The Rect. |
position | A position that should be restricted to the rect. |
extendDistance | The distance to extend/shrink the rect to each side. |
|
static |
Creates a Bounds encapsulating all given colliders bounds.
colliders | The colliders. |
|
static |
Extends/shrinks the rect by extendDistance to each side and then checks if a given point is inside the resulting rect.
rect | The Rect. |
position | A position that should be restricted to the rect. |
extendDistance | The distance to extend/shrink the rect to each side. |
|
static |
Is a specific layer actived in the given LayerMask?
mask | The LayerMask. |
layer | The layer to check for. |
|
static |
Copies the position and rotation from another transform to this transform.
transform | The transform to set the position/rotation at. |
source | The transform to take the position/rotation from. |
|
static |
Creates a Vector2 with a length of 1 pointing towards a certain angle.
angleDeg | The angle in degrees. |
|
static |
Creates a Vector2 with a length of 1 pointing towards a certain angle.
angleRad | The angle in radians. |
|
static |
Framerate-independent eased lerping to a target value, slowing down the closer it is.
If you call
currentValue = UnityHelper.EasedLerpVector3(currentValue, Color.white, 0.75f);
each frame (e.g. in Update()), starting with a currentValue of Color.black, then after 1 second it will be approximately (0.75|0.75|0.75) - which is 75% of the way between Color.white and Color.black.
Adjusting the target or the percentPerSecond between calls is also possible.
current | The current value. |
target | The target value. |
percentPerSecond | How much of the distance between current and target should be covered per second? |
deltaTime | How much time passed since the last call. |
|
static |
Framerate-independent eased lerping to a target value, slowing down the closer it is.
If you call
currentValue = UnityHelper.EasedLerpVector3(currentValue, Vector2.one, 0.75f);
each frame (e.g. in Update()), starting with a currentValue of Vector2.zero, then after 1 second it will be approximately (0.75|0.75) - which is 75% of the way between Vector2.zero and Vector2.one.
Adjusting the target or the percentPerSecond between calls is also possible.
current | The current value. |
target | The target value. |
percentPerSecond | How much of the distance between current and target should be covered per second? |
deltaTime | How much time passed since the last call. |
|
static |
Framerate-independent eased lerping to a target value, slowing down the closer it is.
If you call
currentValue = UnityHelper.EasedLerpVector3(currentValue, Vector3.one, 0.75f);
each frame (e.g. in Update()), starting with a currentValue of Vector3.zero, then after 1 second it will be approximately (0.75|0.75|0.75) - which is 75% of the way between Vector3.zero and Vector3.one.
Adjusting the target or the percentPerSecond between calls is also possible.
current | The current value. |
target | The target value. |
percentPerSecond | How much of the distance between current and target should be covered per second? |
deltaTime | How much time passed since the last call. |
|
static |
Framerate-independent eased lerping to a target value, slowing down the closer it is.
If you call
currentValue = UnityHelper.EasedLerpVector4(currentValue, Vector4.one, 0.75f);
each frame (e.g. in Update()), starting with a currentValue of Vector4.zero, then after 1 second it will be approximately (0.75|0.75|0.75) - which is 75% of the way between Vector4.zero and Vector4.one.
Adjusting the target or the percentPerSecond between calls is also possible.
current | The current value. |
target | The target value. |
percentPerSecond | How much of the distance between current and target should be covered per second? |
deltaTime | How much time passed since the last call. |
|
static |
Extends/shrinks the rect by extendDistance to each side.
rect | The Rect. |
extendDistance | The distance to extend/shrink the rect to each side. |
|
static |
Gets the rotation of a Vector2.
vector | The Vector2. |
|
static |
Gets the rotation of a Vector2.
vector | The Vector2. |
|
static |
Given a CharacterController and a point of origin (the lower point of the capsule), this returns the point1, point2 and radius needed to fill a CapsuleCast().
characterController | The CharacterController to use as the capsule, providing scale, radius, height and center offset. |
origin | The capsule cast starting point at the lower end of the capsule. |
point1 | Outputs the point1 parameter to be used in the CapsuleCast() |
point2 | Outputs the point2 parameter to be used in the CapsuleCast() |
radius | Outputs the radius parameter to be used in the CapsuleCast() |
|
static |
Creates an array containing the four corner points of a Rect.
rect | The Rect. |
|
static |
When UnityEngine.Object.Instantiate(UnityEngine.Object) is called on a prefab named "Original", the resulting instance will be named "Original(Clone)". This method returns the name without "(Clone)" by stripping everything after and including the first "(Clone)" it finds. If no "(Clone)" is found, the name is returned unchanged.
gameObject | The GameObject to return the original name of. |
|
static |
Returns the value corresponding to the key in the preference file if it exists. If it doesn't exist, it will return defaultValue. (Internally, the value is stored as an int with either 0 or 1.)
key | The key. |
defaultValue | The default value if none is given. |
|
static |
Sets the value of the preference entry identified by the key. (Internally, the value is stored as an int with either 0 or 1.)
key | The key. |
value | The value to set the preference entry to. |
|
static |
Extends/shrinks the rect by extendDistance to each side and gets a random position from the resulting rect.
rect | The Rect. |
extendDistance | The distance to extend/shrink the rect to each side. |
|
static |
Gets a random subrect of the given width or height inside this rect.
rect | The Rect. |
width | The target width of the subrect. Clamped to the width of the given rect. |
height | The target height of the subrect. Clamped to the height of the given rect. |
|
static |
Rotates a Vector2.
v | The Vector2 to rotate. |
angleDeg | How far to rotate the Vector2 in degrees. |
|
static |
Rotates a Vector2.
v | The Vector2 to rotate. |
angleRad | How far to rotate the Vector2 in radians. |
|
static |
Sets the x/y/z transform.eulerAngles using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetEulerAngles(x: 5, z: 10), for example, only changing transform.eulerAngles.x and z.
transform | The transform to set the transform.eulerAngles at. |
x | If this is not null, transform.eulerAngles.x is set to this value. |
y | If this is not null, transform.eulerAngles.y is set to this value. |
z | If this is not null, transform.eulerAngles.z is set to this value. |
|
static |
Sets the x/y/z transform.localEulerAngles using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLocalEulerAngles(x: 5, z: 10), for example, only changing transform.localEulerAngles.x and z.
transform | The transform to set the transform.localEulerAngles at. |
x | If this is not null, transform.localEulerAngles.x is set to this value. |
y | If this is not null, transform.localEulerAngles.y is set to this value. |
z | If this is not null, transform.localEulerAngles.z is set to this value. |
|
static |
Sets the x/y/z transform.localPosition using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLocalPosition(x: 5, z: 10), for example, only changing transform.localPosition.x and z.
transform | The transform to set the transform.localPosition at. |
x | If this is not null, transform.localPosition.x is set to this value. |
y | If this is not null, transform.localPosition.y is set to this value. |
z | If this is not null, transform.localPosition.z is set to this value. |
|
static |
Sets the x/y/z transform.localScale using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLocalScale(x: 5, z: 10), for example, only changing transform.localScale.x and z.
transform | The transform to set the transform.localScale at. |
x | If this is not null, transform.localScale.x is set to this value. |
y | If this is not null, transform.localScale.y is set to this value. |
z | If this is not null, transform.localScale.z is set to this value. |
|
static |
Sets the x/y/z transform.lossyScale using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetLossyScale(x: 5, z: 10), for example, only changing transform.lossyScale.x and z.
transform | The transform to set the transform.lossyScale at. |
x | If this is not null, transform.lossyScale.x is set to this value. |
y | If this is not null, transform.lossyScale.y is set to this value. |
z | If this is not null, transform.lossyScale.z is set to this value. |
|
static |
Sets the x/y/z transform.position using optional parameters, keeping all undefined values as they were before. Can be called with named parameters like transform.SetPosition(x: 5, z: 10), for example, only changing transform.position.x and z.
transform | The transform to set the transform.position at. |
x | If this is not null, transform.position.x is set to this value. |
y | If this is not null, transform.position.y is set to this value. |
z | If this is not null, transform.position.z is set to this value. |
|
static |
When UnityEngine.Object.Instantiate(UnityEngine.Object) is called on a prefab named "Original", the resulting instance will be named "Original(Clone)". This method changes the name back to "Original" by stripping everything after and including the first "(Clone)" it finds. If no "(Clone)" is found, the name is left unchanged.
gameObject | The GameObject to change the name of. |
|
staticget |
Returns true or false with equal chance.
|
staticget |
Gets a random Vector2 of length 1 pointing in a random direction.
|
staticget |
Returns -1 or 1 with equal change.