Mixed Reality Utility Kit - Dynamic Spawning.
Updated: Mar 13, 2025
After completing this section, the developer should understand the concept of placing and spawning objects dynamically at various anchor positions.
Developers can create custom logic using the logic described earlier by navigating through the anchors contained in MRUK.room; however, MRUK provides a number of prefabs that can be used directly, or as models to understand dynamic placement.
The
AnchorPrefabSpawner will spawn prefabs at the location of anchors and scale them to match the size of the plane or volume. For each semantic label, you can specify a list of prefabs to choose from. The prefab chosen from the list can be random or selected to best match the size of the anchor. If random is selected you can specify a seed value to the random number generator in order to get deterministic results which can be useful for multiplayer networked use cases. There are various scaling options which allow you to stretch the prefab in all three axes or scale them to best fit, while maintaining the original aspect ratio of the prefab. For example, this can be useful when spawning cylinder shapes and you want to ensure circles don’t get distorted into ellipses. There is logic to infer the best orientation of the prefab. For example when spawning a couch, you will typically want to avoid the seating area pointing towards a wall (this information is not available from the Scene API but is inferred by MRUK using ray casts).
The
FindSpawnPositions helps with instantiating content in unknown, user-defined environments. You can specify a list of semantic labels where you would like to spawn content on. A uniformly random position will be selected from the surface of the anchors specified. When a position is found, a prefab object will be spawned at that location. If more than one prefab is configured to be spawned the process will be repeated until it has successfully spawned the request amount. Optionally, a physics check can be performed to avoid spawning the prefab at a location which overlaps with other existing game objects, the layer mask is configurable. If an overlap is detected, another position will be randomly selected. This process continues until either a valid position is found or the max iterations is reached. Max iterations is used to avoid infinite loops when there are no valid positions available.