What I’m currently trying to achieve is to be able to generate Voronoi diagrams in Unity but with each “cell” created clickable. So for example I’d like to be able to create something like that:
But each cell/region to be clickable so that I can then bind some actions into them.
How would you go about creating something like that? Cause honestly I don’t really even know where to start. Is there some known library for that in the asset store? Or is it easily code-able yourself?
Probably better of looking for an algorithm or sourcecode somewhere, most likely outside of unity. You’d then have to try to turn each region into a separate game object with a mesh/collider? Depends if you want this unique each time the thing runs, or otherwise cut out some shapes with an art program.
I searched “voronoi unity” and found this post with a library that can generate voronoi shapes with C#:
Looks like it can generate a voronoi map using a list of points and the bounds to contain them, and optionally the number of relaxation iterations. You should be able to use the generated edges to dynamically create a polygon collider for each region.
Oh, I did see this topic but discarded the idea of using it after I saw someone mentioning bugs there. Only now that you linked it I decided to have a look at the whole topic again and found out that the author has actually patched the bugs - that’s great, exactly what I needed, thanks a lot
The problem, though, is with forcing some interaction with the created diagram. As you mention, I do receive a list of edges but while drawing them is easy as it doesn’t require any particular order, knowing what subset of them is needed for each created polygon doesn’t seem that easy anymore - do you happen to have any idea how I could approach it?
At first glance it appears that there is a dictionary called “SitesIndexedByLocation” in Voronoi.cs which contains all the “Site” objects generated during the initialization, keyed by the original points. Each one of those Site objects contains a list “Edges” representing that site’s Voronoi Region. So I would think that you could create one polygon collider for each Site using the Edges list. Grab the “LeftVertex” and “RightVertex” properties of each edge, and you should have the complete polygon.