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.GetOrAddComponent<MyComponent>();
someComponent.HasComponent<MyComponent>();
gameObject.GetOrAddComponent<MyComponent>();
gameObject.HasComponent<MyComponent>();
// Change direction of movement without modifying speed.
rigidbody.ChangeDirection(Vector3.right);
// Make array of game objects children of this transform.
transform.AddChildren(someGameObjects);
// Set children of this transform to position (0, 0, 0).
transform.ResetChildPositions();
transform.SetChildLayers("Ignore Raycast");
// Find closest position.
var otherPositions = someTransforms.Select(t => t.position);
transform.position.GetClosest(otherPositions);
// Convert `Vector3Int` struct to `Vector3`.
var vector = new Vector3Int(1, 2, 3);
var vector3 = vector.ToVector3();