Clickable GUI.Window

Alright so what i’m trying to do is make GUI.Window components that do something when clicked, but i have no idea how to make it work.

Here’s what i got so far:

GUI.WindowFunction winFunction;
Rect r = new Rect(30, 30, 300, 300);

void Start(){
    winFunction = windowFunction;
}

void OnGUI(){
    GUI.Window(0, r, winFunction, "test");
}

void windowFunction(int windowID){
    //One of the things i've tried, doesn't work for some reason.
    if(Input.GetMouseButtonDown(0) && r.Contains(Input.mousePosition))
    {
        print("doesnt work");
    }
}

Any ideas how to make it work? besides perhaps using reflection.

The input class is ment for gameinput and shouldn’t be used in OnGUI. Use the Event class in OnGUI:

void windowFunction(int windowID)
{
    Event e = Event.current;
    if(e.type = EventType.MouseDown && e.button == 0 && r.Contains(e.mousePosition))
    {
        print("should work");
    }
}

OnGUI is called for many different events. The GUI functions like Button or Label handle the event internally. When you do anything “manually” in OnGUI, keep in mind to pick the right event or you would execute your code for all events.

edit
Keep in mind that all events processed inside a window callback have mouse coordinates relative to the window origin.