No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PartSwapUI.cs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.U2D.Animation;
  5. using UnityEngine.UIElements;
  6. namespace Unity.U2D.Animation.Sample
  7. {
  8. [Serializable]
  9. internal struct SwapOptionData
  10. {
  11. public SpriteResolver spriteResolver;
  12. public string category;
  13. }
  14. internal class PartSwapUI : MonoBehaviour
  15. {
  16. [SerializeField]
  17. SpriteLibrary m_SpriteLibrary;
  18. [SerializeField]
  19. SwapOptionData[] m_SwapOptionData;
  20. void OnEnable()
  21. {
  22. var uiDocument = GetComponent<UIDocument>();
  23. var description = uiDocument.rootVisualElement.Q<Label>("Description");
  24. description.text = "Different character parts can be swapped by changing the SpriteResolver's Label property on that part.";
  25. foreach (var swapOption in m_SwapOptionData)
  26. {
  27. var libraryAsset = m_SpriteLibrary.spriteLibraryAsset;
  28. var labels = libraryAsset.GetCategoryLabelNames(swapOption.category);
  29. var dropdown = uiDocument.rootVisualElement.Q<VisualElement>(swapOption.category).Q<DropdownField>();
  30. dropdown.choices = new List<string>(labels);
  31. dropdown.value = swapOption.spriteResolver.GetLabel();
  32. dropdown.RegisterValueChangedCallback(evt =>
  33. {
  34. swapOption.spriteResolver.SetCategoryAndLabel(swapOption.category, evt.newValue);
  35. });
  36. }
  37. }
  38. }
  39. }