Class GltfImportBase
Loads a glTF's content, converts it to Unity resources and is able to feed it to an IInstantiator for instantiation.
Inherited Members
Namespace: GLTFast
Assembly: glTFast.dll
Syntax
public abstract class GltfImportBase : IGltfReadable, IMaterialProvider, IMaterialsVariantsProvider, IDisposable
Constructors
GltfImportBase(IDownloadProvider, IDeferAgent, IMaterialGenerator, ICodeLogger)
Constructs a GltfImport instance with injectable customization objects.
Declaration
public GltfImportBase(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 |
MaterialsVariantsCount
Number of materials variants.
Declaration
public int MaterialsVariantsCount { get; }
Property Value
Type | Description |
---|---|
int |
See Also
Root
Main glTF data structure
Declaration
protected abstract RootBase Root { get; set; }
Property Value
Type | Description |
---|---|
RootBase |
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
AddImportAddonInstance<T>(T)
Adds an import add-on instance. To be called before any loading is initiated.
Declaration
public void AddImportAddonInstance<T>(T importInstance) where T : ImportAddonInstance
Parameters
Type | Name | Description |
---|---|---|
T | importInstance | The import instance to add. |
Type Parameters
Name | Description |
---|---|
T | Type of the import instance |
Dispose()
Frees up memory by disposing all sub assets. There can be no instantiation or other element access afterwards.
Declaration
public void Dispose()
GetAccessor(int)
Creates a generic byte-array view into an accessor. Only available during loading phase as underlying buffers are disposed right afterwards.
Declaration
public NativeSlice<byte> GetAccessor(int accessorIndex)
Parameters
Type | Name | Description |
---|---|---|
int | accessorIndex | glTF accessor index |
Returns
Type | Description |
---|---|
NativeSlice<byte> | Valid byte-slice view into accessor's data if parameter was correct and buffers are available. Zero-length slice otherwise. |
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 |
GetDefaultMaterialAsync()
Returns a fallback material to be used when no material was assigned (provided by the IMaterialGenerator)
Declaration
public Task<Material> GetDefaultMaterialAsync()
Returns
Type | Description |
---|---|
Task<Material> | Default material |
GetDefaultMaterialAsync(CancellationToken)
Returns a fallback material to be used when no material was assigned (provided by the IMaterialGenerator)
Declaration
public Task<Material> GetDefaultMaterialAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
Type | Description |
---|---|
Task<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 |
GetImportAddonInstance<T>()
Queries the import instance of a particular type.
Declaration
public T GetImportAddonInstance<T>() where T : ImportAddonInstance
Returns
Type | Description |
---|---|
T | The import instance that was previously added. False if there was none. |
Type Parameters
Name | Description |
---|---|
T | Type of the import instance |
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 |
GetMaterialAsync(int)
Get a Unity Material by its glTF material index
Declaration
public Task<Material> GetMaterialAsync(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | glTF material index |
Returns
Type | Description |
---|---|
Task<Material> | Corresponding Unity Material |
GetMaterialAsync(int, CancellationToken)
Get a Unity Material by its glTF material index
Declaration
public Task<Material> GetMaterialAsync(int index, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
int | index | glTF material index |
CancellationToken | cancellationToken | Token to submit cancellation requests. The default value is None. |
Returns
Type | Description |
---|---|
Task<Material> | Corresponding Unity Material |
GetMaterialsVariantName(int)
Gets the name of a specific materials variant.
Declaration
public string GetMaterialsVariantName(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | Materials variant index. |
Returns
Type | Description |
---|---|
string | Name of a materials variant. |
See Also
GetMaterialsVariantsSlots(int, int)
Returns the material slots that correspond to the given MeshResult's sub-meshes.
Declaration
public IMaterialsVariantsSlot[] GetMaterialsVariantsSlots(int meshIndex, int meshResultOffset)
Parameters
Type | Name | Description |
---|---|---|
int | meshIndex | glTF mesh index. |
int | meshResultOffset | Mesh result offset. |
Returns
Type | Description |
---|---|
IMaterialsVariantsSlot[] | Corresponding materials variants slots. |
See Also
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 CameraBase GetSourceCamera(uint index)
Parameters
Type | Name | Description |
---|---|---|
uint | index | glTF camera index |
Returns
Type | Description |
---|---|
CameraBase | 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 MaterialBase GetSourceMaterial(int index = 0)
Parameters
Type | Name | Description |
---|---|---|
int | index | glTF material index |
Returns
Type | Description |
---|---|
MaterialBase | De-serialized glTF material |
GetSourceMeshPrimitive(int, int)
Get source (de-serialized glTF) mesh primitive
Declaration
public MeshPrimitiveBase GetSourceMeshPrimitive(int meshIndex, int primitiveIndex)
Parameters
Type | Name | Description |
---|---|---|
int | meshIndex | glTF mesh index. |
int | primitiveIndex | glTF primitive index within mesh. |
Returns
Type | Description |
---|---|
MeshPrimitiveBase | De-serialized glTF mesh primitive |
GetSourceNode(int)
Get source (de-serialized glTF) node
Declaration
public NodeBase GetSourceNode(int index = 0)
Parameters
Type | Name | Description |
---|---|---|
int | index | glTF node index |
Returns
Type | Description |
---|---|
NodeBase | De-serialized glTF node |
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 TextureBase GetSourceTexture(int index = 0)
Parameters
Type | Name | Description |
---|---|---|
int | index | glTF texture index |
Returns
Type | Description |
---|---|
TextureBase | 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) 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. |
See Also
InstantiateMainScene(Transform)
Creates an instance of the main scene of the glTF ( scene property in the JSON at root level) 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. |
See Also
InstantiateMainSceneAsync(IInstantiator, CancellationToken)
Creates an instance of the main scene of the glTF ( scene property in the JSON at root level) 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. |
See Also
InstantiateMainSceneAsync(Transform, CancellationToken)
Creates an instance of the main scene of the glTF ( scene property in the JSON at root level) 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. |
See Also
InstantiateScene(IInstantiator, int)
Creates an instance of the scene specified by the scene index.
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. |
See Also
InstantiateScene(Transform, int)
Creates an instance of the scene specified by the scene index.
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. |
See Also
InstantiateSceneAsync(IInstantiator, int, CancellationToken)
Creates an instance of the scene specified by the scene index.
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. |
See Also
InstantiateSceneAsync(Transform, int, CancellationToken)
Creates an instance of the scene specified by the scene index.
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. |
See Also
IsTextureYFlipped(int)
Evaluates if the texture's vertical orientation conforms to Unity's default. If it's not aligned (=true; =flipped), the texture has to be applied mirrored vertically.
Declaration
public bool IsTextureYFlipped(int index = 0)
Parameters
Type | Name | Description |
---|---|---|
int | index | glTF texture index |
Returns
Type | Description |
---|---|
bool | True if the vertical orientation is flipped, false otherwise |
Load(byte[], Uri, ImportSettings, CancellationToken)
Load a glTF from a byte array. If the type (JSON or glTF-Binary) is known, 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 |
LoadStream(Stream, Uri, ImportSettings, CancellationToken)
Load glTF from a stream.
Declaration
public Task<bool> LoadStream(Stream stream, Uri uri = null, ImportSettings importSettings = null, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | Stream of the glTF or 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 |
ParseJson(string)
De-serializes a glTF JSON string and returns the glTF root schema object.
Declaration
protected abstract RootBase ParseJson(string json)
Parameters
Type | Name | Description |
---|---|---|
string | json | glTF JSON |
Returns
Type | Description |
---|---|
RootBase | De-serialized glTF root object. |
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 |
Events
LoadAccessorDataEvent
Is called when retrieving data from accessors should be performed/started.
Declaration
public event Action LoadAccessorDataEvent
Event Type
Type | Description |
---|---|
Action |
MeshResultAssigned
Is called when a mesh and its primitives are assigned to a MeshResult and sub-meshes. Parameters are MeshResult index, mesh index and per sub-mesh primitive index
Declaration
public event Action<int, int, int[]> MeshResultAssigned
Event Type
Type | Description |
---|---|
Action<int, int, int[]> |