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
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- 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
Recent Comments
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
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