Im looking for some help on handling events on GUI.Button or GUI.Repeat Button, but unfortunately i didnt find any source that states the use of any event handler in OnGUI()
What i am trying is to call a function1 when a button is pressed , function2 when it is hovered function3 if it is released
To call function1 when button is pressed , im using :-
function OnGUI()
{
if ( GUI.RepeatButton(Rect(10,10,50,50),“PressMe”) )
{
function1();
}
else
{
function3(); // I want the function 3 to be called when button is released but it is being called every frame even if i have not pressed the button for a single time, and i know it will so bcz ongui will be check the else block evey frame
}
}
if anybody has done similar work, then plz help me with some code or logic
you could keep track of on mouse down and up after pressing the button and do your functions respectively based on that.
the simpler would be to use the monobehavior.onmouse events along side your GUIElements.
for example this is how you could do an on MouseOver
function MouseOverTest(object : GUIElement, MousePos : Vector2) : boolean {
var rect = object.GetScreenRect(camera);
if(rect.Contains(MousePos)){
return true;
}
else{
return false;
}
}
Unfortunately the built-in gui system doesn’t really have any proper event handling – which is why I’m in the process of writing up my own gui management system for enhanced functionality (as more than a few people are).
In regard to the iPod/iPhone, look at the Touch struct in the documentation. It has all the equivalents of the mouse info. I’ve never used it since I don’t have Unity iPhone, though I’m sure I’ll be trying it out at some point.
The most common way you’d do this is to keep flags somewhere, for things like “isPressed”, “wasPressed”, “isHovering” and such, based on your needs. In regard to OnGUI you just call a function or change the flag directly, if the GUI.Button (or other) function returns true. Your code would look something like this (keep in mind none of this is tested, and may need some tweaking):
var isPressed: boolean;
var wasPressed: boolean;
function ButtonTest() // called in Update?
{
if (isPressed wasPressed)
// call a function to handle repeat press
if (!isPressed wasPressed)
// call a function for a release
// do whatever else during this test
wasPressed = isPressed; // consider this frame to be the previous frame
isPressed = false; // during OnGUI we'll set it to true, so it's essentially still true for a continuous click (or touch)
}
function OnGUI()
{
if (....) // insert GUI.Button statement there
{
isPressed = true;
}
// more buttons?
}
Well, thanks Demi . Im too trying this kind of idea to manually code some gui events like script that can work well for this. But it must be great if unity has had provided us such event structure for gui elements. Its good to know that hardworking people like you are doing this thing which is different in itself and would definitely help community members.
Im too trying this, if i end up with a helpful, sharable and easy to understand code then i’ll up it on here as soon as i complete.
great man!
if anybody in the community reading this and have and idea of this then plz, do reply the post.