0
\$\begingroup\$

I used a break point and it's getting to the line:

style.normal.textColor = Color.green;

But not changing the color of the clicked button. And there are no any errors or exceptions.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEngine;
using System.IO;

public class Manager : EditorWindow
{
    [MenuItem("Tools/Manager")]
    static void Manage()
    {
        EditorWindow.GetWindow<Manager>();
    }

    private void OnGUI()
    {
        var style = new GUIStyle(GUI.skin.button);
        style.normal.textColor = Color.red;
        style.fontSize = 18;

        string[] assetPaths = new string[2];
        string[0] = "Test";
        string[1] = "Test1";

        foreach (string assetPath in assetPaths)
        {
            if (assetPath.Contains(".test")
            {
                var i = assetPath.LastIndexOf("/");
                var t = assetPath.Substring(i + 1);

                if (GUILayout.Button(t, style, GUILayout.Width(1000), GUILayout.Height(50)))
                {
                    style.normal.textColor = Color.green;
                }
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ OnGUI runs every frame move the initial style set up outside of onGUI create an init function and call it at the end of the Manage method, it's being reset to red every frame, I think. \$\endgroup\$ – Pheonix2105 Aug 7 '18 at 22:34
1
\$\begingroup\$

Edit : Removed an incorrect answer updated with hopefully a correct one.

It turns the solution is pretty simple

    style = new GUIStyle(EditorStyles.toolbarButton);
    //the color if the button is pressed
    style.active.textColor = Color.red;
    //the default color
    style.normal.textColor = Color.green;

    style.fontSize = 18;

The full code

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEngine;
using System.IO;

public class Manager : EditorWindow
{
    [MenuItem("Tools/Manager")]
    static void Manage()
    {
        EditorWindow.GetWindow<Manager>();
    }

    private void OnGUI()
    {
        var style = new GUIStyle(GUI.skin.button);
        style.normal.textColor = Color.red;
        style.active.textColor = Color.green;
        style.fontSize = 18;

        string[] assetPaths = new string[2];
        string[0] = "Test";
        string[1] = "Test1";

        foreach (string assetPath in assetPaths)
        {
            if (assetPath.Contains(".test")
            {
                var i = assetPath.LastIndexOf("/");
                var t = assetPath.Substring(i + 1);

                if (GUILayout.Button(t, style, GUILayout.Width(1000), GUILayout.Height(50)))
                {
                                        }
            }
        }
    }
}

Also worth noting but not related to answering your question is EditorStyles is full of useful Unity styles you can apply to elements.

\$\endgroup\$
  • \$\begingroup\$ I'm getting some exceptions the first one is : ArgumentException: You can only call GUI functions from inside OnGUI that on the line that call Initi(); \$\endgroup\$ – Benzi Avrumi Aug 7 '18 at 22:43
  • \$\begingroup\$ GUIStyle itself isnt a GUI function like GUI.Button etc so that's strange, I'll set up a demo and see if I can reproduce the error and get back to you. \$\endgroup\$ – Pheonix2105 Aug 7 '18 at 22:45
  • \$\begingroup\$ Sorry there was an extra bracket in the code above and I was wrong about the GUIStyle, in my code it also only runs inside OnGUI just each object of mine calls its own init function once inside. I'll update the post with the way I've used to change colors on certain actions. \$\endgroup\$ – Pheonix2105 Aug 7 '18 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.