3.0 KiB
Use case: Use glTFast Add-on API
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
Before you start, you must add the following package dependencies to your project.
- In the
manifest.jsonfile, 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...
}
}
How do I...?
Add custom data in a glTF asset
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"
}
}
]
Create a custom glTF import behavior
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
CustomGltfImportscript 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
ExtraDatascript and replace the content with the following: [!code-cs extra-data]
Add assembly definitions
In your assembly definition file (.asmdef file), add the following references:
"references": [
// Add these lines:
"glTFast",
"glTFast.Newtonsoft"
// Other references...
]
Set up a new scene
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.
Import the glTF asset in runtime
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:

Trademarks
Unity® is a registered trademark of Unity Technologies.
glTF™ is a trademark of The Khronos Group Inc.