- Home /
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
Answer by Larry-Dietz · Dec 28, 2017 at 07:51 AM
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
absolutely yes. thank you verry much. but can i ask questions again sometimes? hehe i want to be a professional programmer like you.... hope 2 pts reward will help
how to put save button and when i click save it will save the update string in some pad or script? this is last xD
Added a new script as a separate answer. Adding the Save and Load made the script too large to fit within a comment. -Larry
They said i must use FileStream. so this is my filestream code. and it gives me error` if (GUILayout.Button("Save")){
using (FileStream fs = new FileStream("assets/LevelResources/LevEd.txt",File$$anonymous$$ode.Create, FileAccess.Write)){
fs.Close();
using (FileStream fs2 = new FileStream("assets/LevelResources/LevEd.txt",File$$anonymous$$ode.Open, FileAccess.Write)){
using (StreamWriter sw = new StreamWriter(fs2)){
sw.WriteLine(string.Format("{2}") + ",");
sw.Close();
fs2.Close();
}
}
}//end of FileStream
}`
Answer by Larry-Dietz · Dec 26, 2017 at 03:16 PM
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
can you edit my code for visual example? ill create int to change the color. but what happen is when i click 1 of the button. all button is changing its color. is there a proper way to change the color that i just clicked ? without affecting other button? when i didn't click them
Getting a color to cycle, and getting an unknown amount of buttons to cycle independently are very different :) Cycling each button on it own was a bit more difficult, but this should work for you.
public class LevelEditor : EditorWindow
{
int width;
int height;
int[][] map;
Color[] colors = new Color[] { Color.red, Color.yellow, Color.blue, Color.magenta };
int[] ButtonColorIndexs;
int ButtonCounter=0;
bool buttonClicked = false;
[$$anonymous$$enuItem("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("$$anonymous$$ain Button clicked!");
}
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];
}
}
}
thanks a lot. but how if i want to show some text like when the color is red. the text in button will also show red?
Ins$$anonymous$$d of the numbers you are displaying in there now? If so, the easiest was would be to create a secondary string array holding the names of the colors, in the same position as the colors array...
i.e.
string[] colorNames = new string[] { "Red", "Yellow", "Blue", "$$anonymous$$agenta"};
then change your GUILayout.Button code to ...
if (GUILayout.Button(colorNames[ButtonColorIndexs[ButtonCounter]]))
That should result in buttons that change colors, and display the color name in the button
ow well nevermid the color text... but this my new problem how can i convert to string the # value of the color i mean all numbers per button will be printed under them like this
Not sure I understand what you are asking. If you are trying to convert the index number to a string, just do a .ToString() on the index...
i,e, string IndexString = ButtonColorIndexs[ButtonCounter].ToString();
That will put the color index number into IndexString as a string value that will be either "0", "1", "2" or "3"
I am sure I am misunderstanding what you are asking. -Larry
Answer by Larry-Dietz · Dec 28, 2017 at 08:46 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612145231im_/https://answers.unity.com/themes/thub/images/avi.jpg)