Hi! I’m making a procedural game, and I’ve made a system that Instantiates/Destroys GameObjects, to avoid lag, based on distance.
The only problem is that some of the prefabs that needs to be spawned are complex, and when they spawn/destroy they cause annoying frame-rate drops.
Is there a way to do a sort of Asynchronous GameObject.Instantiate, so the frame-rate drops are less annoying? (Or if you know another way to avoid this problem)
Use SetActive instead of instantiating and destroying. That way, the GameObjects remain in the scene but require no processing.
Use the unity.addressable package
you can find a function called InstantiateAsync
public async Task LoadAddress(string path)
AsyncOperationHandle<GameObject> op = Addressables.InstantiateAsync(path);
op.Completed += OnLoadDone;
void OnLoadDone(AsyncOperationHandle<GameObject> op)
GameObject obj = op.Result;
You can then set that up in an async thread and await op.task you can add functions to the op.Completed Action and they will run when it is complete.
To load them all in at once you can skip the await but i wouldn’t advise that for large files.