What are best practices to stagger instantiation of 1000s of RealtimeViews
What are best practices to stagger instantiation of 1000s of RealtimeViews? I am upgrading from an approach using a dictionary to stagger instantiation of objects and need a way to spread this out over multiple frames.
2 Replies
Hi @ixikos. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
When dealing with the instantiation of thousands of RealtimeViews in NormalVR, it's important to manage the process efficiently to avoid performance issues. Here are some best practices you might consider:
1. Use Object Pooling: Object pooling is a technique where you instantiate a pool of objects at the start of the scene and then reuse them as needed, rather than instantiating and destroying them on the fly. This can significantly improve performance, especially in scenes with a large number of objects. NormalVR provides a delegate
IRealtimePrefabInstantiateDelegate
that can be used to implement object pooling for realtime prefab instances. The delegate has two methods: InstantiateRealtimePrefab(GameObject prefab)
and DestroyRealtimePrefab(GameObject prefabInstance)
. The former is called by Room to instantiate an instance of the given prefab, and the latter is called by Room to destroy a realtime prefab instance. When implementing object pooling, use DestroyRealtimePrefab
to return the prefab to the pool for reuse. source (https://normcore.io/documentation/reference/classes/Normal.Realtime.IRealtimePrefabInstantiateDelegate)
2. Stagger Instantiation Over Multiple Frames: If you need to instantiate a large number of objects, it's a good idea to spread this process over multiple frames to avoid a single frame taking too long and causing a noticeable stutter in the game. This can be done by creating a coroutine in Unity that instantiates a certain number of objects each frame until all objects have been instantiated.
3. Use RealtimeSet: RealtimeSet is a special model type that represents an unordered collection of models. It's used for things like keeping track of all RealtimeViews in the scene. If order is not important, this collection is the recommended collection to use for storing collections of models. All updates are applied instantly and are rolled back if rejected by the server (due to ownership). Adding or removing items sends the minimal amount of information to the server in order to perform the update on all clients. The whole collection is not sent every time. source (https://normcore.io/documentation/reference/classes/Normal.Realtime.Serialization.RealtimeSet%601)
Remember, the key to managing large numbers of objects in a VR scene is to be mindful of the resources you're using and to spread out intensive operations over multiple frames where possible.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@346532823143809046> kapa.ai is still learning and improving, please let us know how it did by reacting below