Search Unity

Simple GUI color picker [using sliders]

Discussion in 'Immediate Mode GUI (IMGUI)' started by Elis_Sokolowski, Jun 28, 2013.

  1. Elis_Sokolowski

    Elis_Sokolowski

    Joined:
    Mar 18, 2013
    Posts:
    3
    Hi guys,

    As part of a current project I'm working on I needed to implement a colour picker.
    After seeing a few requests in this forum I thought it would be helpful to share the code with you guys.


    Code (csharp):
    1. public static Color ColorPicker(Rect rect, Color color)
    2.     {
    3.         //Create a blank texture.
    4.         Texture2D tex = new Texture2D(40,40);
    5.        
    6.        
    7.         GUILayout.BeginArea(rect,"","Box");
    8.        
    9.         #region Slider block
    10.         GUILayout.BeginHorizontal();
    11.         GUILayout.BeginVertical("Box");
    12.         //Sliders for rgb variables betwen 0.0 and 1.0
    13.         GUILayout.BeginHorizontal();
    14.         GUILayout.Label("R",GUILayout.Width(10));
    15.         color.r = GUILayout.HorizontalSlider(color.r,0f,1f);
    16.         GUILayout.EndHorizontal();
    17.        
    18.         GUILayout.BeginHorizontal();
    19.         GUILayout.Label("G",GUILayout.Width(10));
    20.         color.g = GUILayout.HorizontalSlider(color.g,0f,1f);
    21.         GUILayout.EndHorizontal();
    22.        
    23.         GUILayout.BeginHorizontal();
    24.         GUILayout.Label("B",GUILayout.Width(10));
    25.         color.b = GUILayout.HorizontalSlider(color.b,0f,1f);
    26.         GUILayout.EndHorizontal();
    27.         GUILayout.EndVertical();
    28.         #endregion
    29.        
    30.         //Color Preview
    31.         GUILayout.BeginVertical("Box",new GUILayoutOption[]{GUILayout.Width(44),GUILayout.Height(44)});
    32.         //Apply color to following label
    33.         GUI.color = color;
    34.         GUILayout.Label(tex);
    35.         //Revert color to white to avoid messing up any following controls.
    36.         GUI.color = Color.white;
    37.            
    38.         GUILayout.EndVertical();
    39.         GUILayout.EndHorizontal();
    40.        
    41.         //Give color as RGB values.
    42.         GUILayout.Label("Current Colour = " + (int)(color.r * 255) + "|" + (int)(color.g * 255) + "|" + (int)(color.b * 255));
    43.        
    44.         GUILayout.EndArea();
    45.         //Finally return the modified value.
    46.         return color;
    47.     }
    The ColorPicker works in exactly the same way as a TextField or Toggle item in the standard Unity GUI which is as follows.

    Code (csharp):
    1. Color someColor = Color.white;
    2.  
    3. void OnGUI() {
    4.         someColor = ColorPicker(new Rect(0,0,120,120),someColor);
    5. }
    Let me know if you have any issues with this, and I hope it helps make your own projects a little bit easier!

    Elis
     

    Attached Files:

    Last edited: Jun 28, 2013
    lerrific likes this.
  2. RetepTrun

    RetepTrun

    Joined:
    Jul 31, 2011
    Posts:
    9
    Thanks I might be needing this for materials.