Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Arin_Jisoo · Dec 26, 2017 at 08:26 AM · unity 5buttoncolor changeguilayout

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arin_Jisoo · Dec 28, 2017 at 08:48 AM 0
Share

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

avatar image Arin_Jisoo · Dec 28, 2017 at 09:34 AM 0
Share

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

avatar image Larry-Dietz Arin_Jisoo · Dec 28, 2017 at 10:11 PM 0
Share

Added a new script as a separate answer. Adding the Save and Load made the script too large to fit within a comment. -Larry

avatar image Arin_Jisoo Larry-Dietz · Dec 29, 2017 at 06:57 AM 0
Share

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
         }`
Show more comments
avatar image
0

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

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arin_Jisoo · Dec 27, 2017 at 08:20 AM 0
Share

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

avatar image Larry-Dietz Arin_Jisoo · Dec 27, 2017 at 03:48 PM 0
Share

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];
         }
     }
 
 }

 
avatar image Arin_Jisoo · Dec 27, 2017 at 08:20 AM 0
Share

thank you bye the way

avatar image Arin_Jisoo · Dec 28, 2017 at 06:01 AM 0
Share

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?

avatar image Larry-Dietz Arin_Jisoo · Dec 28, 2017 at 06:29 AM 0
Share

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

avatar image Arin_Jisoo · Dec 28, 2017 at 06:31 AM 0
Share

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 thisalt text

colorstring.png (42.8 kB)
avatar image Larry-Dietz Arin_Jisoo · Dec 28, 2017 at 06:47 AM 0
Share

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

avatar image Arin_Jisoo Larry-Dietz · Dec 28, 2017 at 07:23 AM 0
Share

what i want sir is the text value of button will be print in the underline below. with the same sorted

colorstwingle.png (79.5 kB)
Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I control the behavior of Android soft buttons (Immersive mode)? 1 Answer

How to know if Unity UI button is being held down? 0 Answers

Add Listeners to array of Buttons 2 Answers

Small GUILayout button 1 Answer

GUI button doesn't appear on Android 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges