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.
>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