PooledGUIContent.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using UnityEngine;
  5. namespace Animancer.Editor
  6. {
  7. /// <summary>[Editor-Only]
  8. /// <see cref="GUIContent"/> with <see cref="IDisposable"/> connected to an <see cref="ObjectPool{T}"/>.
  9. /// </summary>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/PooledGUIContent
  11. public class PooledGUIContent : GUIContent,
  12. IDisposable
  13. {
  14. /************************************************************************************************************************/
  15. /// <summary><see cref="ObjectPool{T}.Acquire()"/>s and initializes an instance.</summary>
  16. public static PooledGUIContent Acquire(
  17. string text = null,
  18. string tooltip = null,
  19. Texture image = null)
  20. {
  21. var item = Pool.Instance.Acquire();
  22. item.text = text;
  23. item.tooltip = tooltip;
  24. item.image = image;
  25. return item;
  26. }
  27. /// <summary><see cref="ObjectPool{T}.Acquire()"/>s and initializes an instance.</summary>
  28. public static PooledGUIContent Acquire(
  29. UnityEditor.SerializedProperty property)
  30. => Acquire(property.displayName, property.tooltip);
  31. /************************************************************************************************************************/
  32. /// <summary><see cref="ObjectPool{T}.Release(T)"/>.</summary>
  33. public void Dispose()
  34. => Pool.Instance.Release(this);
  35. /************************************************************************************************************************/
  36. /// <summary>An <see cref="ObjectPool{T}"/> for <see cref="PooledGUIContent"/>.</summary>
  37. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/Pool
  38. public class Pool : ObjectPool<PooledGUIContent>
  39. {
  40. /************************************************************************************************************************/
  41. /// <summary>Singleton instance.</summary>
  42. public static Pool Instance = new();
  43. /************************************************************************************************************************/
  44. /// <inheritdoc/>
  45. protected override PooledGUIContent New()
  46. => new();
  47. /************************************************************************************************************************/
  48. /// <inheritdoc/>
  49. public override PooledGUIContent Acquire()
  50. {
  51. var content = base.Acquire();
  52. #if UNITY_ASSERTIONS
  53. if (!string.IsNullOrEmpty(content.text) ||
  54. !string.IsNullOrEmpty(content.tooltip) ||
  55. content.image != null)
  56. {
  57. throw new UnityEngine.Assertions.AssertionException(
  58. $"• {nameof(content.text)} = '{content.text}'" +
  59. $"\n• {nameof(content.tooltip)} = '{content.tooltip}'" +
  60. $"\n• {nameof(content.image)} = '{content.image}'",
  61. $"A {nameof(PooledGUIContent)} is not cleared." + NotResetError);
  62. }
  63. #endif
  64. return content;
  65. }
  66. /************************************************************************************************************************/
  67. /// <inheritdoc/>
  68. public override void Release(PooledGUIContent content)
  69. {
  70. content.text = null;
  71. content.tooltip = null;
  72. content.image = null;
  73. base.Release(content);
  74. }
  75. /************************************************************************************************************************/
  76. }
  77. /************************************************************************************************************************/
  78. }
  79. }
  80. #endif