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
2
Question by chips · Nov 07, 2011 at 12:01 AM · guiinputcolor

Get the color under the mouse cursor

Hello there!

Getting the color of a pixel on a 3d object is pretty easy, but anyone knows how to do the same thing on the GUI? What I need is a color picker tool.

Thanks!

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

Answer by tylo · Nov 07, 2011 at 07:09 AM

There is a thread about this on the Unity forums that has a clever solution. I'm actually trying to implement it myself right now.

For some reason, though, the GetPixel method keeps returning a value I'm not expecting.

Ok, here is the solution I use. First, you need to get yourself any color picking texture. I used this one. Now, for some reason the closer I would get to "Black" the brighter the color would get. So my solution was to display one texture and read a color from another that was flipped. You drag these two textures into the ColorPicker inspector's respective slots.

First, I've altered this code file from the above form thread which includes a delegate and event system so that the actual setting of the color can be done outside of this class:

 using UnityEngine;
 using System;
 using System.Collections;

 using System.Runtime.CompilerServices;

 public class ColorPicker : MonoBehaviour {
   public Texture2D colorPicker;
   public Texture2D colorPickerFlipped;
   public int ImageWidth = 256;
   public int ImageHeight = 256;
  
   public delegate void OnColorPickedHandler(Color c);
     private OnColorPickedHandler m_onColorPickedEvent= delegate{ };
 
   private static ColorPicker instance;
 
     public static ColorPicker Instance{
         get{
             if (instance == null)
                 instance = new GameObject ("ColorPicker").AddComponent<ColorPicker> ();
 
             return instance;
         }
     }
  
     public static event OnColorPickedHandler OnColorPickedEvent
     {
         [MethodImpl(MethodImplOptions.Synchronized)]
         add
         {
             Instance.m_onColorPickedEvent -= value;
             Instance.m_onColorPickedEvent += value;
         }
      
         [MethodImpl(MethodImplOptions.Synchronized)]
         remove
         {
             Instance.m_onColorPickedEvent -= value;   
         }
     }
 
   public void OnColorPicked(Color c){
       m_onColorPickedEvent(c);
   }
  
   bool m_pickColor;
  
   public static bool PickColor {
     get {
       return Instance.m_pickColor;
     }
     set {
       Instance.m_pickColor = value;
     }
   } 
  
   #region Monobehaviours
  
   public void Start(){
       instance = this;
     }
 
     public void OnApplicationQuit (){
         instance = null;
     }
  
   void OnGUI ()
   {
     if (m_pickColor && GUI.RepeatButton (new Rect (10, 10, ImageWidth, ImageHeight), colorPicker)) {
       Vector2 pickpos = Event.current.mousePosition;
       int aaa = Convert.ToInt32 (pickpos.x);
       int bbb = Convert.ToInt32 (pickpos.y);

       //Debug.Log (aaa + "," + bbb);

       Color col = colorPickerFlipped.GetPixel (aaa, bbb);
     
       // "col" is the color value that Unity is returning.
       // Here you would do something with this color value, like
       // set a model's material tint value to this color to have it change
       // colors, etc, etc.

         OnColorPicked (col);
   
         if (Input.GetMouseButtonUp (0)) {
             m_pickColor = false;
           }
     }
   }
  
   #endregion
 }

Meanwhile, in another code file (like where your GUI is), you can subscribe to the ColorPicker's delegate and event system by doing the following:

 void OnGUI(){
     if(GUI.Button(new Rect(5,55, 200, 20),"Change Color!")){
                 ColorPicker.PickColor = true;
                 ColorPicker.OnColorPickedEvent += new ColorPicker.OnColorPickedHandler( OnPickColor );
     }
 }
 
 public void OnPickColor (Color c)
 {
     Debug.Log(c);
 }
 


Comment
Add comment · Show 1 · 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 CgShady · Jun 17, 2012 at 06:04 PM 0
Share

I was reading this much interesting thread about color picking, so first of all thanks for this, it saved me some searching.

Reading the following, I thought I might add my contribution :

Now, for some reason the closer I would get to "Black" the brighter the color would get. So my solution was to display one texture and read a color from another that was flipped.

That's because textures are stored and read "flipped" on the Y axis. You can change your code with something like :

int bbb = ImageHeight - Convert.ToInt32 (pickpos.y);

This will read the texture in the proper direction. Hope it helps..

avatar image
0

Answer by frogsbo · Jun 07, 2014 at 12:34 PM

ar i dunno:

 if (GUI.Button(Rect(160,120,150,20),"screentest"))
         {         
                 // Make a new texture of the right size and
                 // read the camera image into it.
                 var tex = new Texture2D(Screen.width, Screen.height);
                 tex.ReadPixels(new Rect(0, 0, 128, 128), 0, 0);
                 tex.Apply();
                 
                 // Set the display texture to the newly captured image.
                 //display.material.mainTexture = tex;
                 
                 // Reset the grab variable to avoid making multiple
                 // captures.        
         }
         
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Gui list and color 0 Answers

Trying to Highlight text in order to copy and paste, but do not want the text to be editable. 1 Answer

Changing the Standalone Input Manager variables 2 Answers

How do I make a colour editor UI? 1 Answer

Why won't my object activate? 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