Class AvatarLoader
Demonstrates how to load and control a custom humanoid avatar using TriLib. This class extends AbstractInputSystem to handle user input, integrate TriLib’s file-loading and network-loading features, and manage avatar loading and scaling in conjunction with AvatarController.
Inherited Members
Namespace: TriLibCore.Samples
Assembly: Assembly-CSharp.dll
Syntax
public class AvatarLoader : AbstractInputSystem
Fields
AssetLoaderOptions
The TriLib loader options used for model loading in this sample. These options can be preconfigured to support humanoid animation or other specialized settings.
Declaration
public AssetLoaderOptions AssetLoaderOptions
Field Value
Type | Description |
---|---|
AssetLoaderOptions |
CameraAngle
Holds the current camera pitch (Y-axis rotation) and yaw (X-axis rotation), allowing the camera to orbit around the loaded model.
Declaration
public Vector2 CameraAngle
Field Value
Type | Description |
---|---|
Vector2 |
FilePickerAssetLoader
The AssetLoaderFilePicker created for this viewer, enabling file or directory selection for model loading.
Declaration
public AssetLoaderFilePicker FilePickerAssetLoader
Field Value
Type | Description |
---|---|
AssetLoaderFilePicker |
InputMultiplierRatio
A ratio that scales mouse input. Larger values make camera movement more sensitive to mouse movement.
Declaration
public const float InputMultiplierRatio = 0.1
Field Value
Type | Description |
---|---|
float |
MaxPitch
The maximum pitch angle (rotation around the local X-axis) to prevent flipping the camera.
Declaration
public const float MaxPitch = 80
Field Value
Type | Description |
---|---|
float |
PeakMemory
Tracks the peak memory usage (in bytes) observed during model loading.
Declaration
public long PeakMemory
Field Value
Type | Description |
---|---|
long |
Play
A UnityEngine.UI.Selectable (e.g., a button) that triggers animation playback.
Declaration
[SerializeField]
public Selectable Play
Field Value
Type | Description |
---|---|
Selectable |
PlaybackAnimation
A UnityEngine.UI.Dropdown used to select different animations for playback.
Declaration
[SerializeField]
public Dropdown PlaybackAnimation
Field Value
Type | Description |
---|---|
Dropdown |
PlaybackSlider
A UnityEngine.UI.Slider controlling animation playback. Displays or manipulates the normalized time of the current animation.
Declaration
[SerializeField]
public Slider PlaybackSlider
Field Value
Type | Description |
---|---|
Slider |
PlaybackTime
A UnityEngine.UI.Text used to display the current time or frame within the played animation.
Declaration
[SerializeField]
public Text PlaybackTime
Field Value
Type | Description |
---|---|
Text |
Stop
A UnityEngine.UI.Selectable (e.g., a button) that stops the current animation.
Declaration
[SerializeField]
public Selectable Stop
Field Value
Type | Description |
---|---|
Selectable |
Properties
Instance
A static instance of this class, allowing simple global access. Set in Start(), ensuring there is only one active AvatarLoader.
Declaration
public static AvatarLoader Instance { get; }
Property Value
Type | Description |
---|---|
AvatarLoader |
RootGameObject
A reference to the loaded avatar’s root UnityEngine.GameObject. This is set once meshes and hierarchy are loaded, and can be destroyed or replaced when the user loads a new model.
Declaration
public GameObject RootGameObject { get; set; }
Property Value
Type | Description |
---|---|
GameObject |
Methods
HideHelp()
Hides the help overlay or panel in the UI.
Declaration
public void HideHelp()
HideModelUrlDialog()
Hides the URL loading dialog, clearing any typed model URL.
Declaration
public void HideModelUrlDialog()
LoadAvatarFromFile()
Loads an avatar from a file, using the internal wrapper object.
This method delegates the actual file loading operation to LoadModelFromFile
.
Declaration
public void LoadAvatarFromFile()
LoadModelFromDirectory(GameObject, Action<AssetLoaderContext>)
Displays a directory picker dialog so the user can select a folder containing
model files to load. Optionally sets the loaded model’s parent to wrapperGameObject
.
Declaration
public void LoadModelFromDirectory(GameObject wrapperGameObject = null, Action<AssetLoaderContext> onMaterialsLoad = null)
Parameters
Type | Name | Description |
---|---|---|
GameObject | wrapperGameObject | An optional object to serve as the loaded model’s parent. |
Action<AssetLoaderContext> | onMaterialsLoad | An optional callback to override OnMaterialsLoad(AssetLoaderContext). |
LoadModelFromFile(GameObject, Action<AssetLoaderContext>)
Displays a file picker dialog so the user can select a local model file
(e.g., FBX, OBJ) to load. If a wrapperGameObject
is provided,
TriLib will place the loaded model under that object’s hierarchy.
Declaration
public void LoadModelFromFile(GameObject wrapperGameObject = null, Action<AssetLoaderContext> onMaterialsLoad = null)
Parameters
Type | Name | Description |
---|---|---|
GameObject | wrapperGameObject | An optional object to serve as the loaded model’s parent. |
Action<AssetLoaderContext> | onMaterialsLoad | An optional callback to override OnMaterialsLoad(AssetLoaderContext). |
LoadModelFromURL(UnityWebRequest, string, GameObject, object, Action<AssetLoaderContext>)
Loads a model from a custom UnityEngine.Networking.UnityWebRequest and file extension,
potentially handling zip archives if fileExtension
indicates so.
Declaration
public void LoadModelFromURL(UnityWebRequest request, string fileExtension, GameObject wrapperGameObject = null, object customData = null, Action<AssetLoaderContext> onMaterialsLoad = null)
Parameters
Type | Name | Description |
---|---|---|
UnityWebRequest | request | A UnityEngine.Networking.UnityWebRequest pointing to a model file. |
string | fileExtension | The file extension (e.g., |
GameObject | wrapperGameObject | An optional object to serve as the loaded model’s parent. |
object | customData | Optional user data to pass along with the load process. |
Action<AssetLoaderContext> | onMaterialsLoad | Optional callback for completion logic after textures/materials load. |
Exceptions
Type | Condition |
---|---|
Exception | Thrown if |
LoadModelFromURLWithDialogValues()
Loads a model from the URL specified in the URL dialog’s input field. Closes the dialog once loading begins.
Declaration
public void LoadModelFromURLWithDialogValues()
OnBeginLoadModel(bool)
Invoked when the user selects a file or directory in the picker, or cancels the selection. Resets the scene if a file was chosen.
Declaration
public void OnBeginLoadModel(bool hasFiles)
Parameters
Type | Name | Description |
---|---|---|
bool | hasFiles | True if the user selected a file/directory; false if the user canceled. |
OnError(IContextualizedError)
Invoked if any error occurs while loading the model. Logs the error, clears the reference to the loaded model, and disables the loading UI.
Declaration
public void OnError(IContextualizedError contextualizedError)
Parameters
Type | Name | Description |
---|---|---|
IContextualizedError | contextualizedError | An object containing exception details and any relevant load context. |
OnLoad(AssetLoaderContext)
Invoked once the model’s meshes and hierarchy have been loaded, but before textures and materials are processed. Resets memory usage counters in this sample. Specific logic (e.g., camera fitting) may be placed here.
Declaration
public void OnLoad(AssetLoaderContext assetLoaderContext)
Parameters
Type | Name | Description |
---|---|---|
AssetLoaderContext | assetLoaderContext | Contains references and data about the loaded model. |
OnMaterialsLoad(AssetLoaderContext)
Invoked after textures and materials are loaded, indicating the model is fully ready. Disables the loading screen and, if needed, integrates the newly loaded model with the AvatarController.
Declaration
public void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
Parameters
Type | Name | Description |
---|---|---|
AssetLoaderContext | assetLoaderContext | The AssetLoaderContext with references to the loaded UnityEngine.GameObject. |
OnProgress(AssetLoaderContext, float)
Invoked to report model loading progress, receiving a value [0..1]. Updates the UI loading bar’s width to reflect current progress.
Declaration
public void OnProgress(AssetLoaderContext assetLoaderContext, float value)
Parameters
Type | Name | Description |
---|---|---|
AssetLoaderContext | assetLoaderContext | Provides info about the current load process. |
float | value | A float from 0.0 to 1.0 indicating loading progress. |
SetLoading(bool)
Enables or disables the loading state. Disabling the loading state re-enables all UnityEngine.UI.Selectable UI elements and hides the loading wrapper/indicator.
Declaration
public void SetLoading(bool value)
Parameters
Type | Name | Description |
---|---|---|
bool | value | True if loading is active, false otherwise. |
ShowHelp()
Displays a help overlay or panel in the UI.
Declaration
public void ShowHelp()
ShowModelUrlDialog()
Displays the URL loading dialog, focusing the input field so the user can paste or type a model URL.
Declaration
public void ShowModelUrlDialog()
Start()
Called by Unity when the script instance is first enabled. Sets up singletons, configures default AssetLoaderOptions for humanoid avatars, and adjusts the scale of any already-loaded avatar.
Declaration
public void Start()
UpdateCamera()
Updates the camera angles (pitch and yaw) based on mouse input, applying InputMultiplierRatio to control sensitivity and limiting pitch to avoid flipping the camera.
Declaration
public void UpdateCamera()