Treasure chest of Unity developer tools. Professional inspector tooling, high-performance utilities, spatial queries, and 20+ editor tools.
This approachable guide shows when to use OctTree3D, KdTree3D, and RTree3D, with quick code you can copy.
Quick picks
Points (OctTree3D / KdTree3D)
using WallstopStudios.UnityHelpers.Core.DataStructure;
using UnityEngine;
using System.Collections.Generic;
struct VfxPoint { public Vector3 pos; public int id; }
var points = new List<VfxPoint>(/* fill with positions */);
// Build trees from points
var oct = new OctTree3D<VfxPoint>(points, p => p.pos);
var kd = new KdTree3D<VfxPoint>(points, p => p.pos); // balanced by default
// Range query (sphere)
var inRange = new List<VfxPoint>();
oct.GetElementsInRange(playerPos, 12f, inRange);
// Bounds (box) query
var inBox = new List<VfxPoint>();
kd.GetElementsInBounds(new Bounds(center, size), inBox);
// Approximate nearest neighbors
var neighbors = new List<VfxPoint>();
kd.GetApproximateNearestNeighbors(playerPos, count: 12, neighbors);
Sized objects (RTree3D)
using WallstopStudios.UnityHelpers.Core.DataStructure;
using UnityEngine;
using System.Collections.Generic;
struct Volume { public Bounds bounds; public int kind; }
var volumes = new List<Volume>(/* fill with bounds */);
// Build from 3D bounds (AABBs)
var rtree = new RTree3D<Volume>(volumes, v => v.bounds);
// Bounds query (fast for large volumes)
var hits = new List<Volume>();
rtree.GetElementsInBounds(worldBounds, hits);
// Range query (treats items by their bounds)
var near = new List<Volume>();
rtree.GetElementsInRange(center, radius, near);
Notes
SpatialHash3D for broad‑phase neighborhood queries.All 3D spatial trees support the same zero-allocation query pattern as their 2D counterparts. Pass a reusable buffer to avoid GC allocations:
// Reusable buffer (declare once)
private List<VfxPoint> nearbyBuffer = new(128);
void Update()
{
nearbyBuffer.Clear();
// 🟢 GOOD: Reuses same List = zero allocations
tree.GetElementsInRange(playerPos, 15f, nearbyBuffer);
foreach (VfxPoint p in nearbyBuffer)
{
p.UpdateEffect();
}
}
All 3D spatial trees support buffered queries:
OctTree3D.GetElementsInRange(pos, radius, buffer)KdTree3D.GetElementsInBounds(bounds, buffer)RTree3D.GetElementsInRange(pos, radius, buffer)📖 For the complete buffering guide including pooled buffers and GC impact analysis, see:
- Zero-Allocation Queries (2D Guide) — detailed examples
- Buffering Pattern — project-wide pooling utilities