GraphicRebuildTracker.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #if UNITY_EDITOR
  2. using System.Collections.Generic;
  3. using UnityEngine.UI.Collections;
  4. namespace UnityEngine.UI
  5. {
  6. /// <summary>
  7. /// EditorOnly class for tracking all Graphics.
  8. /// Used when a source asset is reimported into the editor to ensure that Graphics are updated as intended.
  9. /// </summary>
  10. public static class GraphicRebuildTracker
  11. {
  12. static IndexedSet<Graphic> m_Tracked = new IndexedSet<Graphic>();
  13. static bool s_Initialized;
  14. /// <summary>
  15. /// Add a Graphic to the list of tracked Graphics
  16. /// </summary>
  17. /// <param name="g">The graphic to track</param>
  18. public static void TrackGraphic(Graphic g)
  19. {
  20. if (!s_Initialized)
  21. {
  22. CanvasRenderer.onRequestRebuild += OnRebuildRequested;
  23. s_Initialized = true;
  24. }
  25. m_Tracked.AddUnique(g);
  26. }
  27. /// <summary>
  28. /// Remove a Graphic to the list of tracked Graphics
  29. /// </summary>
  30. /// <param name="g">The graphic to remove from tracking.</param>
  31. public static void UnTrackGraphic(Graphic g)
  32. {
  33. m_Tracked.Remove(g);
  34. }
  35. static void OnRebuildRequested()
  36. {
  37. StencilMaterial.ClearAll();
  38. for (int i = 0; i < m_Tracked.Count; i++)
  39. {
  40. m_Tracked[i].OnRebuildRequested();
  41. }
  42. }
  43. }
  44. }
  45. #endif // if UNITY_EDITOR