x


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!

more ▼

asked Feb 28 '11 at 01:28 AM

Keavon gravatar image

Keavon
358 30 34 44

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

Feb 28 '11 at 02:19 AM Bunny83

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

Oct 30 '11 at 11:39 AM syclamoth

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

Apr 25 '12 at 08:17 PM ManojPrince

@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...".

Apr 25 '12 at 08:24 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;)

more ▼

answered Feb 28 '11 at 02:36 AM

Bunny83 gravatar image

Bunny83
45k 11 48 206

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

Oct 29 '11 at 11:55 AM ina

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.

Oct 29 '11 at 01:03 PM Bunny83

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?

Oct 30 '11 at 07:30 AM ina

umm, yes:

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

Oct 30 '11 at 11:35 AM Bunny83

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

Oct 30 '11 at 11:38 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

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).

more ▼

answered Nov 14 '11 at 12:14 PM

andresp gravatar image

andresp
183 18 21 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3673
x506
x10

asked: Feb 28 '11 at 01:28 AM

Seen: 2635 times

Last Updated: Apr 25 '12 at 08:24 PM