So, I have a scroll view and simply want to get mouse clicks for the items in the list but it’s painful.
Is there some easy way to do this?
At the moment I’m using Rect.Contains that:
Starts with the size of the contents…
Is offset by the view bounds…
Is offset by the content bounds…
Is offset by the scroll position…
Is offset by the margin of the style holding the content…
Is offset by the padding of the style holding the content…
Is then flipped to account for the fact that the input coords and gui coords have inverted y.
This is a lot of processing to figure out how the mouse is interacting with the control. I can’t use the button, toggle or toolbar styles as behavior overrides because they only report changed on mouse-up on the control and so can’t be used for drag, etc.
Problem with Unity GUI is that it’s unlike other popular GUI frameworks and it takes time for people to learn to use it.
For what you do you should probably use GUILayout stuff.
E.g.
GUILayout.BeginScrollView
for (i = 0; i < itemCount; i++)
if (GUILayout.Button("This is item " + i, “Label”))
Debug.Log("Item " + i + " was clicked);
GUILayout.EndScrollView
For more complicated stuff you can use
GUILayout.BeginScrollView
Rect r = GUILayoutUtility.GetRect(myContent, myStyle);
GUI.Label(r, myContent. myStyle);
if (r.Contains(Event.current.mousePosition))
ItemWasClicked();
GUILayout.EndScrollView
Your second suggestion seems like it will work for me, thanks. It isn’t obvious even after reading the doc what this does or how one would use it, so without your post I don’t think I could have figured it out on my own.
Layouted version of GUI uses GetRect for all controls.
You can GetRect with exact dimensions you want (width height)
or you can e.g. specify that you want a rect that should stretch as widely as possible and stretch to height no more than 50 pixels. That is all specified in GUI style. That is sort of CSS.
This seems very cool and makes UnityGUI very close to truly useable for me. I did get my code working based on your post, thanks. I have used UnityGUI for some of my UI before, but in some cases I just need to resort to hand-rolled code using polygons and textures. I was looking forward to the sprite features of U3 that got cut (whatever they were).
Now, if only the UnityGUI shader would let me specify overbright colors beyond white I’d be truly happy! Being able to quickly highlight something by just setting the color to Color.white * 1.5f is soooo useful.
that is now done through GUI styles as well. You can specify e.g. buttons hover texture and colors. it is more complicated than color = something, but usually that’s not what you want. You’d want e.g. buttons borders to get brighter, but not all buttons texture. imho.
No, it’s exactly what I want, haha. To make scalable, interesting, fluid UI is much more difficult when I need to make various gui textures for the different states in different resolutions.
Many times, just using overbrightening, color and alpha do a great job of making the UI feel more alive without needing to make lots of assets.
As far as I know though, you can’t specify a color element more than 1.0 and have it do anything?
Normally for most renderer colors you can use a shader will use colors like 2.0, 2.0, 2.0, 1.0 to make something twice as bright (like mouseover a model in the scene).
Are you saying you should be able to do this in U3?
Color is by definition [0;1], i don’t know if you can have it more than 1 anywhere, and what should that do. I did not hear anything about that in Unity. Though i’m not a graphics programmer so don’t take my word for it.
You can use it almost anywhere and it makes it super easy to do highlighting and so forth because out of the box Unity keeps the ambient light low and so it “just works” because there’s room left to brighten the objects.
Color is simply a value sent to the shader during runtime that is multiplied together to get the final pixel color. That’s how additive overexposed blending is realized.
Granted it depends upon how the final render buffer is setup and the pipeline of shaders, lights, etc. If you setup Unity to be ambient light of white and make your textures all bright, and your vertex colors all white, then in the end there’s no room left in the color to go beyond 1.0.
So no worries, easy to make it work for me by simply brightening my textures and then darkening the GUI.color so that it looks “normal” at less than white. Then use white as the highlight look.