Here we have extension methods
for Unity objects (GameObject
, Vector3
, etc.). These add useful
functionality that I often end up reimplementing in every game I make.
Add the package to your project via UPM using the Git URL https://github.com/mminer/unity-extensions.git. You can also clone the repository and point UPM to your local copy.
The classes are in the UnityExtensions
namespace, so you must first import
them.
using UnityExtensions;
Now call the extensions on the appropriate object type.
void Awake ()
{
var someComponent = gameObject.GetOrAddComponent<MyComponent>();
}
someComponent.AddComponent<MyComponent>();
someComponent.AddComponents<MyComponent, AnotherComponent>();
someComponent.GetOrAddComponent<MyComponent>();
someComponent.HasComponent<MyComponent>();
someComponent.TryGetComponentInChildren<MyComponent>(out var component);
gameObject.AddComponents<MyComponent, AnotherComponent>();
gameObject.GetOrAddComponent<MyComponent>();
gameObject.HasComponent<MyComponent>();
gameObject.IsInCullingMask(Camera.main.cullingMask);
gameObject.SetLayerInChildren(LayerMask.NameToLayer("Ignore Raycast"));
gameObject.TryGetComponentInChildren<MyComponent>(out var component);
// Set camera culling mask to only "Ignore Raycast" and "UI".
LayerMask cullingMask = 0;
Camera.main.cullingMask = cullingMask.WithLayers("Ignore Raycast", "UI");
// Set camera culling mask to everything except "Ignore Raycast" and "UI".
LayerMask cullingMask = ~0;
Camera.main.cullingMask = cullingMask.WithoutLayers("Ignore Raycast", "UI");
var newRotation = rotation.WithEulerX(90f);
var newRotation = rotation.WithEulerY(90f);
var newRotation = rotation.WithEulerZ(90f);
// Change direction of movement without modifying speed.
rigidbody.ChangeDirection(Vector3.right);
transform.AddChildren(gameObject1, gameObject2);
transform.Reset();
transform.ResetChildPositions();
transform.SetLocalPosition(y: 4f);
transform.SetPosition(z: 5f);
transform.SetChildLayers("Ignore Raycast");
var newVector = someVector2.WithX(0.5f);
var newVector = someVector2.WithY(0.5f);
var newVector = someVector2Int.WithX(1);
var newVector = someVector2Int.WithY(1);
var newVector = someVector3.WithX(0.5f);
var newVector = someVector3.WithY(0.5f);
var newVector = someVector3.WithZ(0.5f);
// Find closest position.
var otherPositions = someTransforms.Select(t => t.position);
transform.position.GetClosest(otherPositions);
var newVector = someVector3Int.WithX(1);
var newVector = someVector3Int.WithY(1);
var newVector = someVector3Int.WithZ(1);
var newVector = someVector4.WithX(0.5f);
var newVector = someVector4.WithY(0.5f);
var newVector = someVector4.WithZ(0.5f);
var newVector = someVector4.WithW(0.5f);