using System; using System.Linq; using UnityEditor.AssetImporters; using UnityEditor.U2D.Animation; using UnityEditor.U2D.Sprites; namespace UnityEditor.U2D.PSD { public partial class PSDImporter : 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(ICharacterDataProvider)) { return inCharacterMode ? new CharacterDataProvider { dataProvider = this } as T : null; } if (typeof(T) == typeof(IMainSkeletonDataProvider)) { return inCharacterMode && skeletonAsset != null ? new MainSkeletonDataProvider() { dataProvider = this } as T : null; } 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(Type type) { return HasDataProvider(type); } internal bool HasDataProvider(Type type) { if (inCharacterMode) { if (type == typeof(ICharacterDataProvider)) return true; if (type == typeof(IMainSkeletonDataProvider) && skeletonAsset != null) return true; } 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() { var spriteImportData = GetSpriteImportData(); var skip = inMosaicMode ? 0 : 1; return spriteImportModeToUse == SpriteImportMode.Multiple ? spriteImportData.Skip(skip).Select(x => new SpriteMetaData(x) as SpriteRect).ToArray() : new[] {new SpriteMetaData(spriteImportData[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) { var singleSpriteID = inMosaicMode ? new GUID() : spriteImportData[0].spriteID; spriteImportData.RemoveAll(data => data.spriteID != singleSpriteID && 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]); } } } } }