127 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			127 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| #if ADDRESSABLES && UI_ELEMENTS
 | |
| using UnityEngine;
 | |
| using UnityEngine.UIElements;
 | |
| using System;
 | |
| using System.Collections;
 | |
| using System.ComponentModel;
 | |
| using UnityEngine.AddressableAssets;
 | |
| using UnityEngine.ResourceManagement.AsyncOperations;
 | |
| 
 | |
| 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.");
 | |
|         }
 | |
|     }
 | |
| }
 | |
| #endif |