##Mapbox Unity SDK 3.0 Quickstart guide
-
Get the Mapbox Unity SDK v3.0
- Clone the Mapbox Unity SDK v3.0 branch to local.
-
Open Unity Package Manager (UPM)
- Open your Unity project and navigate to the Unity Package Manager.
-
Add the Package from Disk
- Click the
Add package from diskbutton in the top-left corner of the UPM window. - Locate and select the
package.jsonfile within the Mapbox folder.
- Click the
-
Verify Installation
- The Mapbox Unity SDK package should now appear in the Unity Package Manager window.
-
Access the Demo Scenes
- The SDK includes two demo scenes:
Location Based GameWorld Map Viewer
- The SDK includes two demo scenes:
-
Import the Demo Scenes
- In the Package Manager, select the Mapbox Unity SDK package.
- Go to the "Samples" tab.
- Hit the Import button to create a
Samplesfolder at the root of your project. - Unity will copy the demo scene files into this folder.
-
Configure Your Access Token
- The demo scenes will not work until you set your access token.
- Open the Token Configuration window under the Mapbox section in Unity's top menu.
- Enter your access token.
-
Token Storage
- Unity will save the access token in a file located at:
Resources/Mapbox/MapboxConfiguration.txt
- Unity will save the access token in a file located at:
-
Run the Demo Scenes
- After configuring your access token, open the demo scenes and play them.
The integration process depends heavily on your project setup and how you intend to use the map. Below are steps to help you get started with a location-based game setup:
-
Import the Location-Based Game Sample
- Navigate to
Samples/Mapbox Unity SDK/3.0.0/Location Based Game.
- Navigate to
-
Use the
LocationBasedMapPrefab- Add the
LocationBasedMapprefab to any scene, and it should work. - Optionally, copy the following to another location in your project for easier customization:
LocationBasedMapprefabMapVisualsfolderScriptsfolder
- After copying, you can safely delete the
Samplesfolder if it’s no longer needed.
- Add the
The Mapbox Unity SDK provides two primary methods for working with location:
- The Mapbox Map Behaviour script includes a field for latitude and longitude.
- If the
Initialize On Startcheckbox is enabled:- The specified latitude and longitude will serve as the starting location of the map.
- This method is simple and effective, particularly during development in the Unity editor.
Limitations:
- This approach does not use the device's location.
- If you build the project with these settings, the map will display the predefined location specified in the script.
Usage in the Location-Based Game Demo Scene:
- You can move the
CharTargetgame object within the scene. The Astronaut character will walk toward this object in Unity space.
For a more advanced and dynamic solution, you can use the LocationModule system. This method is used by default in the Location-Based Game demo scene.
- The LocationModule system abstracts location handling:
- In builds: Uses the device's location.
- In the editor: Uses predefined location values.
- Predefined values can be configured in the LocationModule game object located under the map prefab.
Step 1. Disable Initialize On Start
- Uncheck the
Initialize On Startcheckbox in the map script.
Step 2. Add Snap Map To Location Provider
- Attach the
Snap Map To Location Providerscript to your map. - Set it up as shown in the provided documentation or image reference.
Step 3. Enable Player Movement with Device Location
- Attach the
Snap Transform To Location Providerscript to theCharTargetgame object. - Configure the script to update the position of the
CharTargetobject with the device's location in real-time. - The player avatar will automatically follow the
CharTargetobject as it moves.
By following these steps, your map will dynamically update based on the device's location, and the player avatar will respond to real-world movements.
To build your map application for the Android platform, you'll need specific settings files included in the Mapbox Unity SDK package. Follow these steps:
- Go to the
Mapbox Unity SDK/Runtime/AndroidBuildSettingsfolder, you'll find a few folders named after Unity Editor Version. - Find the one matching (or closest lower) with the version you are using
- Copy the
Pluginsfolder inside to your project. Place it under theAssetsfolder.
- If your project already contains custom Android settings, merge them with the copied files to ensure compatibility.
- Open Project Settings and navigate to the Player section.
- Under the Publish Settings section (at the bottom), verify that the custom setting files we just copied are applied.
- After completing the above steps, build your application for the Android platform.