#if ADDRESSABLES && UI_ELEMENTS 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 uxmlHandle, itemUxmlHandle; private VisualTreeAsset itemPrefab; private IList itemsSource; public new class UxmlFactory : UxmlFactory { } 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("NegUiToolkits/MultiSelectChips.uxml"); itemUxmlHandle = Addressables.LoadAssetAsync("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 operation) { if (operation.Status == AsyncOperationStatus.Succeeded) { Add(operation.Result.Instantiate()); Addressables.Release(uxmlHandle); } else { Debug.LogError($"Asset failed to load."); } } private void OnItemUxmlLoaded(AsyncOperationHandle operation) { if (operation.Status == AsyncOperationStatus.Succeeded) { itemPrefab = operation.Result; Addressables.Release(itemUxmlHandle); UpdateItems(); } else { Debug.LogError($"Asset failed to load."); } } } #endif