The leaf nodes are used to store items.
Items hold: a vector position and 4 references to other similar items.
Kd-tree nodes are also referencing each other (parent-child relationship).
To keep those references, each node of my tree is a scriptable object and gets saved as an asset onto the disk.
The items the leaf nodes hold are also scriptable objects and are also saved as assets.
However, just to store 50 items like that, I need to create ~200 tree nodes (200 scriptable objects).
This comes down to about 400KB of size just for the tree. But I need to store information about 50 000 items, which would rocket up to Gigabytes of data.
So, creating a scriptable object to store information in 2 fields and keep a few references seems an overkill.
1 scriptable object =2-3kb of data.
Since you don’t have anything more in your objects than a reference to the parent I don’t think you actually need to necessarily define them as scriptable objects. So long as you keep track of the parent and the Vector3 you’re good right? I think you could use a tree if you create an index that accounts for which ‘branch’ on the tree the Vector3 resides like this:
And then you can get the parent just by using modulus on the index:
private int GetParent(int child)
{
int numDigits = child.ToString().Length;
return child % (int)Mathf.Pow(10, numDigits - 1);
}
With this method you could store it all either in one dictionary with an int key (or long if you have a lot of children) and Vector3 values or split up the Vector3 into floats and create 4 arrays of fixed length, using corresponding indices to match up the values. Tough problem no matter how you go about it, good luck!
You could implement the nodes as serializeable classes (or structs), and simply keep them in an array - ordered or unordered - in your tree scriptable object. Arrays of serializeable objects can be serialized, and that should seriously cut down on your disk space usage.
You could also store this information in your own format. Unity has a great built-in system for serializing stuff directly, without you having to write any code, but if you write your own format to read/write this data to files, you could cut the neccessary amount of data in the files by a lot. It’ll take a bit of time, but might be worth it if you’re storing very large amounts of information.