ObjectNameCache.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_ASSERTIONS
  3. //#define ANIMANCER_DISABLE_NAME_CACHE
  4. using System.Runtime.CompilerServices;
  5. using UnityEngine;
  6. namespace Animancer
  7. {
  8. /// <summary>[Assert-Only]
  9. /// A simple system for caching <see cref="Object.name"/> since it allocates garbage every time it's accessed.
  10. /// </summary>
  11. public static class NameCache
  12. {
  13. /************************************************************************************************************************/
  14. private static readonly ConditionalWeakTable<Object, string>
  15. ObjectToName = new();
  16. /************************************************************************************************************************/
  17. /// <summary>Caches and returns the <see cref="Object.name"/>.</summary>
  18. public static string GetCachedName(this Object obj)
  19. {
  20. #if ANIMANCER_DISABLE_NAME_CACHE
  21. return obj.name;
  22. #else
  23. if (obj == null)
  24. {
  25. if (obj is not null)
  26. ObjectToName.Remove(obj);
  27. return null;
  28. }
  29. if (!ObjectToName.TryGetValue(obj, out var name))
  30. {
  31. name = obj.name;
  32. ObjectToName.Add(obj, name);
  33. }
  34. return name;
  35. #endif
  36. }
  37. /************************************************************************************************************************/
  38. /// <summary>Tries to get the <see cref="Object.name"/> or <see cref="object.ToString"/>.</summary>
  39. public static bool TryToString(object obj, out string name)
  40. {
  41. if (obj == null)
  42. {
  43. name = null;
  44. return false;
  45. }
  46. if (obj is Object unityObject)
  47. {
  48. if (unityObject != null)
  49. {
  50. name = unityObject.GetCachedName();
  51. }
  52. else
  53. {
  54. name = null;
  55. return false;
  56. }
  57. }
  58. else
  59. {
  60. name = obj.ToString();
  61. }
  62. return !string.IsNullOrEmpty(name);
  63. }
  64. /************************************************************************************************************************/
  65. /// <summary>Clears all cached names so they will be re-gathered when next accessed.</summary>
  66. public static void Clear()
  67. => ObjectToName.Clear();
  68. /************************************************************************************************************************/
  69. /// <summary>Sets the <see cref="Object.name"/> and caches it.</summary>
  70. public static void SetName(this Object obj, string name)
  71. {
  72. obj.name = name;
  73. ObjectToName.AddOrUpdate(obj, name);
  74. }
  75. /************************************************************************************************************************/
  76. #if UNITY_EDITOR
  77. /************************************************************************************************************************/
  78. private class Cleaner : UnityEditor.AssetPostprocessor
  79. {
  80. /************************************************************************************************************************/
  81. private static void OnPostprocessAllAssets(
  82. string[] importedAssets,
  83. string[] deletedAssets,
  84. string[] movedAssets,
  85. string[] movedFromAssetPaths,
  86. bool didDomainReload)
  87. {
  88. Clear();
  89. }
  90. /************************************************************************************************************************/
  91. }
  92. /************************************************************************************************************************/
  93. #endif
  94. /************************************************************************************************************************/
  95. }
  96. }
  97. #endif