Class GltfImport
Loads a glTF's content, converts it to Unity resources and is able to
feed it to an
Inherited Members
Namespace: GLTFast
Assembly: glTFast.dll
Syntax
public class GltfImport : IGltfReadable, IDisposable
Constructors
GltfImport(IDownloadProvider, IDeferAgent, IMaterialGenerator, ICodeLogger)
Constructs a GltfImport instance with injectable customization objects.
Declaration
public GltfImport(IDownloadProvider downloadProvider = null, IDeferAgent deferAgent = null, IMaterialGenerator materialGenerator = null, ICodeLogger logger = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IDownloadProvider | downloadProvider | Provides file access or download customization |
| IDeferAgent | deferAgent | Provides custom update loop behavior for better frame rate control |
| IMaterialGenerator | materialGenerator | Provides custom glTF to Unity material conversion |
| ICodeLogger | logger | Provides custom message logging |
Fields
defaultMaterial
Required for Editor import only to preserve default/fallback materials
Declaration
public Material defaultMaterial
Field Value
| Type | Description |
|---|---|
| Material |
Properties
DefaultSceneIndex
Default scene index
Declaration
public int? DefaultSceneIndex { get; }
Property Value
| Type | Description |
|---|---|
| int? |
ImageCount
Number of images
Declaration
public int ImageCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
LoadingDone
True, when loading has finished and glTF can be instantiated
Declaration
public bool LoadingDone { get; }
Property Value
| Type | Description |
|---|---|
| bool |
LoadingError
True if an error happened during glTF loading
Declaration
public bool LoadingError { get; }
Property Value
| Type | Description |
|---|---|
| bool |
MaterialCount
Number of materials
Declaration
public int MaterialCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
SceneCount
Number of scenes
Declaration
public int SceneCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
TextureCount
Number of textures
Declaration
public int TextureCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
Dispose()
Frees up memory by disposing all sub assets. There can be no instantiation or other element access afterwards.
Declaration
public void Dispose()
GetAccessorSparseIndices(AccessorSparseIndices, out void*)
Get sparse indices raw data
Declaration
public void GetAccessorSparseIndices(AccessorSparseIndices sparseIndices, out void* data)
Parameters
| Type | Name | Description |
|---|---|---|
| AccessorSparseIndices | sparseIndices | glTF sparse indices accessor |
| void* | data | Pointer to accessor's data in memory |
GetAccessorSparseValues(AccessorSparseValues, out void*)
Get sparse value raw data
Declaration
public void GetAccessorSparseValues(AccessorSparseValues sparseValues, out void* data)
Parameters
| Type | Name | Description |
|---|---|---|
| AccessorSparseValues | sparseValues | glTF sparse values accessor |
| void* | data | Pointer to accessor's data in memory |
GetAnimationClips()
Returns all imported animation clips
Declaration
public AnimationClip[] GetAnimationClips()
Returns
| Type | Description |
|---|---|
| AnimationClip[] | All imported animation clips |
GetBindPoses(int)
Returns an array of inverse bone matrices representing a skin's bind pose suitable for use with UnityEngine.Mesh.bindposes by glTF skin index.
Declaration
public Matrix4x4[] GetBindPoses(int skinId)
Parameters
| Type | Name | Description |
|---|---|---|
| int | skinId | glTF skin index |
Returns
| Type | Description |
|---|---|
| Matrix4x4[] | Corresponding bind poses |
GetDefaultMaterial()
Returns a fallback material to be used when no material was assigned (provided by the IMaterialGenerator)
Declaration
public Material GetDefaultMaterial()
Returns
| Type | Description |
|---|---|
| Material | Default material |
GetImage(int)
Returns a texture by its glTF image index
Declaration
public Texture2D GetImage(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF image index |
Returns
| Type | Description |
|---|---|
| Texture2D | Corresponding Unity texture |
GetMaterial(int)
Get a Unity Material by its glTF material index
Declaration
public Material GetMaterial(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF material index |
Returns
| Type | Description |
|---|---|
| Material | Corresponding Unity Material |
GetMeshes()
Returns all imported meshes
Declaration
public Mesh[] GetMeshes()
Returns
| Type | Description |
|---|---|
| Mesh[] | All imported meshes |
GetSceneName(int)
Get a glTF's scene's name by its index
Declaration
public string GetSceneName(int sceneIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | sceneIndex | glTF scene index |
Returns
| Type | Description |
|---|---|
| string | Scene name or null |
GetSourceCamera(uint)
Get source (de-serialized glTF) camera
Declaration
public Camera GetSourceCamera(uint index)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | index | glTF camera index |
Returns
| Type | Description |
|---|---|
| Camera | De-serialized glTF camera |
GetSourceImage(int)
Get source (de-serialized glTF) image
Declaration
public Image GetSourceImage(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF image index |
Returns
| Type | Description |
|---|---|
| Image | De-serialized glTF image |
GetSourceLightPunctual(uint)
Get source (de-serialized glTF) light
Declaration
public LightPunctual GetSourceLightPunctual(uint index)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | index | glTF light index |
Returns
| Type | Description |
|---|---|
| LightPunctual | De-serialized glTF light |
GetSourceMaterial(int)
Get source (de-serialized glTF) material
Declaration
public Material GetSourceMaterial(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF material index |
Returns
| Type | Description |
|---|---|
| Material | De-serialized glTF material |
GetSourceNode(int)
Get source (de-serialized glTF) node
Declaration
public Node GetSourceNode(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF node index |
Returns
| Type | Description |
|---|---|
| Node | De-serialized glTF node |
GetSourceRoot()
Get source root (de-serialized glTF JSON). This is intended for read-only access. Changes might corrupt data and break subsequent scene instantiation.
Declaration
public Root GetSourceRoot()
Returns
| Type | Description |
|---|---|
| Root | De-serialized glTF root object |
GetSourceScene(int)
Get source (de-serialized glTF) scene
Declaration
public Scene GetSourceScene(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF scene index |
Returns
| Type | Description |
|---|---|
| Scene | De-serialized glTF scene |
GetSourceTexture(int)
Get source (de-serialized glTF) texture
Declaration
public Texture GetSourceTexture(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF texture index |
Returns
| Type | Description |
|---|---|
| Texture | De-serialized glTF texture |
GetTexture(int)
Returns a texture by its glTF texture index
Declaration
public Texture2D GetTexture(int index = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | glTF texture index |
Returns
| Type | Description |
|---|---|
| Texture2D | Corresponding Unity texture |
InstantiateMainScene(IInstantiator)
Creates an instance of the main scene of the glTF ( "scene" property in the JSON at root level; DefaultSceneIndex) If the main scene index is not set, it instantiates nothing (as defined in the glTF 2.0 specification)
Declaration
[Obsolete("Use InstantiateMainSceneAsync for increased performance and safety. Consult the Upgrade Guide for instructions.")]
public bool InstantiateMainScene(IInstantiator instantiator)
Parameters
| Type | Name | Description |
|---|---|---|
| IInstantiator | instantiator | Instantiator implementation; Receives and processes the scene data |
Returns
| Type | Description |
|---|---|
| bool | True if the main scene was instantiated or was not set. False in case of errors. |
InstantiateMainScene(Transform)
Creates an instance of the main scene of the glTF ( "scene" property in the JSON at root level; DefaultSceneIndex) If the main scene index is not set, it instantiates nothing (as defined in the glTF 2.0 specification)
Declaration
[Obsolete("Use InstantiateMainSceneAsync for increased performance and safety. Consult the Upgrade Guide for instructions.")]
public bool InstantiateMainScene(Transform parent)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | parent | Transform that the scene will get parented to |
Returns
| Type | Description |
|---|---|
| bool | True if the main scene was instantiated or was not set. False in case of errors. |
InstantiateMainSceneAsync(IInstantiator, CancellationToken)
Creates an instance of the main scene of the glTF ( "scene" property in the JSON at root level; DefaultSceneIndex) If the main scene index is not set, it instantiates nothing (as defined in the glTF 2.0 specification)
Declaration
public Task<bool> InstantiateMainSceneAsync(IInstantiator instantiator, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IInstantiator | instantiator | Instantiator implementation; Receives and processes the scene data |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if the main scene was instantiated or was not set. False in case of errors. |
InstantiateMainSceneAsync(Transform, CancellationToken)
Creates an instance of the main scene of the glTF ( "scene" property in the JSON at root level; DefaultSceneIndex) If the main scene index is not set, it instantiates nothing (as defined in the glTF 2.0 specification)
Declaration
public Task<bool> InstantiateMainSceneAsync(Transform parent, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | parent | Transform that the scene will get parented to |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if the main scene was instantiated or was not set. False in case of errors. |
InstantiateScene(IInstantiator, int)
Creates an instance of the scene specified by the scene index. SceneCountGetSceneName(int)
Declaration
[Obsolete("Use InstantiateSceneAsync for increased performance and safety. Consult the Upgrade Guide for instructions.")]
public bool InstantiateScene(IInstantiator instantiator, int sceneIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| IInstantiator | instantiator | Instantiator implementation; Receives and processes the scene data |
| int | sceneIndex | Index of the scene to be instantiated |
Returns
| Type | Description |
|---|---|
| bool | True if the scene was instantiated. False in case of errors. |
InstantiateScene(Transform, int)
Creates an instance of the scene specified by the scene index. SceneCountGetSceneName(int)
Declaration
[Obsolete("Use InstantiateSceneAsync for increased performance and safety. Consult the Upgrade Guide for instructions.")]
public bool InstantiateScene(Transform parent, int sceneIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | parent | Transform that the scene will get parented to |
| int | sceneIndex | Index of the scene to be instantiated |
Returns
| Type | Description |
|---|---|
| bool | True if the scene was instantiated. False in case of errors. |
InstantiateSceneAsync(IInstantiator, int, CancellationToken)
Creates an instance of the scene specified by the scene index. SceneCount GetSceneName(int)
Declaration
public Task<bool> InstantiateSceneAsync(IInstantiator instantiator, int sceneIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IInstantiator | instantiator | Instantiator implementation; Receives and processes the scene data |
| int | sceneIndex | Index of the scene to be instantiated |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if the scene was instantiated. False in case of errors. |
InstantiateSceneAsync(Transform, int, CancellationToken)
Creates an instance of the scene specified by the scene index. SceneCount GetSceneName(int)
Declaration
public Task<bool> InstantiateSceneAsync(Transform parent, int sceneIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | parent | Transform that the scene will get parented to |
| int | sceneIndex | Index of the scene to be instantiated |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if the scene was instantiated. False in case of errors. |
Load(byte[], Uri, ImportSettings, CancellationToken)
Load a glTF from a byte array. If the type (JSON or glTF-Binary) is know, LoadGltfJson(string, Uri, ImportSettings, CancellationToken) and LoadGltfBinary(byte[], Uri, ImportSettings, CancellationToken) should be preferred.
Declaration
public Task<bool> Load(byte[] data, Uri uri = null, ImportSettings importSettings = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | Either glTF-Binary data or a glTF JSON |
| Uri | uri | Base URI for relative paths of external buffers or images |
| ImportSettings | importSettings | Import Settings (ImportSettings for details) |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if loading was successful, false otherwise |
Load(string, ImportSettings, CancellationToken)
Load a glTF file (JSON or binary) The URL can be a file path (using the "file://" scheme) or a web address.
Declaration
public Task<bool> Load(string url, ImportSettings importSettings = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | url | Uniform Resource Locator. Can be a file path (using the "file://" scheme) or a web address. |
| ImportSettings | importSettings | Import Settings (ImportSettings for details) |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if loading was successful, false otherwise |
Load(Uri, ImportSettings, CancellationToken)
Load a glTF file (JSON or binary) The URL can be a file path (using the "file://" scheme) or a web address.
Declaration
public Task<bool> Load(Uri url, ImportSettings importSettings = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | url | Uniform Resource Locator. Can be a file path (using the "file://" scheme) or a web address. |
| ImportSettings | importSettings | Import Settings (ImportSettings for details) |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if loading was successful, false otherwise |
LoadFile(string, Uri, ImportSettings, CancellationToken)
Load glTF from a local file path.
Declaration
public Task<bool> LoadFile(string localPath, Uri uri = null, ImportSettings importSettings = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | localPath | Local path to glTF or glTF-Binary file. |
| Uri | uri | Base URI for relative paths of external buffers or images |
| ImportSettings | importSettings | Import Settings (ImportSettings for details) |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if loading was successful, false otherwise |
LoadGltfBinary(byte[], Uri, ImportSettings, CancellationToken)
Load a glTF-binary asset from a byte array.
Declaration
public Task<bool> LoadGltfBinary(byte[] bytes, Uri uri = null, ImportSettings importSettings = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes | byte array containing glTF-binary |
| Uri | uri | Base URI for relative paths of external buffers or images |
| ImportSettings | importSettings | Import Settings (ImportSettings for details) |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if loading was successful, false otherwise |
LoadGltfJson(string, Uri, ImportSettings, CancellationToken)
Load a glTF JSON from a string
Declaration
public Task<bool> LoadGltfJson(string json, Uri uri = null, ImportSettings importSettings = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | json | glTF JSON |
| Uri | uri | Base URI for relative paths of external buffers or images |
| ImportSettings | importSettings | Import Settings (ImportSettings for details) |
| CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if loading was successful, false otherwise |
SetDefaultDeferAgent(IDeferAgent)
Sets the default IDeferAgent for subsequently generated GltfImport instances.
Declaration
public static void SetDefaultDeferAgent(IDeferAgent deferAgent)
Parameters
| Type | Name | Description |
|---|---|---|
| IDeferAgent | deferAgent | New default IDeferAgent |
UnsetDefaultDeferAgent(IDeferAgent)
Allows un-registering default IDeferAgent. For example if it's no longer available.
Declaration
public static void UnsetDefaultDeferAgent(IDeferAgent deferAgent)
Parameters
| Type | Name | Description |
|---|---|---|
| IDeferAgent | deferAgent | IDeferAgent in question |