Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing texture of GUILayout button when pressed

So I have a custom editor script. And I am trying to change a buttons texture when pressed. I have an array that has textures. When button pressed it will change to next one.

        for (int row = 0; row < Mathf.Sqrt(script.patternMatrix.GetLength(0)); row++)
    {
        EditorGUILayout.BeginHorizontal();
        {
            GUILayout.FlexibleSpace();
            EditorGUILayout.LabelField((row + 1).ToString(), GUILayout.MaxWidth(15.0f), GUILayout.MaxHeight(50.0f));

            EditorGUILayout.BeginHorizontal();
            {
                for (int col = 0; col < Mathf.Sqrt(script.patternMatrix.GetLength(0)); col++)
                {
                    if (GUILayout.Button(Balls[0].BallTexture, GUILayout.Width(50), GUILayout.Height(50)))
                    {

                    }
                }

            }
            EditorGUILayout.EndHorizontal();
            GUILayout.FlexibleSpace();
        }
        EditorGUILayout.EndHorizontal();
    }

I have a scriptable object that has a matrix. In this part I am creating buttons.

Well button is not a variable, So I couldn’t find a way to change its texture afterwards.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the if is only true for one single moment when the button is clicked. So within it you have to change some index state that is stored on the Editor class scope.

For example something like

// class level field
private int textureIndex;

...

if (GUILayout.Button(Balls[textureIndex].BallTexture, GUILayout.Width(50), GUILayout.Height(50)))
{
    textureIndex++;
}

depending of course on your exact use case

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading