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.

RuntimeSwapUI.cs 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.U2D.Animation;
  4. using UnityEngine.UIElements;
  5. namespace Unity.U2D.Animation.Sample
  6. {
  7. internal class RuntimeSwapUI : MonoBehaviour
  8. {
  9. [Serializable]
  10. class SwapEntry
  11. {
  12. public Sprite sprite = null;
  13. public string category = "";
  14. public string entry = "";
  15. }
  16. [Serializable]
  17. class SwapGroup
  18. {
  19. public string name;
  20. public Sprite defaultSprite;
  21. public Sprite overrideSprite;
  22. public SwapEntry[] swapEntries = null;
  23. }
  24. [SerializeField]
  25. SwapGroup[] m_SwapGroup = null;
  26. [SerializeField]
  27. SpriteLibrary m_SpriteLibraryTarget = null;
  28. void OnEnable()
  29. {
  30. var uiDocument = GetComponent<UIDocument>();
  31. var description = uiDocument.rootVisualElement.Q<Label>("Description");
  32. description.text = "The example uses the Sprite Library's inline override functionality to show how visual can be swap in Runtime without setting up additional Sprite Library Asset.";
  33. foreach (var swapGroup in m_SwapGroup)
  34. {
  35. var group = swapGroup;
  36. var groupName = group.name;
  37. var visualElement = uiDocument.rootVisualElement.Q<VisualElement>(groupName);
  38. var add = visualElement.Q<Image>("AddOverride");
  39. add.sprite = swapGroup.overrideSprite;
  40. add.RegisterCallback<PointerDownEvent>(_ => OverrideEntry(group));
  41. var remove = visualElement.Q<Image>("RemoveOverride");
  42. remove.sprite = swapGroup.defaultSprite;
  43. remove.RegisterCallback<PointerDownEvent>(_ => ResetEntry(group));
  44. }
  45. }
  46. void OverrideEntry(SwapGroup swapGroup)
  47. {
  48. foreach (var entry in swapGroup.swapEntries)
  49. m_SpriteLibraryTarget.AddOverride(entry.sprite, entry.category, entry.entry);
  50. }
  51. void ResetEntry(SwapGroup swapGroup)
  52. {
  53. foreach (var entry in swapGroup.swapEntries)
  54. m_SpriteLibraryTarget.RemoveOverride(entry.category, entry.entry);
  55. }
  56. }
  57. }