using System.Linq; using UnityEditor.AssetImporters; using UnityEditor.U2D.Sprites; namespace UnityEditor.U2D.Aseprite { public partial class AsepriteImporter : ScriptedImporter, ISpriteEditorDataProvider { SpriteImportMode ISpriteEditorDataProvider.spriteImportMode => spriteImportModeToUse; UnityEngine.Object ISpriteEditorDataProvider.targetObject => targetObject; internal UnityEngine.Object targetObject => this; /// /// Implementation for ISpriteEditorDataProvider.pixelsPerUnit. /// float ISpriteEditorDataProvider.pixelsPerUnit => pixelsPerUnit; internal float pixelsPerUnit => m_TextureImporterSettings.spritePixelsPerUnit; /// /// Implementation for ISpriteEditorDataProvider.GetDataProvider. /// /// Data provider type to retrieve. /// T ISpriteEditorDataProvider.GetDataProvider() { return GetDataProvider(); } internal T GetDataProvider() where T : class { if (typeof(T) == typeof(ISpriteBoneDataProvider)) { return new SpriteBoneDataProvider { dataProvider = this } as T; } if (typeof(T) == typeof(ISpriteMeshDataProvider)) { return new SpriteMeshDataProvider { dataProvider = this } as T; } if (typeof(T) == typeof(ISpriteOutlineDataProvider)) { return new SpriteOutlineDataProvider { dataProvider = this } as T; } if (typeof(T) == typeof(ISpritePhysicsOutlineDataProvider)) { return new SpritePhysicsOutlineProvider { dataProvider = this } as T; } if (typeof(T) == typeof(ITextureDataProvider)) { return new TextureDataProvider { dataProvider = this } as T; } if (typeof(T) == typeof(ISecondaryTextureDataProvider)) { return new SecondaryTextureDataProvider() { dataProvider = this } as T; } else return this as T; } /// /// Implementation for ISpriteEditorDataProvider.HasDataProvider. /// /// Data provider type to query. /// True if data provider is supported, false otherwise. bool ISpriteEditorDataProvider.HasDataProvider(System.Type type) { return HasDataProvider(type); } internal bool HasDataProvider(System.Type type) { if (type == typeof(ISpriteBoneDataProvider) || type == typeof(ISpriteMeshDataProvider) || type == typeof(ISpriteOutlineDataProvider) || type == typeof(ISpritePhysicsOutlineDataProvider) || type == typeof(ITextureDataProvider) || type == typeof(ISecondaryTextureDataProvider)) { return true; } else return type.IsAssignableFrom(GetType()); } /// /// Implementation for ISpriteEditorDataProvider.Apply. /// void ISpriteEditorDataProvider.Apply() { Apply(); } /// /// Implementation for ISpriteEditorDataProvider.InitSpriteEditorDataProvider. /// void ISpriteEditorDataProvider.InitSpriteEditorDataProvider() { InitSpriteEditorDataProvider(); } void InitSpriteEditorDataProvider() {} /// /// Implementation for ISpriteEditorDataProvider.GetSpriteRects. /// /// An array of SpriteRect for the current import mode. SpriteRect[] ISpriteEditorDataProvider.GetSpriteRects() { return GetSpriteRects(); } internal SpriteRect[] GetSpriteRects() { if (spriteImportModeToUse == SpriteImportMode.Multiple) { switch (m_AsepriteImporterSettings.fileImportMode) { case FileImportModes.SpriteSheet: return m_SpriteSheetImportData.Select(x => new SpriteMetaData(x) as SpriteRect).ToArray(); case FileImportModes.AnimatedSprite: default: return m_AnimatedSpriteImportData.Select(x => new SpriteMetaData(x) as SpriteRect).ToArray(); } } return new[] { new SpriteMetaData(m_SingleSpriteImportData[0]) }; } /// /// Implementation for ISpriteEditorDataProvider.SetSpriteRects. /// /// Set the SpriteRect data for the current import mode. void ISpriteEditorDataProvider.SetSpriteRects(SpriteRect[] spriteRects) { SetSpriteRects(spriteRects); } internal void SetSpriteRects(SpriteRect[] spriteRects) { var spriteImportData = GetSpriteImportData(); if (spriteImportModeToUse == SpriteImportMode.Multiple) { spriteImportData.RemoveAll(data => spriteRects.FirstOrDefault(x => x.spriteID == data.spriteID) == null); foreach (var sr in spriteRects) { var importData = spriteImportData.FirstOrDefault(x => x.spriteID == sr.spriteID); if (importData == null) spriteImportData.Add(new SpriteMetaData(sr)); else { importData.name = sr.name; importData.alignment = sr.alignment; importData.border = sr.border; importData.pivot = sr.pivot; importData.rect = sr.rect; } } } else if (spriteRects.Length == 1 && (spriteImportModeToUse == SpriteImportMode.Single || spriteImportModeToUse == SpriteImportMode.Polygon)) { if (spriteImportData[0].spriteID == spriteRects[0].spriteID) { spriteImportData[0].name = spriteRects[0].name; spriteImportData[0].alignment = spriteRects[0].alignment; m_TextureImporterSettings.spriteAlignment = (int)spriteRects[0].alignment; m_TextureImporterSettings.spriteBorder = spriteImportData[0].border = spriteRects[0].border; m_TextureImporterSettings.spritePivot = spriteImportData[0].pivot = spriteRects[0].pivot; spriteImportData[0].rect = spriteRects[0].rect; } else { spriteImportData[0] = new SpriteMetaData(spriteRects[0]); } } } } }