- Home /
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!
Did you even read the corresponding text to this script snippet?
This is why you should never just copy-paste something without understanding what it does.
$$anonymous$$e applied this to my application and it gets RED color only... Please Help $$anonymous$$e Iwas Stucked Here...
@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...".
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;)
how do you adjust the offset based on texture? where does the 9 and 41 etc come from?
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.
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?
umm, yes:
I needed a buttonheight of 55 for my texheight of 41 for instance
They're basically magic numbers, you adjust them for your specific application!
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).
Your answer
Follow this Question
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