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 to Trent
Recent posts
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
- Check if point is inside polygon 2D (using line intersection)
- UnityHub: Enable Create Project Button (without having to select Cloud Organization)
- UI: Make Tooltip background fit Text content length
- Pong-Wars in Unity
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub)
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