Class AssetViewer
A TriLib sample that allows the user to load models and HDR skyboxes from the local file-system and URLs. The loaded model can be manipulated (rotated, zoomed) within the scene, and the user can toggle various debug and rendering options.
Inherited Members
Namespace: TriLibCore.Samples
Assembly: Assembly-CSharp.dll
Syntax
public class AssetViewer : AbstractInputSystem
Fields
AssetLoaderOptions
Options controlling how the model is loaded (e.g., whether to import materials, animations, cameras, etc.). If not set in the inspector, a default is created at runtime.
Declaration
public AssetLoaderOptions AssetLoaderOptions
Field Value
Type | Description |
---|---|
AssetLoaderOptions |
CameraAngle
Current angles for camera rotation around the loaded model (x = yaw, y = pitch).
Declaration
public Vector2 CameraAngle
Field Value
Type | Description |
---|---|
Vector2 |
CameraDistance
Current camera distance from the CameraPivot.
Declaration
public float CameraDistance
Field Value
Type | Description |
---|---|
float |
CameraDistanceRatio
Specifies how far the camera should be placed from the loaded model, in relation to the model’s bounding size.
Declaration
public const float CameraDistanceRatio = 2
Field Value
Type | Description |
---|---|
float |
CameraPivot
The position the camera orbits around (usually at or near the model’s center/bounds).
Declaration
public Vector3 CameraPivot
Field Value
Type | Description |
---|---|
Vector3 |
CanvasScaler
Reference to the scene’s CanvasScaler for configuring UI scaling on different devices.
Declaration
[SerializeField]
public CanvasScaler CanvasScaler
Field Value
Type | Description |
---|---|
CanvasScaler |
FilePickerAssetLoader
An instance of the TriLib file picker for selecting models/directories on supported platforms.
Declaration
public AssetLoaderFilePicker FilePickerAssetLoader
Field Value
Type | Description |
---|---|
AssetLoaderFilePicker |
InputMultiplier
Dynamically scales input movement based on the loaded model’s bounding size. Larger models result in a higher InputMultiplier for easier navigation.
Declaration
public float InputMultiplier
Field Value
Type | Description |
---|---|
float |
InputMultiplierRatio
Scales mouse movement inputs (higher means more sensitive movements).
Declaration
public const float InputMultiplierRatio = 0.1
Field Value
Type | Description |
---|---|
float |
MaxPitch
The maximum pitch angle for the camera or directional light (rotation around X-axis). Prevents flipping beyond 80 degrees.
Declaration
public const float MaxPitch = 80
Field Value
Type | Description |
---|---|
float |
MinCameraDistance
The minimum allowable camera distance to the pivot (prevents zooming through the model).
Declaration
public const float MinCameraDistance = 0.01
Field Value
Type | Description |
---|---|
float |
PeakMemory
Tracks the peak memory usage (in bytes) observed during model loading. Reset each time a new model is loaded.
Declaration
public long PeakMemory
Field Value
Type | Description |
---|---|
long |
Play
Button (or other UnityEngine.UI.Selectable) used to trigger animation playback.
Declaration
[SerializeField]
public Selectable Play
Field Value
Type | Description |
---|---|
Selectable |
PlaybackAnimation
Dropdown listing available animation clips on the loaded model. Allows the user to switch between clips.
Declaration
[SerializeField]
public Dropdown PlaybackAnimation
Field Value
Type | Description |
---|---|
Dropdown |
PlaybackSlider
Slider representing the normalized time of the current animation. Allows manual scrubbing through the clip if the animation is not playing.
Declaration
[SerializeField]
public Slider PlaybackSlider
Field Value
Type | Description |
---|---|
Slider |
PlaybackTime
Displays the current animation time in a mm:ss
format.
Declaration
[SerializeField]
public Text PlaybackTime
Field Value
Type | Description |
---|---|
Text |
Skybox
A dedicated UnityEngine.GameObject for rendering the HDR skybox.
Declaration
[SerializeField]
public GameObject Skybox
Field Value
Type | Description |
---|---|
GameObject |
SkyboxScale
Used for scaling the skybox in relation to the loaded model’s bounding size.
Declaration
public const float SkyboxScale = 100
Field Value
Type | Description |
---|---|
float |
Stop
Button (or other UnityEngine.UI.Selectable) used to stop animation playback.
Declaration
[SerializeField]
public Selectable Stop
Field Value
Type | Description |
---|---|
Selectable |
Properties
Instance
A singleton reference to the active AssetViewer in the scene, established in Start().
Declaration
public static AssetViewer Instance { get; }
Property Value
Type | Description |
---|---|
AssetViewer |
RootGameObject
The root UnityEngine.GameObject of the currently loaded model. If a new model is loaded, the previous root is destroyed and replaced.
Declaration
public GameObject RootGameObject { get; set; }
Property Value
Type | Description |
---|---|
GameObject |
Methods
CameraChanged(int)
Switches between cameras (if any) embedded in the loaded model and the default user camera. Triggered by the “Camera” dropdown TriLibCore.Samples.AssetViewer._camerasDropdown.
Declaration
public void CameraChanged(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The dropdown index indicating which camera to enable. |
ClearSkybox()
Removes the existing HDR skybox texture, setting the skybox to a default blank state.
Declaration
public void ClearSkybox()
HideHelp()
Hides the help or usage instructions panel.
Declaration
public void HideHelp()
HideModelUrlDialog()
Hides the model URL dialog and clears the entered URL text.
Declaration
public void HideModelUrlDialog()
IsLoading()
Indicates whether a model (or skybox) is currently being loaded.
Declaration
public bool IsLoading()
Returns
Type | Description |
---|---|
bool | True if the system is loading; otherwise, false. |
LoadModelFromDirectory()
Displays a folder picker dialog to load a model from all files in the chosen directory. The AssetLoaderOptions are updated with user toggles before loading.
Declaration
public void LoadModelFromDirectory()
LoadModelFromFile()
Displays a file picker dialog to select a single model file. Updates AssetLoaderOptions based on UI toggles before loading.
Declaration
public void LoadModelFromFile()
LoadModelFromURL(UnityWebRequest, string, GameObject, object, Action<AssetLoaderContext>)
Loads a model from a remote URL, optionally handling zip archives if fileExtension
indicates a “.zip”. Integrates with TriLib’s AssetDownloader for network downloads.
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 the model. |
string | fileExtension | File extension (e.g., |
GameObject | wrapperGameObject | Optional parent for the loaded model’s root UnityEngine.GameObject. |
object | customData | Optional user data passed into TriLib load methods. |
Action<AssetLoaderContext> | onMaterialsLoad | Optional callback after textures and materials finish loading. |
LoadModelFromURLWithDialogValues()
Loads a model from the URL the user entered into TriLibCore.Samples.AssetViewer._modelUrl. Invokes LoadModelFromURL(UnityWebRequest, string, GameObject, object, Action<AssetLoaderContext>) once the user commits the address.
Declaration
public void LoadModelFromURLWithDialogValues()
LoadSkyboxFromFile()
Opens a file picker dialog to select an HDR image for the skybox.
Declaration
public void LoadSkyboxFromFile()
OnBeginLoadModel(bool)
Called when the user either selects a file/directory/url or cancels the dialog. If a file was chosen, clears previous model data and starts tracking load time.
Declaration
public void OnBeginLoadModel(bool hasFiles)
Parameters
Type | Name | Description |
---|---|---|
bool | hasFiles | True if the user selected a file/directory, otherwise false (canceled). |
OnDebugOptionsDropdownChanged(int)
Invoked when the user changes an option in the debug dropdown, switching between various debug shaders (e.g., normals, albedo) or showing/hiding the skeleton.
Declaration
public void OnDebugOptionsDropdownChanged(int value)
Parameters
Type | Name | Description |
---|---|---|
int | value | The zero-based index of the debug option selected. |
OnError(IContextualizedError)
Called if any error occurs during model loading. Displays the error message in the UI, logs it to the console, stops animation playback, and disables the loading state.
Declaration
public void OnError(IContextualizedError contextualizedError)
Parameters
Type | Name | Description |
---|---|---|
IContextualizedError | contextualizedError | Provides context on the error, including the exception and potentially related data. |
OnFastLoadToggleChanged(bool)
Toggles “Fast Mode” on or off. When on, certain advanced settings (e.g., advanced geometry processing) are disabled to speed up loading, at the cost of skipping some features.
Declaration
public void OnFastLoadToggleChanged(bool value)
Parameters
Type | Name | Description |
---|---|---|
bool | value | True if fast load mode is toggled on, false otherwise. |
OnLoad(AssetLoaderContext)
Called once the model’s meshes and hierarchy are loaded (but before textures/materials). Resets certain viewer state, configures cameras, handles animations, etc.
Declaration
public void OnLoad(AssetLoaderContext assetLoaderContext)
Parameters
Type | Name | Description |
---|---|---|
AssetLoaderContext | assetLoaderContext | Context containing the newly loaded root UnityEngine.GameObject and other references. |
OnMaterialsLoad(AssetLoaderContext)
Invoked once textures and materials finish loading, indicating the model is fully ready. Disables the loading UI, logs load time, and handles post-load actions (e.g., skeleton display).
Declaration
public void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
Parameters
Type | Name | Description |
---|---|---|
AssetLoaderContext | assetLoaderContext | Provides references to the fully loaded model, including |
OnModelTransformChanged()
Repositions the camera relative to the loaded model bounds whenever the model’s transform changes. Adjusts the skybox scale and sets CameraDistance accordingly.
Declaration
public void OnModelTransformChanged()
OnPointCloudToggleChanged(bool)
Toggles point cloud loading. Also toggles visibility of the point size UI slider/label.
Declaration
public void OnPointCloudToggleChanged(bool value)
Parameters
Type | Name | Description |
---|---|---|
bool | value | True if point clouds should be loaded, false otherwise. |
OnPointSizeSliderChanged(float)
Called when the point size slider value changes, updating the size of any PointRenderer components rendering point clouds.
Declaration
public void OnPointSizeSliderChanged(float value)
Parameters
Type | Name | Description |
---|---|---|
float | value | A float representing the new point size. |
OnProgress(AssetLoaderContext, float)
Reports loading progress for the current model (0.0 to 1.0). Updates the loading bar’s width accordingly.
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 representing the loading progress. |
OnSkyboxExposureChanged(float)
Adjusts the skybox exposure level based on exposure
,
updating the reflection probe to reflect the new brightness.
Declaration
public void OnSkyboxExposureChanged(float exposure)
Parameters
Type | Name | Description |
---|---|---|
float | exposure | A float representing the skybox exposure multiplier. |
PlayAnimation()
Begins playback of the currently selected animation (if any). Uses the name from PlaybackAnimation to identify the clip.
Declaration
public void PlayAnimation()
PlaybackAnimationChanged(int)
When the animation selection changes in PlaybackAnimation, stops the currently playing animation to allow switching to the new one.
Declaration
public void PlaybackAnimationChanged(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The zero-based index for the selected animation option. |
PlaybackSliderChanged(float)
Invoked when the PlaybackSlider value changes. If no animation is playing, updates the RootGameObject to display the current frame of the selected animation at that normalized time.
Declaration
public void PlaybackSliderChanged(float value)
Parameters
Type | Name | Description |
---|---|---|
float | value | Normalized time [0..1] for the animation. |
ProcessInput()
Processes input each frame, such as rotating, zooming, or panning the camera, but only if the primary TriLibCore.Samples.AssetViewer._mainCamera is active.
Declaration
public void ProcessInput()
ResetModelScale()
Resets the loaded model’s scale to Vector3.one
if a model is present.
Called after loading or reloading a model to clear any previously applied scaling.
Declaration
public void ResetModelScale()
SetCustomBounds(Bounds)
Adjusts the camera to fit a specific bounding volume (e.g., after user modifications), updating CameraDistance, CameraPivot, and skybox scaling.
Declaration
public void SetCustomBounds(Bounds bounds)
Parameters
Type | Name | Description |
---|---|---|
Bounds | bounds | The bounding volume to fit the camera to. |
SetLoading(bool)
Toggles loading state UI, disabling or enabling UnityEngine.UI.Selectable elements, and showing or hiding the TriLibCore.Samples.AssetViewer._loadingWrapper and TriLibCore.Samples.AssetViewer._loadingBar.
Declaration
public void SetLoading(bool value)
Parameters
Type | Name | Description |
---|---|---|
bool | value | If true, shows loading UI; false hides loading UI. |
ShowHelp()
Displays the help panel.
Declaration
public void ShowHelp()
ShowModelUrlDialog()
Shows the input field and dialog for loading a model from a remote URL.
Declaration
public void ShowModelUrlDialog()
Start()
Initializes this AssetViewer, setting up references, toggles, and default skybox conditions. Called automatically on scene load.
Declaration
public void Start()
StopAnimation()
Stops playback of the currently selected animation and resets its time to zero.
Declaration
public void StopAnimation()
UpdateCamera()
Updates camera angles based on mouse movement (pitch and yaw), applying InputMultiplierRatio and clamping pitch within MaxPitch.
Declaration
public void UpdateCamera()