Unity TinyPNG compression

Compress Textures in Unity Editor by using TinyPNG

public class TinyPNGEditor : MonoBehaviour
{
    private const string URL_API = "https://api.tinify.com/shrink";
    
    // your key here
    private const string KEY_API = "xxxxxxx";


    private static UnityWebRequest www;
    private static Action onDone;
<!-- more -->


    private static string Auth_Key
    {
        get
        {
            var key = "api:" + KEY_API;
            key = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(key));
            return string.Format("Basic {0}", key);
        }
    }

    //{"size":91,"type":"image/png"}
    [Serializable]
    public class InputModel
    {
        public int size;
        public string type;
    }


    //{"size":91,"type":"image/png","width":16,"height":16,"ratio":1,"url":"https://api.tinify.com/output/d6vu2y5mm231fjkjhp6fj22jd648k2vz"}
    [Serializable]
    public class OutputModel
    {
        public int size;
        public string type;
        public int width;
        public int height;
        public float ratio;
        public string url;
    }

    [Serializable]
    public class ResponseModel
    {
        public InputModel input;
        public OutputModel output;
    }

    public static void Compress(Texture2D texture, bool overwrite = false)
    {
        var filePath = Application.dataPath + AssetDatabase.GetAssetPath(texture).Replace("Assets", "");
        var bytes = File.ReadAllBytes(filePath);
        Debug.Log("file:" + filePath);
        UnityWebRequest www = UnityWebRequest.Put(URL_API, bytes);
        www.SetRequestHeader("Authorization", Auth_Key);
        www.method = UnityWebRequest.kHttpVerbPOST;
        Debug.Log("-->" + www.url);
        www.SendWebRequest();

        while (!www.isDone)
        {
//            Debug.Log("running");
        }


        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }

        if (!string.IsNullOrEmpty(www.downloadHandler.text))
        {
            try
            {
                var model = JsonUtility.FromJson<ResponseModel>(www.downloadHandler.text);
                www.Dispose();

                var url = model.output.url;
                Debug.Log("<--" + url);

                www = UnityWebRequest.Get(url);
                www.SendWebRequest();

                while (!www.isDone)
                {
                }

                if (www.downloadHandler.data.Length > 100)
                {
                    File.WriteAllBytes(overwrite ? filePath : filePath.Replace(".png", "_tiny.png"),
                        www.downloadHandler.data);
                    AssetDatabase.Refresh();
                    AssetDatabase.ImportAsset(filePath);
                    
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
    }
}
WRITTEN BY:    Suntabu