using System; using UnityEngine; using UnityEngine.U2D; #if UNITY_EDITOR using System.Reflection; #endif namespace Coffee.UIParticleInternal { /// /// Extension methods for Sprite class. /// internal static class SpriteExtensions { #if UNITY_EDITOR private static readonly Type s_SpriteEditorExtensionType = Type.GetType("UnityEditor.Experimental.U2D.SpriteEditorExtension, UnityEditor") ?? Type.GetType("UnityEditor.U2D.SpriteEditorExtension, UnityEditor"); private static readonly Func s_GetActiveAtlasTextureMethod = (Func)Delegate.CreateDelegate(typeof(Func), s_SpriteEditorExtensionType .GetMethod("GetActiveAtlasTexture", BindingFlags.Static | BindingFlags.NonPublic)); private static readonly Func s_GetActiveAtlasMethod = (Func)Delegate.CreateDelegate(typeof(Func), s_SpriteEditorExtensionType .GetMethod("GetActiveAtlas", BindingFlags.Static | BindingFlags.NonPublic)); /// /// Get the actual texture of a sprite in play mode or edit mode. /// public static Texture2D GetActualTexture(this Sprite self) { if (!self) return null; var ret = s_GetActiveAtlasTextureMethod(self); return ret ? ret : self.texture; } /// /// Get the active sprite atlas of a sprite in play mode or edit mode. /// public static SpriteAtlas GetActiveAtlas(this Sprite self) { if (!self) return null; return s_GetActiveAtlasMethod(self); } #else /// /// Get the actual texture of a sprite in play mode. /// internal static Texture2D GetActualTexture(this Sprite self) { return self ? self.texture : null; } #endif } }