public void GenerateSpriteTests(GenerateSpriteTestCase testCase) { var nativeBuffer = new NativeArray <Color32>(testCase.buffer, Allocator.Temp); var output = TextureGeneratorHelper.GenerateTextureSprite(nativeBuffer, testCase.bufferWidth, testCase.bufferHeight, testCase.settings, testCase.platformSettings, testCase.spriteSettings, testCase.alphaSettings, testCase.mipmapSettings, testCase.wrapSettings); nativeBuffer.Dispose(); //Verify texture is generated Assert.NotNull(output.texture); Assert.AreEqual(testCase.alphaSettings == null || testCase.alphaSettings.alphaSource != TextureImporterAlphaSource.None, output.texture.alphaIsTransparency); int expectedWidth = testCase.platformSettings.overridden ? Mathf.Min(testCase.bufferWidth, testCase.platformSettings.maxTextureSize) : testCase.bufferWidth; int expectedHeight = testCase.platformSettings.overridden ? Mathf.Min(testCase.bufferHeight, testCase.platformSettings.maxTextureSize) : testCase.bufferHeight; Assert.AreEqual(expectedWidth, output.texture.width); Assert.AreEqual(expectedHeight, output.texture.height); //Verify sprites var spriteSheetData = testCase.spriteSettings.spriteSheetData; if (spriteSheetData == null) { Assert.AreEqual(0, output.sprites.Length); } else { Assert.AreEqual(spriteSheetData.Length, output.sprites.Length); for (int i = 0; i < spriteSheetData.Length; ++i) { var spriteID = new GUID(spriteSheetData[i].spriteID); var sprite = output.sprites.FirstOrDefault(x => x.GetSpriteID() == spriteID); Assert.NotNull(sprite); Assert.AreEqual(spriteSheetData[i].name, sprite.name); if (!testCase.platformSettings.overridden) { Assert.AreEqual(spriteSheetData[i].rect, sprite.textureRect); } Assert.AreEqual(output.texture, sprite.texture); } } }
TextureGenerationOutput ImportTexture(AssetImportContext ctx, NativeArray <Color32> imageData, int textureWidth, int textureHeight, int spriteStart, int spriteCount) { UnityEngine.Profiling.Profiler.BeginSample("ImportTexture"); var platformSettings = GetPlatformTextureSettings(ctx.selectedBuildTarget); var textureSettings = m_TextureImporterSettings.ExtractTextureSettings(); textureSettings.assetPath = ctx.assetPath; textureSettings.enablePostProcessor = false; textureSettings.containsAlpha = true; textureSettings.hdr = false; var textureAlphaSettings = m_TextureImporterSettings.ExtractTextureAlphaSettings(); var textureMipmapSettings = m_TextureImporterSettings.ExtractTextureMipmapSettings(); var textureCubemapSettings = m_TextureImporterSettings.ExtractTextureCubemapSettings(); var textureWrapSettings = m_TextureImporterSettings.ExtractTextureWrapSettings(); TextureGenerationOutput output; switch (m_TextureImporterSettings.textureType) { case TextureImporterType.Default: output = TextureGeneratorHelper.GenerateTextureDefault(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureAlphaSettings, textureMipmapSettings, textureCubemapSettings, textureWrapSettings); break; case TextureImporterType.NormalMap: var textureNormalSettings = m_TextureImporterSettings.ExtractTextureNormalSettings(); output = TextureGeneratorHelper.GenerateNormalMap(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureNormalSettings, textureMipmapSettings, textureCubemapSettings, textureWrapSettings); break; case TextureImporterType.GUI: output = TextureGeneratorHelper.GenerateTextureGUI(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureAlphaSettings, textureMipmapSettings, textureWrapSettings); break; case TextureImporterType.Sprite: var textureSpriteSettings = m_TextureImporterSettings.ExtractTextureSpriteSettings(); textureSpriteSettings.packingTag = m_SpritePackingTag; textureSpriteSettings.qualifyForPacking = !string.IsNullOrEmpty(m_SpritePackingTag); textureSpriteSettings.spriteSheetData = new UnityEditor.Experimental.AssetImporters.SpriteImportData[spriteCount]; textureSettings.npotScale = TextureImporterNPOTScale.None; var spriteImportData = GetSpriteImportData(); for (int i = 0; i < spriteCount; ++i) { //AutoGenerateSpriteSkinData(m_SpriteImportData[spriteStart + i]); textureSpriteSettings.spriteSheetData[i] = spriteImportData[spriteStart + i]; } output = TextureGeneratorHelper.GenerateTextureSprite(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureSpriteSettings, textureAlphaSettings, textureMipmapSettings, textureWrapSettings); break; case TextureImporterType.Cursor: output = TextureGeneratorHelper.GenerateTextureCursor(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureAlphaSettings, textureMipmapSettings, textureWrapSettings); break; case TextureImporterType.Cookie: output = TextureGeneratorHelper.GenerateCookie(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureAlphaSettings, textureMipmapSettings, textureCubemapSettings, textureWrapSettings); break; case TextureImporterType.Lightmap: output = TextureGeneratorHelper.GenerateLightmap(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureMipmapSettings, textureWrapSettings); break; case TextureImporterType.SingleChannel: output = TextureGeneratorHelper.GenerateTextureSingleChannel(imageData, textureWidth, textureHeight, textureSettings, platformSettings, textureAlphaSettings, textureMipmapSettings, textureCubemapSettings, textureWrapSettings); break; default: Debug.LogAssertion("Unknown texture type for import"); output = default(TextureGenerationOutput); break; } UnityEngine.Profiling.Profiler.EndSample(); return(output); }