Example #1
0
 void WebRequestButton()
 {
     if (GUILayout.Button("Build font database"))
     {
         webRequest = UnityWebRequest.Get("https://www.googleapis.com/webfonts/v1/webfonts?key=" + apiKey);
         webRequest.SendWebRequest();
         fontDatabase = null;
     }
 }
Example #2
0
    static void OpenWindow()
    {
        FontGrab window = EditorWindow.GetWindow <FontGrab>();

        // Clear all data when opening the window to make sure we're at a clear, knowable state
        webRequest   = null;
        fontData     = null;
        fontDatabase = null;

        // You must provide you're own API key! They're rate limited but easy to pick up
        if (File.Exists("font_apikey.txt"))
        {
            apiKey = File.ReadAllText("font_apikey.txt");
        }

        // If the font database was already loaded then load the cache, cuts down on API calls
        if (File.Exists(databasePath))
        {
            var text = File.ReadAllText(databasePath);
            fontDatabase = JsonUtility.FromJson <GoogleFontResponse>(text);
        }

        window.Show();
    }
Example #3
0
    void OnGUI()
    {
        if (!File.Exists("font_apikey.txt"))
        {
            // TODO: provide a url so people can easily get an API key
            // TODO: provide an easy way to save the API key
            GUILayout.Label("No API key found! place a file font_apikey.txt in the root of your project with the api key inside!");
            return;
        }

        if (fontDatabase == null)
        {
            // State 1 : Download the database

            if (webRequest == null)
            {
                WebRequestButton();
            }
            else
            {
                if (fontDatabase == null)
                {
                    GUILayout.Label("Building font library...");

                    EditorGUI.ProgressBar(new Rect(0, this.position.height - EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight), webRequest.downloadProgress, "Progress");

                    if (webRequest.isDone)
                    {
                        string database = webRequest.downloadHandler.text;
                        File.WriteAllText(databasePath, database);
                        fontDatabase = JsonUtility.FromJson <GoogleFontResponse>(database);
                        webRequest.Dispose();
                        webRequest = null;
                        fontData   = null;
                    }
                }
            }
        }
        else if (fontData == null)
        {
            // State 2 : no current font

            if (webRequest == null)
            {
                if (GUILayout.Button("Grab a random font!"))
                {
                    GetRandomFont();
                }

                if (GUILayout.Button("Rebuild Database"))
                {
                    webRequest = UnityWebRequest.Get("https://www.googleapis.com/webfonts/v1/webfonts?key=" + apiKey);
                    webRequest.SendWebRequest();
                    fontDatabase = null;
                }
            }
            else
            {
                if (!webRequest.isDone)
                {
                    GUILayout.Label("Downloading!");
                    GUILayout.Label($"Response code: {webRequest.responseCode}");
                    EditorGUI.ProgressBar(new Rect(0, this.position.height - EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight), webRequest.downloadProgress, "Progress");
                }
                else
                {
                    fontData = webRequest.downloadHandler.data;

                    File.WriteAllBytes(tempPath, fontData);
                    AssetDatabase.Refresh();

                    currentFont            = AssetDatabase.LoadAssetAtPath <Font>(tempPath);
                    previewStyle           = new GUIStyle(GUI.skin.label);
                    previewStyle.fontSize *= 2;

                    previewStyle.font = currentFont;
                }
            }
        }
        else
        {
            // State 3 : current font, reroll, save

            GUILayout.Label("How's this cool font?", previewStyle);

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Save it!"))
            {
                File.WriteAllBytes(fontOutputPath + outputPath, fontData);
            }

            if (GUILayout.Button("Reroll"))
            {
                GetRandomFont();
            }

            GUILayout.EndHorizontal();
        }
    }