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
1
Question by patrick_scheper · Apr 27, 2017 at 09:01 AM · unity 5uiimageslider

RGB UI Color Slider

Hey, guys!

So I am just doing some research on creating a color slider, something like this: alt text

alt text

People would just slide the button to the color they want and then the color would preview the selected color.

I hoped if someone could steer me in the right direction of creating something like this because I cannot find anything useful on Google...

Thanks anyway!

0ef27127860228881ec918dc85a2cba6.png (26.2 kB)
0ef27127860228881ec918dc85a2cba5.png (23.8 kB)
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
3

Answer by Hellium · Apr 27, 2017 at 09:09 AM

You could use a UI Slider with :

  • the image of your Hue bar as a background

  • A min value of 0, a max value of 1

  • A listener to the onValueChanged event setting the color of your handle as follow (code not tested)

     // Drag & drop slider
      public UnityEngine.UI.Slider slider ;
     
      // Drag & drop handle
      public UnityEngine.UI.Image handle ;
     
      public void Start()
      {
          mySlider.onValueChanged.AddListener(delegate {ValueChangeCheck(); });
      }
     
      // Invoked when the value of the slider changes.
      public void ValueChangeCheck()
      {
          handle.color = Color.HSVToRGB(mySlider.value, 1, 1);
      }
    
Comment
Add comment · Show 4 · 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 Bunny83 · Apr 27, 2017 at 09:56 AM 2
Share

That texture should be easy to create procedurally. Actually a 4x1 texture should be enough. Just use the 4 colors: red, green, blue, red:

 var hueTex = new Texture2D(4,1);
 hueTex.SetColors(new Color[]{Color.red, color.green, Color.blue, Color.red});
 hueTex.Apply();
avatar image Ellie97 Bunny83 · Nov 07, 2018 at 04:56 PM 0
Share

Those colours do not create the gradient that OP has. Use

hueTex.SetPixels(new Color[] { Color.red, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.red });

avatar image Hellium Ellie97 · Nov 07, 2018 at 05:01 PM 2
Share
 Color[] colors = new Color[]
 {
      new Color(1, 0, 0),
      new Color(1, 1, 0),
      new Color(0, 1, 0),
      new Color(0, 1, 1),
      new Color(0, 0, 1),
      new Color(1, 0, 1),
      new Color(1, 0, 0)
 } ;
 var hueTex = new Texture2D(colors.Length,1);
 hueTex.SetPixels( colors );
 hueTex.Apply();
 hueImage.sprite = Sprite.Create( hueTex, new Rect( Vector2.zero, new Vector2( colors.Length, 1 ) ), Vector2.one * 0.5f );


Show more comments
avatar image
0

Answer by toddisarockstar · Nov 08, 2018 at 11:44 PM

you can display your picture of the slider with a UI. then use texture2d.getpixel from your calculated mouse position to get your color!!!! here is a quick example:

     // drag your picture of the slider into the next variable
     // the import settings of your image must be set for read/write enabled 
     // under import settings of the image in the inspector select advanced
     // then tick read/write enabled
     public Texture2D myslider;
     public Texture2D picked ;
     public int x = 20;
     public int y = 20;
     public int h = 50;
     public int w = 400;
     public Color select = Color.black;
         
     void Start(){
         picked = new Texture2D(1,1);
     }
     void Update(){
 
         if (Input.GetMouseButton(0)) {
             float MX = Input.mousePosition.x;        
             float MY = Screen.height - Input.mousePosition.y;
 
             if(MX>x&&MX<x+w){
             if(MY>y&&MY<y+h){
                     float ratiox = (float)myslider.width/(float)w;
                     float ratioy = (float)myslider.height/(float)h;
                     int getx = Mathf.FloorToInt((MX-x)*ratiox);
                     int gety = Mathf.FloorToInt((MY-y)*ratioy);
                     gety = myslider.height-gety;
                     
                     select = myslider.GetPixel(getx,gety);
                     picked.SetPixel(0,0,select);
                     picked.Apply();}}}}
     void OnGUI(){
         GUI.DrawTexture (new Rect (x, y, w, h), myslider);
         GUI.DrawTexture (new Rect (200,200,50,50), picked);
 
     }
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

10 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

Related Questions

How to get height of filled image ? 1 Answer

Why is UI Slider not working while Animator is enabled? 1 Answer

How I can change the sprite with resources.Load? 1 Answer

UI Slider value gets stucked in loop and Doesn't increase further 0 Answers

UI slider background image 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