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