#Suggestions for instancing many game objects

1 messages · Page 1 of 1 (latest)

humble night
#

I have this class which contains a collection of CellData, each cell stores the position and type of object to create. In the image this shows hundred of different cells.

public class CellDataArray {
    public CellData[] cells;

    public CellDataArray(PointDataArray pointDataArray) {
        PointData[] pointData = pointDataArray.points;

        cells = new CellData[pointData.Length];
        for (int i = 0; i < pointData.Length; i++) {
            CellData newCell = new CellData(pointData[i]);
            cells[i] = newCell;
        }
    }
}

public struct CellData {
    public int ID; // ID corresponds with a resource mesh prefab
    public Vector3 Position;

    public CellData(PointData data) {
        cellID = data.id;
        cellPosition = new Vector3(data.p[0], 0f, data.p[1]);
    }
}```I need to generate the actual objects, but I want to improve my current approach, that is to for-loop over all the cells instantiate a GameObject prefab which loads the correct mesh there.

There are 2 things I dislike about this
1: It feels like its far too heavy when I have thousands of them in the scene. I have ideas for optimizing the way cells are constructed, but I need to have a better way of representing those as actual game objects.
2: Constructing/Disposing the cell instances feels like it takes extra steps (perhaps this is just a reality of Unity), i.e I cant simply do `CellDataArray.cells = new CellData[0];` and expect the objects in the scene to automatically be destroyed.

Ideally all I want is for as long as a CellData exists, I want an object to exist in the scene at its position. How its actually represented as a game object, doesnt really matter a whole bunch, it just needs to be able to load one of the FBX meshes
#

For context, this is the most recent form of how the cell object instancing is handled

// old implementation for creating the cell prefabs
for (int i = 0; i < points.Length; i++) {
    Cell cellInstance = InstanceHelper.LoadAndInstancePrefab<Cell>($"Prefabs/Cell");
    cellInstance.CellPoint = points[i];
    cellInstance.transform.position = points[i].Position;
}

// old cell GameObject
public class Cell : MonoBehaviour {
    public Point CellPoint;

    void Start() {
        InstanceFBX();
    }

    void InstanceFBX() {
        GameObject fbxInstance = InstanceHelper.LoadAndInstancePrefab<GameObject>($"FBX/{CellPoint.id}");
        fbxInstance.transform.position = CellPoint.Position;
        fbxInstance.transform.SetParent(transform);
    }
}```
static whale
#

To improve cpu side performance, I'd avoid spawning them as game objects entirely. Instead draw them procrammatically with draw index indirect or something.
This way the cells can be just plain classes with cell data and you can use constructors as you wanted.

#

You can further improve on that by making the cells structs, so that there's no GC overhead.

#

Oh wait, looking at your code again, that's already happening.

#

So, just the avoid game objects part I guess.

humble night
fallen vine
#

have you seen this?

#

you can think of vfx graph as a super shader graph

#

so you can give it your buffer of celldata and draw in vfx graph

#

it's easy to do that

#

GraphicsBuffers are very easy to use

#

you don't want to reinvent vfx graph by writing your own Graphics.DrawProceduralIndirect calls

#

with vfx graph you can set the mesh to use as your cells at runtime too

#

that said you don't have a lot of meshes so i'm not sure it is worth optimizing too much

#

will you need colliders or to interact with the mouse?

humble night
# fallen vine what is your big picture goal?

This is more of an experiment than something with a concrete goal. For the moment its just designed to take a table of points and output geometry according what the file contains, and I have a way to tile those chunks for potentially infinite worlds (not that I have any real plan on making something like Minecraft from this)

humble night
#

something like that VFX graph seems neat, but I'm not a huge fan of terrain systems that just give you a few parameters to drive a noise texture. My thing does use perlin noise to produce noise, but I'm planning on a way to design rules for the file that are more "abstract" and those rules are what drive the noise.

for example, I want to be able to mark a location like (0,0) as the player spawn, and tag all terrain in some radius to that. that tag can have settings that might forbid there from being a ledge more than 5 meters high, or I can make all terrain in that tag to be completely flat.

humble night
humble night
# fallen vine https://www.reddit.com/r/Unity3D/comments/1m8409u/after_25_years_of_work_i_final...

I like that a lot!, Its pretty interesting that something they do, is exactly what I have planned for development!
I can think of more than 1 way I could utilize splines to some degree with my tool, but not quite like their editor

Plus as my tool is intended to be used as for a game, (which is not the actual goal of this project), it would be good to mask a region along terrain to help make it easier to travel along

fallen vine
#

i think you should synthesize all this stuff first