128 lines
3.0 KiB
C#
128 lines
3.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using System;
|
|
using System.Collections;
|
|
using System.ComponentModel;
|
|
|
|
public class MultiSelectChips : VisualElement
|
|
{
|
|
public string LabelText
|
|
{
|
|
get => label.text;
|
|
set
|
|
{
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
if (label == null)
|
|
{
|
|
InitLabel();
|
|
}
|
|
|
|
label.text = value;
|
|
}
|
|
else if (label != null)
|
|
{
|
|
label.RemoveFromHierarchy();
|
|
label = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public IList ItemsSource
|
|
{
|
|
get => itemsSource;
|
|
|
|
set
|
|
{
|
|
itemsSource = value;
|
|
UpdateItems();
|
|
}
|
|
}
|
|
|
|
private Label label;
|
|
private AsyncOperationHandle<VisualTreeAsset> uxmlHandle, itemUxmlHandle;
|
|
|
|
private VisualTreeAsset itemPrefab;
|
|
|
|
private IList itemsSource;
|
|
|
|
public new class UxmlFactory : UxmlFactory<MultiSelectChips, UxmlTraits>
|
|
{
|
|
|
|
}
|
|
|
|
public new class UxmlTraits : VisualElement.UxmlTraits
|
|
{
|
|
private readonly UxmlStringAttributeDescription label;
|
|
|
|
public UxmlTraits()
|
|
{
|
|
label = new()
|
|
{
|
|
name = "label"
|
|
};
|
|
}
|
|
|
|
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
|
{
|
|
base.Init(ve, bag, cc);
|
|
((MultiSelectChips)ve).LabelText = label.GetValueFromBag(bag, cc);
|
|
}
|
|
}
|
|
|
|
public MultiSelectChips() : base()
|
|
{
|
|
uxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChips.uxml");
|
|
itemUxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChipsItem.uxml");
|
|
uxmlHandle.Completed += OnUxmlLoaded;
|
|
itemUxmlHandle.Completed += OnItemUxmlLoaded;
|
|
}
|
|
|
|
public void UpdateItems()
|
|
{
|
|
if (itemPrefab == null)
|
|
return;
|
|
}
|
|
|
|
private void InitLabel()
|
|
{
|
|
label = new Label()
|
|
{
|
|
pickingMode = PickingMode.Ignore
|
|
};
|
|
Add(label);
|
|
}
|
|
|
|
private void OnUxmlLoaded(AsyncOperationHandle<VisualTreeAsset> operation)
|
|
{
|
|
if (operation.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
Add(operation.Result.Instantiate());
|
|
Addressables.Release(uxmlHandle);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Asset failed to load.");
|
|
}
|
|
}
|
|
|
|
private void OnItemUxmlLoaded(AsyncOperationHandle<VisualTreeAsset> operation)
|
|
{
|
|
|
|
if (operation.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
itemPrefab = operation.Result;
|
|
Addressables.Release(itemUxmlHandle);
|
|
UpdateItems();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Asset failed to load.");
|
|
}
|
|
}
|
|
|
|
private void
|
|
|
|
} |