This use case describes the steps to use glTFast Add-on API to import custom data from the extras
property of a glTF™ JSON object. This example uses Newtonsoft JSON parser to deserialize data.
To accomplish this use case, do the following:
- Add custom data in a glTF asset
- Create a custom glTF import behavior
- Add assembly definitions
- Set up a new scene
- Import the glTF asset in runtime
Before you start, you must add the following package dependencies to your project.
- In the
manifest.json
file, add the following dependencies:
{
"dependencies": {
// Add these lines:
// Replace "<x.y.z>" with the version you wish to install
"com.unity.cloud.gltfast": "<x.y.z>",
"com.unity.nuget.newtonsoft-json": "<x.y.z>"
// Other dependencies...
}
}
Add some custom data in the extras
property of a glTF JSON object:
"nodes": [
{
// Example of mesh data in a glTF
"mesh": 0,
"name": "Cube",
// Add these lines:
"extras": {
"some-extra-key": "some-extra-value"
}
}
]
To create a custom glTF import behavior, follow these steps:
- Open your Unity® Project.
- Go to the Assets folder in the Project window.
- Select and hold Create.
- Select C# Script.
- Rename the new script as
CustomGltfImport
. - Open the
CustomGltfImport
script and replace the content with the following: [!code-cs custom-gltf-import] - Repeat step 2-4 to create another new script
- Rename the new script as
ExtraData
. - Open the
ExtraData
script and replace the content with the following: [!code-cs extra-data]
In your assembly definition file (.asmdef
file), add the following references:
"references": [
// Add these lines:
"glTFast",
"glTFast.Newtonsoft"
// Other references...
]
To set up a new scene, follow these steps:
- Create a new scene.
- Create a GameObject called GltfImport.
- Select Add Component in the Inspector window and add the Custom Gltf Import component.
- In the Uri field, set the path to point to where the glTF asset is stored.
Select Play, the glTF asset should be loaded and displayed at runtime.
You can verify that the custom data in the extras
property of the glTF is imported correctly by inspecting the loaded glTF asset:
Unity® is a registered trademark of Unity Technologies.
glTF™ is a trademark of The Khronos Group Inc.