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 Keavon · Feb 28, 2011 at 01:28 AM · guicolorcolorpicker

I am trying to make this color picker script work

Hello. I found some code on the Unity forums, from the post from GaborD here

The code is:

if (GUI.RepeatButton (Rect (xxx,yyy,215,55), colorPicker)) { pickpos = Event.current.mousePosition; aaa = pickpos.x-xxx-9; bbb = pickpos.y-yyy-5; col = colorPicker.GetPixel(aaa,41-bbb);

         model.materials[matnum].SetColor("_Color",col);
     }

I have gotten these errors:

Unknown identifier: 'xxx'
I added var xxx; to the top, then got

Unknown identifier: 'yyy'
I added var yyy; to the top, then got

Unknown identifier: 'colorPicker'
I added var colorPicker; to the top, then got

Unknown identifier: 'model'
I added var model; to the top, then got

Unknown identifier: 'colorPicker'
I added var colorPicker; to the top, then got

model.materials[matnum].SetColor("_Color",col);
I added var model : Transform; to the top, then got

So I am assuming this is where you drop the model or prefab I want to control the color of. But how do I do this right? Am I doing any of this right? Should I assign anything to those variables, xxx, yyy, and colorPicker? Thank you!

Comment
Add comment · Show 4
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 · Feb 28, 2011 at 02:19 AM 0
Share

Did you even read the corresponding text to this script snippet?

avatar image syclamoth · Oct 30, 2011 at 11:39 AM 1
Share

This is why you should never just copy-paste something without understanding what it does.

avatar image ManojPrince · Apr 25, 2012 at 08:17 PM -1
Share

$$anonymous$$e applied this to my application and it gets RED color only... Please Help $$anonymous$$e Iwas Stucked Here...

avatar image Bunny83 · Apr 25, 2012 at 08:24 PM 0
Share

@manojprince: Don't post such requests as an Answer. If you have a question ask a Question and be a bit more specific as just "I've tried something and it doesn't work...".

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Feb 28, 2011 at 02:36 AM

Here's the script with the correct variable declarations.

// position on x axis
var xxx : float = 10;
// position on y axis
var yyy : float = 10;
// assign your colorpicker texture to this variable
var colorPicker : Texture2D;
// assign your model to this variable
var model : Renderer;
// The material index of the material you want to pick a color for.
var matnum : int = 0;
function OnGUI() {    
    if (GUI.RepeatButton (Rect (xxx,yyy,215,55), colorPicker)) {
        var pickpos : Vector2 = Event.current.mousePosition;
        var pixelPosX : int = pickpos.x-xxx-9;
        var pixelPosY : int = 41-(pickpos.y-yyy-5);
        var col : Color = colorPicker.GetPixel(pixelPosX,pixelPosY);
        model.materials[matnum].SetColor("_Color",col);
    }
}

And as GaborD mentioned you may have to adjust the offset values depending on your texture: (215; 55; -9;-5;41;)

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 ina · Oct 29, 2011 at 11:55 AM 0
Share

how do you adjust the offset based on texture? where does the 9 and 41 etc come from?

avatar image Bunny83 · Oct 29, 2011 at 01:03 PM 1
Share

Did you even read the forum article?

Almost everything is explained there. They are some adjustments due to the used GUISkin(margin / padding).

quote from "GaborD":

There is some space between the button edge and the texture edge plus my texture had a thin border too, so those added up to the values that worked. I just tried around until it was pixelperfect.

avatar image ina · Oct 30, 2011 at 07:30 AM 0
Share

ok, just to be sure, his texture had a 9 offset on the x, and a 5 offset on the y? and 41 was the texture vertical size?

avatar image Bunny83 · Oct 30, 2011 at 11:35 AM 0
Share

umm, yes:

I needed a buttonheight of 55 for my texheight of 41 for instance

avatar image syclamoth · Oct 30, 2011 at 11:38 AM 0
Share

They're basically magic numbers, you adjust them for your specific application!

avatar image
1

Answer by andresp · Nov 14, 2011 at 12:14 PM

there is no need to use "magic numbers" and trial and error here if you use GetPixelBilinear function instead of GetPixel:

     if (GUI.RepeatButton(new Rect(beginX, beginY, dimX, dimY), colorSpectrumTexture, GUI.skin.GetStyle("ColorPickerStyle"))) {
         Vector2 pickpos = Event.current.mousePosition;
         Vector2 coords = new Vector2((pickpos.x - beginX) / dimX, -(pickpos.y - beginY) / dimY);
         Color color = colorSpectrum.GetPixelBilinear(coords.x, coords.y);
     }

Note: Make sure your "ColorPickerStyle" has no padding, margin and border; has UpperLeft alignment; and dimX and dimY are proportional to the size of your texture, i.e. it fills the entire Rect - for some reason I think Unity can't correctly stretch a background image of a GUI controller, like a RepeatButton, even with StretchWidth and StretchHeight enabled).

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

Help with coloring a button? 1 Answer

A color select menu? 1 Answer

Multicolor GUI Labels/text? 4 Answers

guiTexture color half alpha White? 1 Answer

Help,How to fix this? 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