- Home /
Create my own non-compound GUI Control? (GUIElement?)
I would like to create a control in my GUI that allows the user to pick a color. What I have in mind is a rectangle full of colors (dynamically generated, not a bitmap, to ensure that it looks good on any screen).
I looked through the GUI documentation but didn't see anything that would allow me to do anything along these lines. I would expect that there's some kind of base GUI Widget or Box or View class that all of the other GUI controls inherit from, and I was figuring that I could somehow write my own decedent of that class which, when some display or render method is called on it, I could run through a loop of x and y positions and assign the colors to individual pixels, and then when a mouse is clicked, it could adjust a variable, or something like that.
Actually, just before I hit the "Ask Your Question" button I looked over the classes list one last time and noticed the classes GUIElement and GUIUtility... these both look like they might be relevant, but they seem like they're missing crucial parts to me. IE, there's no onDraw
method that I can see, or anything like that.
I'd love if somebody could share an example of a custom non-compound GUI Control, or at least point out the essential methods that I need to use in order to pick the color for each individual pixel and draw it to the screen, as well as detect when the widget is clicked.
The code for the standard colour picker should be out there somewhere. You could s$$anonymous$$l that.
@Bored$$anonymous$$ormon - I couldn't find anyone doing quite what I want, but I found that I can create my own texture pixel-by-pixel with Texture2D's setPixel() or setPixels() method. Then GaborD over in this thread (http://forum.unity3d.com/threads/making-a-color-wheel-color-picker.32760/) has a method for turning a Texture2D into color picker by applying it to a RepeatButton and tracking where in the button the mouse is when it's down. I'll try that, then if I can get it working, I'll post it as an answer here.
Answer by ArtOfWarfare · Nov 08, 2014 at 09:12 PM
Okay, this (Boo) worked pretty well:
# Helper function for generating colors
def channelValue(x as double, y as double):
return (2.0 * x) - (x * y) + y - 0.9
# Another helper function for generating colors
def colorFor(x as double, y as double):
return Color(x + y, channelValue(x, y), channelValue(y, x))
# Function that utilizes the above two helpers to make a texture with the colors.
def ColorPicker(maxX as int, maxY as int):
ret = Texture2D(maxX, maxY)
colors = array(Color, maxX * maxY)
for x in range(maxX):
for y in range(maxY):
colors[x * maxX + y] = colorFor(1.0 * x / maxX, 1.0 * y / maxY)
ret.SetPixels(colors)
ret.Apply()
return ret
pickerTexture as Texture2D
def Start():
pickerTexture = ColorPicker(100, 100)
def OnGUI():
if GUI.RepeatButton(Rect(25, 25, 100, 100), colorPicker, GUIStyle.none):
position = Event.current.mousePosition
x = position.x - 25
y = 125 - position.y # For some reason the texture's Y values needed to be flipped
print(pickerTexture.GetPixel(x, y))
This should nicely demonstrate how to make a unique GUI Control that neither looks nor behaves like a normal button, which is what I wanted.