#pragma warning disable 672
using System;
using System.IO;
using TriLibCore.Interfaces;
using TriLibCore.Mappers;
using TriLibCore.Utils;
using UnityEngine;
namespace TriLibCore.Samples
{
///
/// Provides static methods for loading 3D models from byte arrays or objects.
/// This class integrates custom data and texture mappers via user-supplied callbacks, enabling
/// flexible loading scenarios such as in-memory data, custom file paths, or network streams.
///
public class SimpleCustomAssetLoader
{
///
/// Loads a model from an in-memory byte array, using callbacks to handle events and external data.
///
/// The raw byte array containing the model data.
/// The model file extension (e.g., ".fbx", ".obj"). If omitted, you can provide a to infer the extension.
/// An optional callback invoked if an error occurs during loading.
/// A callback invoked to report loading progress, where the float parameter goes from 0.0 to 1.0.
/// A callback invoked once the model is fully loaded (including textures and materials).
/// A required callback for obtaining external file data streams when additional files are referenced.
/// An optional callback to resolve or modify the final filename from the original reference.
/// A required callback for obtaining texture data streams.
/// An optional filename to associate with the model. If provided, TriLib may use this to infer the file extension.
/// An optional to serve as a parent for the loaded model’s root object.
/// An optional set of loading options for finer control over the model load process.
/// Any custom user data that should be passed through TriLib’s loading pipeline and accessible in callbacks.
/// An containing references to the loaded root and other metadata.
/// Thrown if is null or empty.
public static AssetLoaderContext LoadModelFromByteData(
byte[] data,
string modelExtension,
Action onError,
Action onProgress,
Action onModelFullyLoad,
Func customDataReceivingCallback,
Func customFilenameReceivingCallback,
Func customTextureReceivingCallback,
string modelFilename = null,
GameObject wrapperGameObject = null,
AssetLoaderOptions assetLoaderOptions = null,
object customData = null)
{
if (data == null || data.Length == 0)
{
throw new Exception("Missing model file byte data.");
}
// Create a MemoryStream from the provided byte array and forward to the overload that handles streams
return LoadModelFromStream(
new MemoryStream(data),
modelExtension,
onError,
onProgress,
onModelFullyLoad,
customDataReceivingCallback,
customFilenameReceivingCallback,
customTextureReceivingCallback,
modelFilename,
wrapperGameObject,
assetLoaderOptions,
customData
);
}
///
/// Loads a model from a , using callbacks to handle external data and textures.
///
///
/// This method is useful if you have already prepared a , such as from a custom
/// data source, a file, or network operation. You can attach event callbacks to monitor progress,
/// receive errors, and handle external dependencies or textures.
///
/// The containing the model data.
/// The model file extension (e.g., ".fbx", ".obj"). This is used by TriLib to determine import logic.
/// An optional callback invoked if an error occurs during loading.
/// A callback to report loading progress, from 0.0 (start) to 1.0 (fully loaded).
/// A callback invoked once the model is fully loaded (including meshes, materials, and textures).
/// A required callback for obtaining streams to any external file data the model references.
/// An optional callback to modify or resolve filenames before loading.
/// A required callback for obtaining texture data streams.
/// An optional filename to represent the model. If an extension is not provided in , it will be derived from this parameter.
/// An optional that will become the parent of the loaded model’s root .
/// Optional loading options to customize import settings, scaling, etc.
/// Any additional user data to be passed through the loading pipeline and accessible within callbacks.
/// An containing the loaded model’s references and metadata.
/// Thrown if is null or if the model extension cannot be resolved.
public static AssetLoaderContext LoadModelFromStream(
Stream stream,
string modelExtension,
Action onError,
Action onProgress,
Action onModelFullyLoad,
Func customDataReceivingCallback,
Func customFilenameReceivingCallback,
Func customTextureReceivingCallback,
string modelFilename = null,
GameObject wrapperGameObject = null,
AssetLoaderOptions assetLoaderOptions = null,
object customData = null)
{
if (stream == null)
{
throw new Exception("Missing model file stream.");
}
// Attempt to infer the file extension if it is not provided
if (string.IsNullOrWhiteSpace(modelExtension) && !string.IsNullOrWhiteSpace(modelFilename))
{
modelExtension = FileUtils.GetFileExtension(modelFilename);
}
if (string.IsNullOrWhiteSpace(modelExtension))
{
throw new Exception("Missing model extension parameter.");
}
// Create instances of our custom data mappers
var simpleExternalDataMapper = ScriptableObject.CreateInstance();
simpleExternalDataMapper.Setup(customDataReceivingCallback, customFilenameReceivingCallback);
var simpleTextureMapper = ScriptableObject.CreateInstance();
simpleTextureMapper.Setup(customTextureReceivingCallback);
// If no AssetLoaderOptions are provided, create a default set
if (assetLoaderOptions == null)
{
assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
}
// Inject the custom data and texture mappers into the AssetLoaderOptions
assetLoaderOptions.ExternalDataMapper = simpleExternalDataMapper;
assetLoaderOptions.TextureMappers = new TextureMapper[] { simpleTextureMapper };
// Use the standard TriLib stream-loading mechanism with custom mappers and callbacks
return AssetLoader.LoadModelFromStream(
stream,
modelFilename,
modelExtension,
null,
onModelFullyLoad,
onProgress,
onError,
wrapperGameObject,
assetLoaderOptions,
customData
);
}
}
}