protected override void Prepare(AssetCompilerContext context, AssetItem assetItem, string targetUrlInStorage, AssetCompilerResult result) { var asset = (SpriteSheetAsset)assetItem.Asset; var gameSettingsAsset = context.GetGameSettingsAsset(); var renderingSettings = gameSettingsAsset.GetOrCreate <RenderingSettings>(context.Platform); result.BuildSteps = new ListBuildStep(); var prereqSteps = new Queue <BuildStep>(); // create the registry containing the sprite assets texture index association var imageToTextureUrl = new Dictionary <SpriteInfo, string>(); var colorSpace = context.GetColorSpace(); // create and add import texture commands if (asset.Sprites != null && !asset.Packing.Enabled) { // sort sprites by referenced texture. var spriteByTextures = asset.Sprites.GroupBy(x => x.Source).ToArray(); for (int i = 0; i < spriteByTextures.Length; i++) { // skip the texture if the file is not valid. var textureFile = spriteByTextures[i].Key; if (!TextureFileIsValid(textureFile)) { continue; } var textureUrl = SpriteSheetAsset.BuildTextureUrl(assetItem.Location, i); var spriteAssetArray = spriteByTextures[i].ToArray(); foreach (var spriteAsset in spriteAssetArray) { imageToTextureUrl[spriteAsset] = textureUrl; } // create an texture asset. var textureAsset = new TextureAsset { Id = AssetId.Empty, // CAUTION: It is important to use an empty GUID here, as we don't want the command to be rebuilt (by default, a new asset is creating a new guid) IsStreamable = asset.IsStreamable && asset.Type != SpriteSheetType.UI, IsCompressed = asset.IsCompressed, GenerateMipmaps = asset.GenerateMipmaps, Type = new ColorTextureType { Alpha = asset.Alpha, PremultiplyAlpha = asset.PremultiplyAlpha, ColorKeyColor = asset.ColorKeyColor, ColorKeyEnabled = asset.ColorKeyEnabled, UseSRgbSampling = true, } }; // Get absolute path of asset source on disk var assetDirectory = assetItem.FullPath.GetParent(); var assetSource = UPath.Combine(assetDirectory, spriteAssetArray[0].Source); // add the texture build command. var textureConvertParameters = new TextureConvertParameters(assetSource, textureAsset, context.Platform, context.GetGraphicsPlatform(assetItem.Package), renderingSettings.DefaultGraphicsProfile, gameSettingsAsset.GetOrCreate <TextureSettings>().TextureQuality, colorSpace); var textureConvertCommand = new TextureAssetCompiler.TextureConvertCommand(textureUrl, textureConvertParameters, assetItem.Package); var assetBuildStep = new AssetBuildStep(new AssetItem(textureUrl, textureAsset)); assetBuildStep.Add(textureConvertCommand); prereqSteps.Enqueue(assetBuildStep); result.BuildSteps.Add(assetBuildStep); } } if (!result.HasErrors) { var parameters = new SpriteSheetParameters(asset, imageToTextureUrl, context.Platform, context.GetGraphicsPlatform(assetItem.Package), renderingSettings.DefaultGraphicsProfile, gameSettingsAsset.GetOrCreate <TextureSettings>().TextureQuality, colorSpace); var assetBuildStep = new AssetBuildStep(assetItem); assetBuildStep.Add(new SpriteSheetCommand(targetUrlInStorage, parameters, assetItem.Package)); result.BuildSteps.Add(assetBuildStep); while (prereqSteps.Count > 0) { var prereq = prereqSteps.Dequeue(); BuildStep.LinkBuildSteps(prereq, assetBuildStep); } } }
protected override void Compile(AssetCompilerContext context, string urlInStorage, UFile assetAbsolutePath, SpriteSheetAsset asset, AssetCompilerResult result) { var gameSettingsAsset = context.GetGameSettingsAsset(); result.BuildSteps = new AssetBuildStep(AssetItem); // create the registry containing the sprite assets texture index association var imageToTextureUrl = new Dictionary <SpriteInfo, string>(); var colorSpace = context.GetColorSpace(); // create and add import texture commands if (asset.Sprites != null && !asset.Packing.Enabled) { // sort sprites by referenced texture. var spriteByTextures = asset.Sprites.GroupBy(x => x.Source).ToArray(); for (int i = 0; i < spriteByTextures.Length; i++) { // skip the texture if the file is not valid. var textureFile = spriteByTextures[i].Key; if (!TextureFileIsValid(textureFile)) { continue; } var spriteAssetArray = spriteByTextures[i].ToArray(); foreach (var spriteAsset in spriteAssetArray) { imageToTextureUrl[spriteAsset] = SpriteSheetAsset.BuildTextureUrl(urlInStorage, i); } // create an texture asset. var textureAsset = new TextureAsset { Id = Guid.Empty, // CAUTION: It is important to use an empty GUID here, as we don't want the command to be rebuilt (by default, a new asset is creating a new guid) Alpha = asset.Alpha, Format = asset.Format, GenerateMipmaps = asset.GenerateMipmaps, PremultiplyAlpha = asset.PremultiplyAlpha, ColorKeyColor = asset.ColorKeyColor, ColorKeyEnabled = asset.ColorKeyEnabled, ColorSpace = asset.ColorSpace, Hint = TextureHint.Color }; // Get absolute path of asset source on disk var assetDirectory = assetAbsolutePath.GetParent(); var assetSource = UPath.Combine(assetDirectory, spriteAssetArray[0].Source); // add the texture build command. result.BuildSteps.Add( new TextureAssetCompiler.TextureConvertCommand( SpriteSheetAsset.BuildTextureUrl(urlInStorage, i), new TextureConvertParameters(assetSource, textureAsset, context.Platform, context.GetGraphicsPlatform(), gameSettingsAsset.DefaultGraphicsProfile, gameSettingsAsset.TextureQuality, colorSpace))); } result.BuildSteps.Add(new WaitBuildStep()); // wait the textures to be imported } if (!result.HasErrors) { var parameters = new SpriteSheetParameters(asset, imageToTextureUrl, context.Platform, context.GetGraphicsPlatform(), gameSettingsAsset.DefaultGraphicsProfile, gameSettingsAsset.TextureQuality, colorSpace); result.BuildSteps.Add(new SpriteSheetCommand(urlInStorage, parameters)); } }