Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/mminer/unity-extensions
- Owner: mminer
- License: mit
- Created: 2014-04-30T00:50:31.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-07-24T01:16:30.000Z (4 months ago)
- Last Synced: 2024-07-24T03:42:49.216Z (4 months ago)
- Topics: game-development, gamedev, unity, unity-extensions, unity3d
- Language: C#
- Homepage:
- Size: 44.9 KB
- Stars: 74
- Watchers: 5
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-unity-open-source-on-github - unity-extensions - Useful extension methods (Extension Methods)
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);
```