Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mminer/unity-extensions

Useful extension methods for Unity.
https://github.com/mminer/unity-extensions

game-development gamedev unity unity-extensions unity3d

Last synced: 3 months ago
JSON representation

Useful extension methods for Unity.

Awesome Lists containing this project

README

        

# Unity Extensions

Here we have [extension methods](http://en.wikipedia.org/wiki/Extension_method)
for Unity objects (`GameObject`, `Vector3`, etc.). These add useful
functionality that I often end up reimplementing in every game I make.

## Installing

Add the package to your project via
[UPM](https://docs.unity3d.com/Manual/upm-ui.html) using the Git URL
https://github.com/mminer/unity-extensions.git. You can also clone the
repository and point UPM to your local copy.

## Using

The classes are in the `UnityExtensions` namespace, so you must first import
them.

```csharp
using UnityExtensions;
```

Now call the extensions on the appropriate object type.

```csharp
void Awake ()
{
var someComponent = gameObject.GetOrAddComponent();
}
```

## Included Extensions

### Component

```csharp
someComponent.AddComponent();

someComponent.GetOrAddComponent();

someComponent.HasComponent();
```

### GameObject

```csharp
gameObject.GetOrAddComponent();

gameObject.HasComponent();

gameObject.IsInCullingMask(Camera.main.cullingMask);
```

### LayerMask

```csharp
// 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");
```

### Rigidbody

```csharp
// Change direction of movement without modifying speed.
rigidbody.ChangeDirection(Vector3.right);
```

### Transform

```csharp
// Make game objects children of this transform.
transform.AddChildren(gameObject1, gameObject2);

transform.Reset();

// Set children of this transform to position (0, 0, 0).
transform.ResetChildPositions();

transform.SetLocalPosition(y: 4f);
transform.SetPosition(z: 5f);

transform.SetChildLayers("Ignore Raycast");

```

### Vector2

```csharp
var newVector = someVector2.WithX(0.5f);
var newVector = someVector2.WithY(0.5f);
```

### Vector2Int

```csharp
var newVector = someVector2Int.WithX(1);
var newVector = someVector2Int.WithY(1);
```

### Vector3

```csharp
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);
```

### Vector3Int

```csharp
var newVector = someVector3Int.WithX(1);
var newVector = someVector3Int.WithY(1);
var newVector = someVector3Int.WithZ(1);
```

### Vector4

```csharp
var newVector = someVector4.WithX(0.5f);
var newVector = someVector4.WithY(0.5f);
var newVector = someVector4.WithZ(0.5f);
var newVector = someVector4.WithW(0.5f);
```