public override void OnImportAsset(AssetImportContext ctx) { name = GetFileName(ctx.assetPath); AseFile aseFile = ReadAseFile(ctx.assetPath); int frameCount = aseFile.Header.Frames; SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height); Texture2D[] frames = null; if (importType != AseFileImportType.LayerToSprite) { frames = aseFile.GetFrames(); } else { frames = aseFile.GetLayersAsFrames(); } SpriteImportData[] spriteImportData = new SpriteImportData[0]; //if (textureSettings.transparentMask) //{ // atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentColor, false); //} //else //{ // atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, false); //} atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentMask, false); atlas.filterMode = textureSettings.filterMode; atlas.alphaIsTransparency = false; atlas.wrapMode = TextureWrapMode.Clamp; atlas.name = "Texture"; ctx.AddObjectToAsset("Texture", atlas); ctx.SetMainObject(atlas); switch (importType) { case AseFileImportType.LayerToSprite: case AseFileImportType.Sprite: ImportSprites(ctx, aseFile, spriteImportData); break; case AseFileImportType.Tileset: ImportTileset(ctx, atlas); break; } ctx.SetMainObject(atlas); }
public override void OnImportAsset(AssetImportContext ctx) { name = GetFileName(ctx.assetPath); AseFile aseFile = ReadAseFile(ctx.assetPath); //Debug.Log(string.Format("导入Ase动画文件:{0},帧间隔为{1}(相当于动画帧率{2})", name, aseFile.Header.Speed, Mathf.RoundToInt(1000 / aseFile.Header.Speed))); this.aseFile = aseFile; SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height); Texture2D[] frames; if (importType != AseFileImportType.LayerToSprite) { frames = aseFile.GetFrames(); } else { frames = aseFile.GetLayersAsFrames(); } SpriteImportData[] spriteImportData = new SpriteImportData[0]; //if (textureSettings.transparentMask) //{ // atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentColor, false); //} //else //{ // atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, false); //} //翻转贴图 if (textureSettings.flipTexture) { for (int i = 0; i < frames.Length; i++) { frames[i] = FlipTexture(frames[i]); } } atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentMask, false); atlas.filterMode = textureSettings.filterMode; atlas.alphaIsTransparency = false; atlas.wrapMode = TextureWrapMode.Clamp; atlas.name = "Texture"; ctx.AddObjectToAsset("Texture", atlas); ctx.SetMainObject(atlas); switch (importType) { case AseFileImportType.LayerToSprite: case AseFileImportType.Sprite: ImportSprites(ctx, aseFile, spriteImportData); break; case AseFileImportType.Tileset: ImportTileset(ctx, atlas); break; } ctx.SetMainObject(atlas); }
public override void OnImportAsset(AssetImportContext ctx) { name = GetFileName(ctx.assetPath); AseFile aseFile = ReadAseFile(ctx.assetPath); int frameCount = aseFile.Header.Frames; SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height); Texture2D[] frames = null; if (importType != AseFileImportType.LayerToSprite) { frames = aseFile.GetFrames(textureSettings.ignoreBackground); } else { frames = aseFile.GetLayersAsFrames(); } SpriteImportData[] spriteImportData; //if (textureSettings.transparentMask) //{ // atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentColor, false); //} //else //{ // atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, false); //} atlas = atlasBuilder.GenerateAtlas(frames, name, out spriteImportData, textureSettings.transparencyMode, false); atlas.filterMode = textureSettings.filterMode; atlas.alphaIsTransparency = textureSettings.transparencyMode == TransparencyMode.Alpha; atlas.wrapMode = textureSettings.wrapMode; atlas.name = "Texture"; MetaData[] metadatas = aseFile.GetMetaData(textureSettings.spritePivot, textureSettings.pixelsPerUnit); List <SecondarySpriteTexture> secondarySpriteTextures = new List <SecondarySpriteTexture>(); // Find any metadata with a secondary texture foreach (var metadata in metadatas) { if (metadata.Type == MetaDataType.SECONDARY_TEXTURE) { var textureName = metadata.Args[0]; var secondaryTextureFrames = aseFile.GetLayerTexture(metadata.LayerIndex, metadata.Layer); Texture2D secondaryTexture = atlasBuilder.GenerateAtlas(secondaryTextureFrames.ToArray(), name, out _, textureSettings.transparencyMode, false); secondaryTexture.alphaIsTransparency = textureSettings.transparencyMode == TransparencyMode.Alpha;; secondaryTexture.wrapMode = textureSettings.wrapMode; secondaryTexture.filterMode = textureSettings.filterMode; secondaryTexture.name = textureName; secondarySpriteTextures.Add(new SecondarySpriteTexture() { name = textureName, texture = secondaryTexture }); } } TextureGenerationSettings generationSettings = new TextureGenerationSettings(); generationSettings.platformSettings = new TextureImporterPlatformSettings(); generationSettings.spriteImportData = spriteImportData; generationSettings.secondarySpriteTextures = secondarySpriteTextures.ToArray(); generationSettings.sourceTextureInformation = new SourceTextureInformation() { width = atlas.width, height = atlas.height, containsAlpha = true, }; generationSettings.textureImporterSettings = new TextureImporterSettings { textureType = TextureImporterType.Sprite, textureShape = TextureImporterShape.Texture2D, alphaIsTransparency = textureSettings.transparencyMode == TransparencyMode.Alpha, alphaSource = TextureImporterAlphaSource.FromInput, spritePixelsPerUnit = textureSettings.pixelsPerUnit, spritePivot = textureSettings.spritePivot, spriteMeshType = SpriteMeshType.Tight, convertToNormalMap = false, mipmapEnabled = false, sRGBTexture = true, readable = false, fadeOut = false, wrapMode = TextureWrapMode.Clamp, wrapModeU = TextureWrapMode.Clamp, wrapModeV = TextureWrapMode.Clamp, wrapModeW = TextureWrapMode.Clamp }; var imageBuffer = atlas.GetRawTextureData <Color32>(); var generatedOutput = TextureGenerator.GenerateTexture(generationSettings, imageBuffer); generatedOutput.texture.name = "Texture"; // Main texture atlas ctx.AddObjectToAsset("Texture", generatedOutput.texture); ctx.SetMainObject(generatedOutput.texture); // Add secondary textures foreach (var secondarySpriteTexture in secondarySpriteTextures) { ctx.AddObjectToAsset("SecondaryTex_" + secondarySpriteTexture.name, secondarySpriteTexture.texture); } // Add sprites foreach (var sprite in generatedOutput.sprites) { ctx.AddObjectToAsset(sprite.name, sprite); } // Add animations if (importType == AseFileImportType.Sprite) { GenerateAnimations(ctx, aseFile, generatedOutput.sprites, metadatas); } switch (importType) { case AseFileImportType.LayerToSprite: case AseFileImportType.Sprite: // ImportSprites(ctx, aseFile, spriteImportData, metadatas); break; case AseFileImportType.Tileset: ImportTileset(ctx, atlas); break; } }