GUI.Button with double functionality

  1. I want to have a GUI button on my top right.

  2. When I mouseClick it ONCE, I want it to go to MainMenu screen (still inside the app)

  3. When I mouseClick and hold down the LMB for 2 seconds, I want the game to load the login screen, which is outside the app itself

I’ve been trying to work this out for the whole damn day yesterday and the day before, so I’m pretty mad and desperate. Pls help me out with this.

According to the docs, GUi.Button is a single press button, so it returns true whenever it is pressed (I haven’t tried it, but it seems it only returns true the frame the mouse clicks down on it). I suggest you use some kind of timer and control it with the OnMouseUp() function.

Try this:

#pragma strict

public var waittime : float = 2.0;
private var timestamp : float = 0.0;

var rect : Rect = Rect(100,100,200,50);

function OnGUI() {
	var e : Event = Event.current;
	
	if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition)) {
		timestamp = Time.time;
	}
	
	if (GUI.Button(rect, "Some Button")) {
		if (Time.time - timestamp > waittime) {
		 	Debug.Log("Clicked and held");
		}
		else {
			Debug.Log("Just clicked");
		}
	}
}