示例#1
0
        public static void CreateTexture(Texture vpxTex, string textureFolder)
        {
            var unityTex = vpxTex.ToUnityTexture();

            byte[] bytes = null;
            if (vpxTex.IsHdr)
            {
                // this is a hack to decompress the texture or unity will throw an error as it cant write compressed files.
                var renderTex = RenderTexture.GetTemporary(
                    unityTex.width,
                    unityTex.height,
                    0,
                    RenderTextureFormat.Default,
                    RenderTextureReadWrite.Linear
                    );
                var previous = RenderTexture.active;
                RenderTexture.active = renderTex;
                Graphics.Blit(unityTex, renderTex);
                var rawImage = new Texture2D(unityTex.width, unityTex.height, TextureFormat.RGBAFloat, false);
                rawImage.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
                rawImage.Apply();
                RenderTexture.active = previous;
                RenderTexture.ReleaseTemporary(renderTex);
                bytes = rawImage.EncodeToEXR(Texture2D.EXRFlags.CompressZIP);
            }
            else
            {
                bytes = unityTex.EncodeToPNG();
            }

            var path = vpxTex.GetUnityFilename(textureFolder);

            File.WriteAllBytes(path, bytes);
            AssetDatabase.ImportAsset(path);
        }
        public static void WriteAsAsset(this Texture texture, string folder, bool skipIfExists)
        {
            var path = texture.GetUnityFilename(folder);

            if (skipIfExists && File.Exists(path))
            {
                return;
            }

            // convert if bmp
            if (texture.ConvertToPng)
            {
                using var im = texture.GetImage();
                im.Pngsave(path);
            }
            else if (texture.IsWebp)
            {
                // write both original and png conversion
                File.WriteAllBytes(path, texture.Content);

                using var im = texture.GetImage();
                im.Pngsave(Path.Combine(
                               Path.GetDirectoryName(path) ?? "",
                               Path.GetFileNameWithoutExtension(path) + ".png"
                               ));
            }
            else                 // might need to convert other formats like webp
            {
                File.WriteAllBytes(path, texture.Content);
            }
        }