Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ereptor · Sep 23, 2014 at 10:07 PM · c#colorrandom.rangeguilayout

Color GUILayout Buttons from Array of Colors (C#)

Hi everyone, I'm having a bit of an issue and right when I thought I figured it out I have a problem. I'm making a game that allows you to pick from 30 randomly-colored icons.

Here is an array of colors I've created:

 public Color[] avatarColors = new Color[9];
 
 void Start () {
 
         avatarColors [0] = new Color (255/255f, 130/255f, 171/255f); //palevioletred1
         avatarColors [1] = new Color (218/255f, 112/255f, 214/255f); //orchid
         avatarColors [2] = new Color (131/255f, 111/255f, 255/255f); //slateblue1
         avatarColors [3] = new Color (100/255f, 149/255f, 237/255f); //cornflowerblue
         avatarColors [4] = new Color (174/255f, 238/255f, 238/255f); //paleturquoise2
         avatarColors [5] = new Color (152/255f, 251/255f, 152/255f); //palegreen
         avatarColors [6] = new Color (154/255f, 205/255f, 50/255f); //olivedrab3
         avatarColors [7] = new Color (240/255f, 230/255f, 140/255f); //khaki
         avatarColors [8] = new Color (255/255f, 193/255f, 37/255f); //goldenrod1
 
     }

Now, making my GUILayout use these colors should be simple, here is my attempt:

 for (int i = 0; i < 3; i++) {
             GUILayout.BeginHorizontal ();
             GUILayout.FlexibleSpace ();
             for (int j = 0; j < 10; j++){
                 GUI.contentColor = avatarColors[Random.Range (0, avatarColors.Length)];
                 if(GUILayout.Button (avatarImage, faceplateStyle, GUILayout.Width (avatarWidth), GUILayout.Height(avatarHeight))){}
                 GUI.contentColor = preColor;
             }
             GUILayout.FlexibleSpace ();
             GUILayout.EndHorizontal ();
             GUILayout.Space (bufferSpace);
         }


This successfully creates three rows of ten icons (30 items total) with random colors; however, the colors fluctuate constantly! I'm not sure how to assign the colors in a way that is both completely random, and that doesn't change once it's been set.

Thank you in advance to anyone who can 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

2 Replies

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

Answer by Ereptor · Sep 24, 2014 at 08:48 PM

I fixed my problem, here is the result:

 public Color[] avatarColors = new Color[10];

     void Start () {
 
         //There are ten potential pastel colors for coloring avatars, one for each digit 0-9.
         avatarColors [0] = new Color (255/255f, 130/255f, 171/255f); //palevioletred1
         avatarColors [1] = new Color (218/255f, 112/255f, 214/255f); //orchid
         avatarColors [2] = new Color (131/255f, 111/255f, 255/255f); //slateblue1
         avatarColors [3] = new Color (100/255f, 149/255f, 237/255f); //cornflowerblue
         avatarColors [4] = new Color (174/255f, 238/255f, 238/255f); //paleturquoise2
         avatarColors [5] = new Color (152/255f, 251/255f, 152/255f); //palegreen
         avatarColors [6] = new Color (154/255f, 205/255f, 50/255f); //olivedrab3
         avatarColors [7] = new Color (255/255f, 246/255f, 143/255f); //khaki1
         avatarColors [8] = new Color (255/255f, 215/255f, 0/255f); //gold1
         avatarColors [9] = new Color (205/255f, 92/255f, 92/255f); //indianred
 
         ShuffleArray (avatarColors);
 
     }

 void OnGUI () {
 
 GUILayout.BeginArea (menuBackground);
             
             //This section creates avatar icons in the menu. These icons are not the actual avatars.
             for (int i = 0; i < 3; i++) {
                 GUILayout.BeginHorizontal ();
                 GUILayout.FlexibleSpace ();
                 for (int j = 0; j < 10; j++){
                     int k = Mathf.Abs ((i + 9) * (j * 7));
                     while(k >= 10)
                         k /= 10;
                     GUI.contentColor = avatarColors[k];
                     if(GUILayout.Button (avatarImage, faceplateStyle, GUILayout.Width (avatarWidth), GUILayout.Height(avatarHeight))){}
                     GUI.contentColor = preColor;
                 }
                 GUILayout.FlexibleSpace ();
                 GUILayout.EndHorizontal ();
                 GUILayout.Space (bufferSpace);
             }
 
             GUILayout.EndArea (); }

     //This function can shuffle an array. Call it using ShuffleArray(yourArray);
     public static void ShuffleArray<T>(T[] arr) {
         for (int i = arr.Length - 1; i > 0; i--) {
             int r = Random.Range(0, i);
             T tmp = arr[i];
             arr[i] = arr[r];
             arr[r] = tmp;
         }
     }


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
avatar image
0

Answer by Eric5h5 · Sep 23, 2014 at 10:30 PM

Randomize the colors in the array first, rather than randomly picking colors from it every frame. By the way, use Color32 instead of Color, then you don't need to do math.

Comment
Add comment · Show 2 · 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 Ereptor · Sep 24, 2014 at 12:42 AM 0
Share

So, if I use:

     public static void ShuffleArray<T>(T[] arr) {
     for (int i = arr.Length - 1; i > 0; i--) {
     int r = Random.Range(0, i);
     T tmp = arr[i];
     arr[i] = arr[r];
     arr[r] = tmp;
     }
     }
 
     ShuffleArray(arr);

(Thank you to user vipaware for this randomize array tool.)

...How would I call a color from within this GUI structure?

  GUI.contentColor = avatarColors[x];

...will make all the icons the same color...?

avatar image Ereptor · Sep 24, 2014 at 12:54 AM 0
Share

I was thinking of extending avatarColors by one and using:

GUI.contentColor = avatarColors[Abs(j-i)];

But this would create a non-random, very obvious pattern:

0 1 2 3 4 5 6 7 8 9 1 0 1 2 3 4 5 6 7 8 2 1 0 1 2 3 4 5 6 7

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Change the color of the string 2 Answers

how to accomplish:"When click a button , four button occour ." 0 Answers

Accessing color presets in c# script. 1 Answer


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