This commit is contained in:
Hubert Mattusch 2023-08-17 10:47:21 +02:00
parent 85daf89041
commit a83c4cfb3b
7 changed files with 17 additions and 52 deletions

View File

@ -1,7 +1,4 @@
#if ADDRESSABLES && UI_ELEMENTS using UnityEngine.UIElements;
using System.Collections;
using UnityEngine;
using UnityEngine.UIElements;
namespace NEG.Utils.UiToolkits namespace NEG.Utils.UiToolkits
{ {
@ -22,5 +19,4 @@ namespace NEG.Utils.UiToolkits
visualElement.Q<Button>("RemoveBtn").clicked += () => parent.TryRemoveItem(element); visualElement.Q<Button>("RemoveBtn").clicked += () => parent.TryRemoveItem(element);
} }
} }
} }
#endif

View File

@ -1,8 +1,5 @@
#if ADDRESSABLES && UI_ELEMENTS
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using System.Collections.Generic; using System.Collections.Generic;
using System; using System;
using System.IO; using System.IO;
@ -51,7 +48,6 @@ namespace NEG.Utils.UiToolkits
} }
private Label label; private Label label;
private AsyncOperationHandle<VisualTreeAsset> uxmlHandle, itemUxmlHandle;
private VisualTreeAsset itemPrefab; private VisualTreeAsset itemPrefab;
@ -91,13 +87,11 @@ namespace NEG.Utils.UiToolkits
string path = AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets($"t:Script {nameof(MultiSelectChips)}")[0]); string path = AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets($"t:Script {nameof(MultiSelectChips)}")[0]);
path = path.Remove(path.LastIndexOf('/')); path = path.Remove(path.LastIndexOf('/'));
SetVisuals(AssetDatabase.LoadAssetAtPath<VisualTreeAsset>($"{path}/MultiSelectChips.uxml")); SetVisuals(AssetDatabase.LoadAssetAtPath<VisualTreeAsset>($"{path}/Resources/MultiSelectChips.uxml"));
itemPrefab = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>($"{path}/MultiSelectChipItem.uxml"); itemPrefab = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>($"{path}/Resources/MultiSelectChipItem.uxml");
#else #else
uxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChips.uxml"); SetVisuals(Resources.Load<VisualTreeAsset>("MultiSelectChips.uxml"));
itemUxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChipItem.uxml"); itemPrefab = Resources.Load<VisualTreeAsset>("MultiSelectChipItem.uxml");
uxmlHandle.Completed += OnUxmlLoaded;
itemUxmlHandle.Completed += OnItemUxmlLoaded;
#endif #endif
} }
@ -106,7 +100,7 @@ namespace NEG.Utils.UiToolkits
if (itemPrefab == null || itemsSource == null || realItemsParent == null) if (itemPrefab == null || itemsSource == null || realItemsParent == null)
return; return;
List<MultiSelectChipItem> itemsToDestroy = new List<MultiSelectChipItem>(spawnedItems); var itemsToDestroy = new List<MultiSelectChipItem>(spawnedItems);
itemsToDestroy.RemoveAll((x) => itemsSource.Contains(x.ChipItem)); itemsToDestroy.RemoveAll((x) => itemsSource.Contains(x.ChipItem));
@ -132,10 +126,7 @@ namespace NEG.Utils.UiToolkits
} }
} }
public void TryRemoveItem(IMultiSelectChipItem item) public void TryRemoveItem(IMultiSelectChipItem item) => OnTryingToRemoveItem?.Invoke(item);
{
OnTryingToRemoveItem?.Invoke(item);
}
private void InitLabel() private void InitLabel()
{ {
@ -146,35 +137,6 @@ namespace NEG.Utils.UiToolkits
Insert(0, label); Insert(0, label);
} }
private void OnUxmlLoaded(AsyncOperationHandle<VisualTreeAsset> operation)
{
if (operation.Status == AsyncOperationStatus.Succeeded)
{
SetVisuals(operation.Result);
Addressables.Release(uxmlHandle);
UpdateItems();
}
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 SetVisuals(VisualTreeAsset treeAsset) private void SetVisuals(VisualTreeAsset treeAsset)
{ {
realItemsParent = treeAsset.Instantiate(); realItemsParent = treeAsset.Instantiate();
@ -187,5 +149,4 @@ namespace NEG.Utils.UiToolkits
realItemsParent = button.parent; realItemsParent = button.parent;
} }
} }
} }
#endif

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5a807a09d7c00be478bf44636c2cc89f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: