#if UNITY_EDITOR using System; using UnityEditor; using UnityEngine; using UnityEngine.U2D; namespace DTT.Utils.EditorUtilities { /// /// Provides methods for validating sprites. /// public static class SpriteUtility { #region Methods #region Public /// /// Returns the for the given object. /// Will return null if the sprite is a unity buildin sprite. /// /// The sprite to get the importer of. /// The importer of the given sprite. public static TextureImporter GetImporterOfSprite(this Sprite sprite) { if (sprite == null) throw new ArgumentNullException(nameof(sprite)); string path = AssetDatabase.GetAssetPath(sprite); return path != PathNames.BUILDIN_RESOURCES ? (TextureImporter)TextureImporter.GetAtPath(path) : null; } /// /// Determines whether the given sprite is a build in Unity sprite. /// /// The sprite to be checked on whether it's a build in Unity sprite. /// Whether the given sprite is a build in Unity sprite. public static bool IsSpriteFromUnity(this Sprite sprite) { if (sprite == null) throw new ArgumentNullException(nameof(sprite)); string path = AssetDatabase.GetAssetPath(sprite); return path == PathNames.BUILDIN_RESOURCES; } /// /// Returns whether a sprite is imported with a sprite mode of . /// Will return false on internal Unity resources sprites. /// /// The sprite to check. /// Whether the sprite is imported with the multiple sprite mode. public static bool IsImportedWithMultipleSpriteMode(this Sprite sprite) { if (sprite == null) throw new ArgumentNullException(nameof(sprite)); TextureImporter importer = GetImporterOfSprite(sprite); return importer != null ? importer.spriteImportMode == SpriteImportMode.Multiple : false; } /// /// Returns whether a sprite is part of an atlas in the project. /// This is a performance heavy call and should thus only be called in initialization methods. /// /// The sprite to check. /// Whether the sprite is part of an atlas in the project. public static bool IsAtlasPacked(this Sprite sprite) { if (sprite == null) throw new ArgumentNullException(nameof(sprite)); string[] guids = AssetDatabase.FindAssets("t:spriteatlas"); foreach (string guid in guids) { SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); if (atlas.CanBindTo(sprite)) return true; } return false; } #endregion #endregion } } #endif