ActiveTextureManagement.TextureConverter.WriteTo C# (CSharp) Method

WriteTo() static private method

static private WriteTo ( Texture2D cacheTexture, string cacheFile, bool compress ) : bool
cacheTexture UnityEngine.Texture2D
cacheFile string
compress bool
return bool
        internal static bool WriteTo(Texture2D cacheTexture, string cacheFile, bool compress)
        {
            String directory = Path.GetDirectoryName(cacheFile + ".none");
            if (File.Exists(directory))
            {
                File.Delete(directory);
            }
            Directory.CreateDirectory(directory);
            FileStream imgStream = new FileStream(cacheFile, FileMode.Create, FileAccess.Write);
            imgStream.Position = 0;
            //byte[] png = cacheTexture.EncodeToPNG();
            byte[] img = cacheTexture.bytes(0);
            SquishFlags compression = SquishFlags.kDxt5;
            TextureFormat format = TextureFormat.DXT5;

            bool hasAlpha = texHasAlpha(img);
            if(!hasAlpha)
            {
                compression = SquishFlags.kDxt1;
                format = TextureFormat.DXT1;
            }

            for (int i = 0; i < cacheTexture.mipmapCount; i++)
            {
                int width = Math.Max(1, cacheTexture.width >> i);
                int height = Math.Max(1, cacheTexture.height >> i);
                if (compress)
                {
                    if (i != 0)
                    {
                        img = cacheTexture.bytes(i);
                    }
                    int size = squish.GetStorageRequirements(width, height, compression);
                    if (DatabaseLoaderTexture_ATM.UseSquish)
                    {
                        squish.CompressImage(img, width, height, imageBuffer, compression | SquishFlags.kColourIterativeClusterFit | SquishFlags.kWeightColourByAlpha, DatabaseLoaderTexture_ATM.WaitOnDone);
                    }
                    else
                    {
                        TextureToolsDXT.GetDXT(cacheTexture, i, imageBuffer, format);
                    }
                    imgStream.Write(imageBuffer, 0, size);
                }
                else
                {
                    img = cacheTexture.bytes(i, hasAlpha);
                    imgStream.Write(img, 0, img.Length);
                }
                if(width == 1 || height == 1)
                {
                    break;
                }
            }
            imgStream.Close();
            return hasAlpha;
        }

Usage Example

        private static TextureInfoWrapper RebuildCache(TexInfo Texture, bool compress, bool mipmaps)
        {
            Texture.loadOriginalFirst = true;
            ActiveTextureManagement.DBGLog("Loading texture...");
            TextureConverter.GetReadable(Texture, mipmaps);
            ActiveTextureManagement.DBGLog("Texture loaded.");

            TextureInfoWrapper cacheTexture = Texture.texture;
            Texture2D          tex          = cacheTexture.texture;

            String textureName = cacheTexture.name;
            String cacheFile   = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName;

            ActiveTextureManagement.DBGLog("Rebuilding Cache... " + Texture.name);

            ActiveTextureManagement.DBGLog("Saving cache file " + cacheFile + ".imgcache");
            tex.Apply(mipmaps);
            Color32[] colors   = tex.GetPixels32();
            bool      hasAlpha = TextureConverter.WriteTo(tex, cacheFile + ".imgcache", compress);

            String originalTextureFile = Texture.filename;
            String cacheConfigFile     = cacheFile + ".tcache";

            ActiveTextureManagement.DBGLog("Created Config for" + originalTextureFile);

            String hashString = GetMD5String(originalTextureFile);

            ConfigNode config = new ConfigNode();

            config.AddValue("md5", hashString); ActiveTextureManagement.DBGLog("md5: " + hashString);
            config.AddValue("orig_format", Path.GetExtension(originalTextureFile)); ActiveTextureManagement.DBGLog("orig_format: " + Path.GetExtension(originalTextureFile));
            config.AddValue("orig_width", Texture.width.ToString()); ActiveTextureManagement.DBGLog("orig_width: " + Texture.width.ToString());
            config.AddValue("orig_height", Texture.height.ToString()); ActiveTextureManagement.DBGLog("orig_height: " + Texture.height.ToString());
            config.AddValue("is_normal", cacheTexture.isNormalMap.ToString()); ActiveTextureManagement.DBGLog("is_normal: " + cacheTexture.isNormalMap.ToString());
            config.AddValue("is_compressed", compress); ActiveTextureManagement.DBGLog("is_compressed: " + compress);
            config.AddValue("width", Texture.resizeWidth.ToString()); ActiveTextureManagement.DBGLog("width: " + Texture.resizeWidth.ToString());
            config.AddValue("height", Texture.resizeHeight.ToString()); ActiveTextureManagement.DBGLog("height: " + Texture.resizeHeight.ToString());
            config.AddValue("hasAlpha", hasAlpha); ActiveTextureManagement.DBGLog("hasAlpha: " + hasAlpha.ToString());
            config.AddValue("hasMipmaps", mipmaps); ActiveTextureManagement.DBGLog("hasMipmaps: " + hasAlpha.ToString());
            config.Save(cacheConfigFile);
            ActiveTextureManagement.DBGLog("Saved Config.");

            if (compress)
            {
                tex.Compress(true);
            }
            cacheTexture.isCompressed = compress;
            tex.Apply(false, Texture.makeNotReadable);

            cacheTexture.isReadable = !Texture.makeNotReadable;

            return(cacheTexture);
        }
All Usage Examples Of ActiveTextureManagement.TextureConverter::WriteTo