// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // #if UNITY_ASSERTIONS //#define ANIMANCER_DISABLE_NAME_CACHE using System.Runtime.CompilerServices; using UnityEngine; namespace Animancer { /// [Assert-Only] /// A simple system for caching since it allocates garbage every time it's accessed. /// public static class NameCache { /************************************************************************************************************************/ private static readonly ConditionalWeakTable ObjectToName = new(); /************************************************************************************************************************/ /// Caches and returns the . public static string GetCachedName(this Object obj) { #if ANIMANCER_DISABLE_NAME_CACHE return obj.name; #else if (obj == null) { if (obj is not null) ObjectToName.Remove(obj); return null; } if (!ObjectToName.TryGetValue(obj, out var name)) { name = obj.name; ObjectToName.Add(obj, name); } return name; #endif } /************************************************************************************************************************/ /// Tries to get the or . public static bool TryToString(object obj, out string name) { if (obj == null) { name = null; return false; } if (obj is Object unityObject) { if (unityObject != null) { name = unityObject.GetCachedName(); } else { name = null; return false; } } else { name = obj.ToString(); } return !string.IsNullOrEmpty(name); } /************************************************************************************************************************/ /// Clears all cached names so they will be re-gathered when next accessed. public static void Clear() => ObjectToName.Clear(); /************************************************************************************************************************/ /// Sets the and caches it. public static void SetName(this Object obj, string name) { obj.name = name; ObjectToName.AddOrUpdate(obj, name); } /************************************************************************************************************************/ #if UNITY_EDITOR /************************************************************************************************************************/ private class Cleaner : UnityEditor.AssetPostprocessor { /************************************************************************************************************************/ private static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload) { Clear(); } /************************************************************************************************************************/ } /************************************************************************************************************************/ #endif /************************************************************************************************************************/ } } #endif