- Home /
GUI.Button with double functionality
I want to have a GUI button on my top right.
When I mouseClick it ONCE, I want it to go to MainMenu screen (still inside the app)
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.
Answer by Andres-Fernandez · Aug 07, 2014 at 06:43 AM
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.
I'm a newb, bro ... I really need somebody to code it out for me :(...I was trying to solve it yesterday but I just can't anymore.
he chould place thi on a empty game object and give it a dont destroy on load and it will keep the object http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Answer by robertbu · Aug 07, 2014 at 07:06 AM
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");
}
}
}
this is UnityScript, right? :) I'm learning C# but I think I will be able to "translate" this into C#. I'm gonna go learn something about Events now and then I'll get back to you and accept your answer if it solves my problem. Until then...
A C# translation:
using UnityEngine;
public class Bug25a : $$anonymous$$onoBehaviour
{
public float waittime = 2.0f;
private float timestamp = 0.0f;
private Rect rect = new Rect(100,100,200,50);
void OnGUI() {
Event e = Event.current;
if (e.type == EventType.$$anonymous$$ouseDown && 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");
}
}
}
}
Your answer
Follow this Question
Related Questions
button with content in multiple colors 3 Answers
(Unity 4.6) Button animations 1 Answer
Simulate IMGUI button press 1 Answer
Unity GUI Nested If statements causing GUI Buttons not to render 1 Answer
GUI.Button doesn't show texture 0 Answers