UsoUIElements 0.1.0
Data Centric implimentation of Unity's UI Elements (UI Toolkit)
Loading...
Searching...
No Matches
GWG.UsoUIElements.CustomElements.PrefabPreviewElement Class Reference

A custom UI element that provides a visual preview of a GameObject prefab along with its detailed information. This element combines an ObjectField for prefab selection with an automatically generated preview image and metadata display. More...

Inheritance diagram for GWG.UsoUIElements.CustomElements.PrefabPreviewElement:

Public Member Functions

 PrefabPreviewElement ()
 Initializes a new instance of the PrefabPreviewElement, setting up the UI structure and styling. Creates and configures all child elements including the ObjectField, preview image, and detail labels.
void SetValueWithoutNotify (Object newValue)
 Sets the value of the element without triggering change notifications or events. Updates the internal GameObject reference, preview image, and ObjectField display.

Static Public Attributes

static readonly string ussContainerClassName = "prefab-preview-container"
 The CSS class name applied to the main container of the prefab preview element.
static readonly string ussDetailsClassName = "prefab-preview-details"
 The CSS class name applied to the details text column containing prefab metadata.
static readonly string ussElementClassName = "prefab-preview-element"
 The CSS class name applied to the preview image element.
static readonly string ussStylesheetPath = "prefab_preview_element"
 The path to the USS stylesheet resource that styles this element.

Properties

Object value [get, set]
 Gets or sets the current GameObject value of the element. Setting this property triggers change events and updates the preview display.

Private Member Functions

void OnObjectFieldValueChanged (ChangeEvent< Object > evt)
 Handles value changes from the ObjectField and updates the element's value accordingly. This method is called when the user selects a different GameObject in the ObjectField.
void SetPreview (Object newValue)
 Generates and sets the preview image and details for the specified GameObject. This method handles the asynchronous nature of Unity's AssetPreview system with a scheduled execution.

Private Attributes

VisualElement m_DetailsWrapper
 Container element that wraps the preview image and details text column.
ObjectField m_ObjectField
 The ObjectField that allows users to select and assign GameObject prefabs.
Label m_PrefabName
 Label that displays the name of the selected prefab.
Label m_PrefabTags
 Label that displays the tags associated with the selected prefab.
Label m_PrefabType
 Label that displays the type information of the selected prefab.
Label m_PrefabUniqueId
 Label that displays the unique instance ID of the selected prefab.
Texture2D m_Preview
 The cached preview texture generated for the current prefab.
Image m_PreviewElement
 The Image element that displays the visual preview of the selected prefab.
GameObject m_Value
 The currently selected GameObject prefab value.

Detailed Description

A custom UI element that provides a visual preview of a GameObject prefab along with its detailed information. This element combines an ObjectField for prefab selection with an automatically generated preview image and metadata display.

This element implements INotifyValueChanged to support Unity's data binding system and inherits from BindableElement. The preview image is generated asynchronously using Unity's AssetPreview system, with a delay to ensure proper loading. The element displays prefab name, unique instance ID, and tags alongside the visual preview. Requires Unity Editor as it uses AssetPreview and ObjectField functionality.

Constructor & Destructor Documentation

◆ PrefabPreviewElement()

GWG.UsoUIElements.CustomElements.PrefabPreviewElement.PrefabPreviewElement ( )

Initializes a new instance of the PrefabPreviewElement, setting up the UI structure and styling. Creates and configures all child elements including the ObjectField, preview image, and detail labels.

The constructor loads the associated stylesheet, configures the ObjectField for GameObject selection, creates the preview image and detail labels, and sets up the initial layout structure. The details wrapper is initially hidden and will be shown when a valid prefab preview is available.

Member Function Documentation

◆ OnObjectFieldValueChanged()

void GWG.UsoUIElements.CustomElements.PrefabPreviewElement.OnObjectFieldValueChanged ( ChangeEvent< Object > evt)
private

Handles value changes from the ObjectField and updates the element's value accordingly. This method is called when the user selects a different GameObject in the ObjectField.

Parameters
evtThe change event containing the old and new values from the ObjectField.

This callback ensures that changes in the ObjectField are properly propagated to the element's value property, which will trigger the preview update and notification events.

◆ SetPreview()

void GWG.UsoUIElements.CustomElements.PrefabPreviewElement.SetPreview ( Object newValue)
private

Generates and sets the preview image and details for the specified GameObject. This method handles the asynchronous nature of Unity's AssetPreview system with a scheduled execution.

Parameters
newValueThe GameObject to generate a preview for, or null to clear the preview.

The method uses Unity's AssetPreview.GetAssetPreview to generate a visual representation of the prefab. Due to the asynchronous nature of preview generation, the method schedules an execution with a 2-frame delay to ensure the preview texture is available. When successful, it populates the preview image and all detail labels, then makes the details wrapper visible. If the preview generation fails, the details remain hidden.

◆ SetValueWithoutNotify()

void GWG.UsoUIElements.CustomElements.PrefabPreviewElement.SetValueWithoutNotify ( Object newValue)

Sets the value of the element without triggering change notifications or events. Updates the internal GameObject reference, preview image, and ObjectField display.

Parameters
newValueThe new GameObject value to set, or null to clear the selection.
Exceptions
ArgumentExceptionThrown when the newValue is not null and not a GameObject.

This method is used internally to update the element's state without triggering change events, which is important for avoiding infinite loops during value synchronization. The method validates that the provided value is either null or a GameObject before proceeding.

Member Data Documentation

◆ m_DetailsWrapper

VisualElement GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_DetailsWrapper
private

Container element that wraps the preview image and details text column.

This element uses horizontal flex direction to arrange the preview and details side by side. It's initially hidden and becomes visible when a valid prefab is selected and preview is generated.

◆ m_ObjectField

ObjectField GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_ObjectField
private

The ObjectField that allows users to select and assign GameObject prefabs.

This field is configured to only accept GameObject types and triggers preview updates when its value changes.

◆ m_PrefabName

Label GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_PrefabName
private

Label that displays the name of the selected prefab.

Shows the prefab's name with HTML formatting for better visual presentation.

◆ m_PrefabTags

Label GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_PrefabTags
private

Label that displays the tags associated with the selected prefab.

Shows the Unity tags assigned to the prefab with HTML formatting for better presentation.

◆ m_PrefabType

Label GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_PrefabType
private

Label that displays the type information of the selected prefab.

Currently defined but not actively populated with type information in the current implementation.

◆ m_PrefabUniqueId

Label GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_PrefabUniqueId
private

Label that displays the unique instance ID of the selected prefab.

Shows the Unity-generated unique identifier for the prefab instance with HTML formatting.

◆ m_Preview

Texture2D GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_Preview
private

The cached preview texture generated for the current prefab.

This texture is generated asynchronously by Unity's AssetPreview system and cached to avoid repeated generation.

◆ m_PreviewElement

Image GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_PreviewElement
private

The Image element that displays the visual preview of the selected prefab.

This image is populated asynchronously using Unity's AssetPreview system and shows a rendered view of the prefab.

◆ m_Value

GameObject GWG.UsoUIElements.CustomElements.PrefabPreviewElement.m_Value
private

The currently selected GameObject prefab value.

This represents the main value of the element and is synchronized with the ObjectField selection.

◆ ussContainerClassName

readonly string GWG.UsoUIElements.CustomElements.PrefabPreviewElement.ussContainerClassName = "prefab-preview-container"
static

The CSS class name applied to the main container of the prefab preview element.

Used for styling the overall container that holds all child elements of the prefab preview.

◆ ussDetailsClassName

readonly string GWG.UsoUIElements.CustomElements.PrefabPreviewElement.ussDetailsClassName = "prefab-preview-details"
static

The CSS class name applied to the details text column containing prefab metadata.

Used for styling the container that holds the prefab name, ID, type, and tags labels.

◆ ussElementClassName

readonly string GWG.UsoUIElements.CustomElements.PrefabPreviewElement.ussElementClassName = "prefab-preview-element"
static

The CSS class name applied to the preview image element.

Used for styling the image that displays the visual preview of the selected prefab.

◆ ussStylesheetPath

readonly string GWG.UsoUIElements.CustomElements.PrefabPreviewElement.ussStylesheetPath = "prefab_preview_element"
static

The path to the USS stylesheet resource that styles this element.

This stylesheet should be placed in a Resources folder and contains the visual styling for the prefab preview element.

Property Documentation

◆ value

Object GWG.UsoUIElements.CustomElements.PrefabPreviewElement.value
getset

Gets or sets the current GameObject value of the element. Setting this property triggers change events and updates the preview display.

The currently selected GameObject prefab, or null if no prefab is selected.

The setter implements the INotifyValueChanged pattern by creating and sending a ChangeEvent when the value actually changes. It uses SetValueWithoutNotify internally to update the display and then sends the appropriate change notification to any listeners.