Esempio n. 1
0
        private void ReduceTextureSizeIfNeeded(string texturePath, float maxSize)
        {
            string finalTexturePath = finalDownloadedPath + texturePath;

            byte[] image = File.ReadAllBytes(finalTexturePath);

            var tmpTex = new Texture2D(1, 1);

            if (!ImageConversion.LoadImage(tmpTex, image))
            {
                return;
            }

            float factor = 1.0f;
            int   width  = tmpTex.width;
            int   height = tmpTex.height;

            float maxTextureSize = maxSize;

            if (width < maxTextureSize && height < maxTextureSize)
            {
                return;
            }

            if (width >= height)
            {
                factor = (float)maxTextureSize / width;
            }
            else
            {
                factor = (float)maxTextureSize / height;
            }

            Texture2D dstTex = TextureHelpers.Resize(tmpTex, (int)(width * factor), (int)(height * factor));

            byte[] endTex = ImageConversion.EncodeToPNG(dstTex);
            UnityEngine.Object.DestroyImmediate(tmpTex);

            File.WriteAllBytes(finalTexturePath, endTex);

            AssetDatabase.ImportAsset(finalDownloadedAssetDbPath + texturePath, ImportAssetOptions.ForceUpdate);
            AssetDatabase.SaveAssets();
        }