Hey guys (and girls)!
I’ve been writing these small utility and extension methods over and over again with each new project, and now I’m done with that
UnityQuery aims to collect the most general, versatile and helpful of these code snippets for re-use. It is a small, lightweight C# library designed to increase productivity with Unity3D.
Features:
- Hierarchy queries (e.g. GetChildren, GetDescendants, OnLayer, IsAncestor, GetRoot, …)
- Hierarchy manipulation (e.g. DestroyChildren)
- Component-wise vector swizzling (e.g. v.XZ)
- Changing single vector or color components while preserving immutability
- Picking (e.g. object at mouse position)
- Collection extensions (e.g. ContainsAll, GetValueOrDefault, IsNullOrEmpty, SequenceToString)
- Logs with timestamps and object names
Examples:
Here are a few examples of what comes with UnityQuery.
foreach (var descendant in this.gameObject.GetDescendants().OnLayer("UI"))
{
// Selects all descendants (children, grandchildren, etc.) of a game object.
}
// Instantiates a prefab and parents it to this one.
// Resets position, rotation and scale and inherits the layer.
this.gameObject.AddChild(prefab);
// v = (2, 3, 4)
Vector3 v = new Vector3(2, 3, 4);
// w = (2, 3, 5)
Vector3 w = v.WithZ(5);
Check the PDF cheat sheet for a quick glance of other features.
The full source code is available at GitHub. If you’re missing any of your personal favorites, we’d love to see it - we are always open for contributions! Just post an answer in this thread, and we’ll consider adding it in the next release.
If you need any help or experience any issues, feel free to contact us immediately.
Please note this library is not officially related to Unity Technologies in any way.