31
2018
Find Nearby Objects using CullingGroup

Saw this tip at #UniteKL18, fast way to get nearby objects using CullingGroup!
(main image with 5000 gameobjects, searching objects within 3m radius)
example source code:
https://github.com/UnityCommunity/UnityLibrary/blob/master/Assets/Scripts/Docs/UnityEngine/CullingGroupExample.cs
my example project:
https://github.com/unitycoder/GetNearestObjectsWithCullingGroup
Some notes on that:
– You need to have MainCamera in the scene (as it uses that in “cullGroup.targetCamera = Camera.main;”)
– You need to set proper bounds for your objects, i’m currently doing that inside that for-loop (see b.position=.. and b.radius=..)
– i’m using array of Renderers to keep track of spawned objects (just to make it easy to change color without GetComponent<Renderer>), but you might want to use List, so that can add/remove objects there easily.
– I’m spawning objects on 2D circle, but this would work in 3D too
—
TODO
– Compare speed with OnTriggerEnter, Physics.OverlapSphere, BruteForce LINQ distances query, KD/QuadTree
– Test how this works with moving objects? (to get closest enemies for player..)
– Test how to get closests objects to 2 points? (probably cannot easily?)
—
Can also check which objects are outside camera view:

Related Posts
3 Comments + Add Comment
Leave a comment
Recent posts
- Favorites in PackageManager
- LudumDare59 : Signal
- Unity Editor: Tree Generator
- Leaf/Foliage Generator Tools (Runs in Browser)
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
Recent Comments
- on Mesh Exploder (sources)
- on Sprite Sheet Flip Book Shader
- on Sprite Sheet Flip Book Shader
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by












I tested with OnTrigger and also for moving objects. The performance is almost 5 times better with CullingGroup
Any update on this? Ive been using CullingGroup stuff for basic culling/LOD but this use case really interests me!
I’ve looked at K-D trees too – but this seems like it might be faster out of the box?
UnityEngine.CullingGroup API for everyone
https://github.com/mackysoft/Vision