Replace features
This example demonstrates how you can use the ReplaceFeatureModifier
to swap out a default extruded building with a custom 3D object. With this modifier, you can replace your corporate office locations with a branded building model, or spawn a highly detailed 3D object in place of a monument or building.
Game object modifiers
Game object modifiers are ScriptableObjects
that work with meshes and game objects to further allow you to decorate, enhance, or make modifications to your game objects. To create a new modifier, right click in your project window and navigate to Create > Mapbox > Modifiers
You can also create modifiers by clicking Add New in the Game Object Modifiers > Behavior Modifiers section of the AbstractMap
script.
ReplaceFeatureModifier overview
To open the modifier for this example, navigate to MAP LAYERS > FEATURES, and then select the ExtrudedBuildings Map Feature. Next, with ExtrudedBuildings selected, open the Behavior Modifiers section.
Under Game Object Modifiers, double click on ReplaceFeatureModifier. This will open the modifier in your Inspector window and you will see the following settings:
Active: This boolean setting toggles the modifier on and off.
Prefab: This is the 3D object to spawn in place of the feature is specified.
Scale down with world: This setting scales the specified prefab to the size of the tile.
Prefab Locations: This is an array that holds all locations to all the buildings and features to be replaced with the supplied prefab. You can increase the value of Size to add new elements to the array. Click Search to find a feature, or paste in the latitude and longitude coordinates.
Explicitly Blocked Feature Ids: This is a list of ids on the ExtrudedBuildings layer to prevent from spawning. You can leave this empty unless you find an issue where the latitude and longitude coordinates are not properly filtering out the correct building to replaced.
Note that in the Advanced section of ExtrudedBuildings that Buildings with Unique Ids is checked. Also, in the same layer Group Features is unchecked. These settings are required for feature replacement to work as expected.
Replace a building with a 3D model
Follow the steps below or in this video tutorial to replace the Empire State Building with a custom 3D model.
To replace a building with a 3D model, you can configure a new ReplaceFeatureModifier
. In your Project window, open up the CitySimulator scene found in the Mapbox > Examples > 0_PrefabScenes
folder. Or, you can create a new scene and add the CitySimulatorMap
prefab to the scene.
- Select the CitySimulatorMap game object in your Hierarchy window.
- On the
AbstractMap
navigate to MAP LAYERS > FEATURES, and then select the ExtrudedBuildings Map Features. - Select ExtrudedBuildings, and open Behavior Modifiers.
- Under Game Object Modifiers, click Add New and select ReplaceFeatureModifier
- This will create a new modifier. Double click NewReplaceFeatureModifier to open the modifier in your Inspector window.
- Assign the
Building Pivot
prefab. You can use your own model here if you would like. - Click Prefab Locations and change the Size to
1
. - Click Search to search for
Empire State Building
or paste in40.748333333333, -73.985277777778
to Element 0.
- To make sure that the location of the Empire State Building is in the view of the camera, select CitySimulatorMap in your Hierarchy. Then, inside the GENERAL settings of the
AbstractMap
script, set the Latitude Longitude to40.748333333333, -73.985277777778
which is the location of the building.
- Enter Play mode to see the prefab spawned in place of the Empire State Building!