Unity GUILayout Button

**public class LevelEditor : EditorWindow
{
int width;
int height;
int map;

	bool buttonClicked = false;


	[MenuItem("Window/Level Editor")]
	public static void ShowWindow()
	{
		EditorWindow.GetWindow(typeof(LevelEditor));
	}

	void OnGUI()
	{

		if (buttonClicked) {
			GUI.color = Color.yellow;
	
		}else{

			GUI.color = Color.green;
		}



		GUILayout.BeginVertical();
		{
			width = EditorGUILayout.IntField(width);
			height = EditorGUILayout.IntField(height);
			if (GUILayout.Button ("Generate")) 
			{
				UpdateArray(width, height);

			}

			
			if (map != null) {
				foreach (var y in Enumerable.Range(0, map.Length)) {

					GUILayout.BeginHorizontal ();
					{
						foreach (var x in Enumerable.Range(0, map[y].Length)) {


							if (GUILayout.Button (string.Format ("{0},{1}:{2}", x, y, map[y][x])))							
							{
	
								map [y] [x]++;
								buttonClicked = true;


								Debug.Log("Main Button clicked!");
				
							}

						}
					}
					GUILayout.EndHorizontal ();
				}
			}
		}
		GUILayout.EndVertical();
	}
		
	void UpdateArray(int width, int height)
	{
		map = new int[height][];
		foreach (var y in Enumerable.Range(0, height)) 
		{
			map[y] = new int[width];
		}
	}

}**

what i want to happen is when i clicked button it will change color red,yellow,blue,pink, example when i click 1st . the button will become red, when i click again it will become yellow, and when i click again. it will become color blue. then repeat… thx for help

First thought that I have to cycle through colors like this, would be to create an array of colors, and an int variable to hold your current index into that array.

In the click, increment the int, check to see if it is larger than the array and reset it to 0 if it is.
Then change color to the color in the array at the index you have in the int.

That way it would step through each color you have in the array till it reached the end, then start back at the beginning of the array again.

Hope this helps,
-Larry

For some reason it will not let me submit the updated code ;(
As soon as I paste the code in, the submit button quits working ;(
Maybe the responses have gotten too long? Who knows.
Trying it as a separate answer.

Let me know if this produces the results you want.

public class LevelEditor : EditorWindow
{
    int width;
    int height;
    int[][] map;
    Color[] colors = new Color[] { Color.red, Color.yellow, Color.blue, Color.magenta };
    string[] colorNames = new string[] { "Red", "Yellow", "Blue", "Magenta" };
    int[] ButtonColorIndexs;
    int ButtonCounter = 0;
    bool buttonClicked = false;

    [MenuItem("Window/Level Editor")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(LevelEditor));
    }

    void OnGUI()
    {
        GUILayout.BeginVertical();
        {
            width = EditorGUILayout.IntField(width);
            height = EditorGUILayout.IntField(height);
            if (GUILayout.Button("Generate"))
            {
                UpdateArray(width, height);
                ButtonColorIndexs = new int[(width * height) + 1];
            }

            if (map != null)
            {
                ButtonCounter = 0;
                foreach (var y in Enumerable.Range(0, map.Length))
                {
                    GUILayout.BeginHorizontal();
                    {
                        foreach (var x in Enumerable.Range(0, map[y].Length))
                        {
                            GUI.color = colors[ButtonColorIndexs[ButtonCounter]];
                            if (GUILayout.Button(string.Format("{0},{1}:{2}", x, y, map[y][x])))
                            {
                                ButtonColorIndexs[ButtonCounter]++;
                                if (ButtonColorIndexs[ButtonCounter] > colors.Length - 1)
                                    ButtonColorIndexs[ButtonCounter] = 0;

                                map[y][x]++;
                                buttonClicked = true;
                                ButtonCounter++;

                                Debug.Log("Main Button clicked!");
                            }
                            ButtonCounter++;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }
        }
        GUILayout.EndVertical();

        GUILayout.BeginVertical();
        {
            if (map != null)
            {
                ButtonCounter = 0;
                foreach (var y in Enumerable.Range(0, map.Length))
                {
                    GUILayout.BeginHorizontal();
                    {
                        foreach (var x in Enumerable.Range(0, map[y].Length))
                        {
                            GUILayout.Label(string.Format("{0},{1}:{2}", x, y, map[y][x]) + " " + colorNames[ButtonColorIndexs[ButtonCounter]]);
                            ButtonCounter++;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }
        }
        GUILayout.EndVertical();
    }

    void UpdateArray(int width, int height)
    {
        map = new int[height][];
        foreach (var y in Enumerable.Range(0, height))
        {
            map[y] = new int[width];
        }
    }
}

-Larry

No problem. Free free to reach out anytime. I am always glad to help.

Had to add as a new answer. Too large for a comment.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEditor;
using UnityEngine;

public class LevelEditor : EditorWindow
{
    int width;
    int height;
    int[][] map;
    Color[] colors = new Color[] { Color.red, Color.yellow, Color.blue, Color.magenta };
    string[] colorNames = new string[] { "Red", "Yellow", "Blue", "Magenta" };
    int[] ButtonColorIndexs;
    int ButtonCounter = 0;
    bool buttonClicked = false;

    [MenuItem("Window/Level Editor")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(LevelEditor));
    }

    void OnGUI()
    {
        GUILayout.BeginVertical();
        {
            width = EditorGUILayout.IntField(width);
            height = EditorGUILayout.IntField(height);
            if (GUILayout.Button("Generate"))
            {
                UpdateArray(width, height);
                ButtonColorIndexs = new int[(width * height) + 1];
            }
            if (GUILayout.Button("Save"))
            {
                Save();
            }
            if (GUILayout.Button("Load"))
            {
                Load();
            }
            if (map != null)
            {
                ButtonCounter = 0;
                foreach (var y in Enumerable.Range(0, map.Length))
                {
                    GUILayout.BeginHorizontal();
                    {
                        foreach (var x in Enumerable.Range(0, map[y].Length))
                        {
                            GUI.color = colors[ButtonColorIndexs[ButtonCounter]];
                            if (GUILayout.Button(string.Format("{0},{1}:{2}", x, y, map[y][x])))
                            {
                                ButtonColorIndexs[ButtonCounter]++;
                                if (ButtonColorIndexs[ButtonCounter] > colors.Length - 1)
                                    ButtonColorIndexs[ButtonCounter] = 0;

                                map[y][x]++;
                                buttonClicked = true;
                                ButtonCounter++;

                                Debug.Log("Main Button clicked!");
                            }
                            ButtonCounter++;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }
        }
        GUILayout.EndVertical();

        GUILayout.BeginVertical();
        {
            if (map != null)
            {
                ButtonCounter = 0;
                foreach (var y in Enumerable.Range(0, map.Length))
                {
                    GUILayout.BeginHorizontal();
                    {
                        foreach (var x in Enumerable.Range(0, map[y].Length))
                        {
                            GUILayout.Label(string.Format("{0},{1}:{2}", x, y, map[y][x]) + " " + colorNames[ButtonColorIndexs[ButtonCounter]]);
                            ButtonCounter++;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }
        }
        GUILayout.EndVertical();
    }

    void UpdateArray(int width, int height)
    {
        map = new int[height][];
        foreach (var y in Enumerable.Range(0, height))
        {
            map[y] = new int[width];
        }
    }

    public void Save()
    {
        string dataPath = string.Format("{0}/map.dat", Application.persistentDataPath);
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        FileStream fileStream;

        try
        {
            if (File.Exists(dataPath))
            {
                File.WriteAllText(dataPath, string.Empty);
                fileStream = File.Open(dataPath, FileMode.Open);
            }
            else
            {
                fileStream = File.Create(dataPath);
            }

            MapData md = new MapData(map, ButtonColorIndexs);
            binaryFormatter.Serialize(fileStream, md);
            fileStream.Close();
        }
        catch (Exception e)
        {
            Debug.Log("Failed to Save: " + e.Message);
        }
    }

    public void Load()
    {

        string dataPath = string.Format("{0}/map.dat", Application.persistentDataPath);

        try
        {
            if (File.Exists(dataPath))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                FileStream fileStream = File.Open(dataPath, FileMode.Open);

                MapData md = (MapData)binaryFormatter.Deserialize(fileStream);
                map = md.map;
                height = map.Length;
                width = map[0].Length;
                ButtonColorIndexs = md.ButtonColorIndexs;
                fileStream.Close();
            }
        }
        catch (Exception e)
        {
            Debug.Log("Failed to Load: " + e.Message);
        }
    }

}

[Serializable]
public class MapData
{
    public int[][] map;
    public int[] ButtonColorIndexs;

    public MapData(int[][] map, int[] buttonColorIndexs)
    {
        this.map = map;
        this.ButtonColorIndexs = buttonColorIndexs;
    }
}

This adds the requested Save and Load functionality to the script.
-Larry