- Home /
RepeatButton AND Button at the same time
What I need to be able to do is to click-drag using a repeat button, but also just click to initiate movement. Think of the status bar on android phones (the bar at the very top). You drag it down to move it. But i also want to just tap it to have it open/drag down on its own. I have the dragging code all working fine, and the clicking code working fine, I just don't know a way to have both be active at the same time. I believe a Button Up function would fix this, because if you're dragging you aren't Up, but if you click you are. I haven't found anything about GUI.Button Up and even if I did, there's still the problem of only triggering the button that's on "top", the button under it still wouldn't trigger an up function. Any help is appreciated. If there is a snippet of code that just tells Unity to push all buttons the mouse is on or to ignore button priority that would rock. Thanks for the help!
The reason I'm using a repeat button is so that you can't just drag anywhere on the screen to use it. You have to be on the graphic to pull it, and since a GUI.Button only triggers when "clicked", and dragging keeps the mouse held down, I can't just lump the two into one button. Obviously if there is alternative to using RepeatButton this would clear up my issue, and would be appreciated.
using UnityEngine;
using System.Collections;
public class Drawer : MonoBehaviour
{
public GUISkin skin;
private float drawerPosX = 1232;
private float drawerBGPosX = 1280;
private float drawerDragZone = 100;
private float mousePosX;
private bool drawerRight = false;
private bool drawerDockRight = false;
private bool drawerDockLeft = false;
public bool drawerDrag = false;
Vector2 StartPos;
int SwipeID = -1;
float minMovement = 20.0f;
void Start ()
{
}
void OnGUI()
{
GUI.skin = skin;
GUI.Box(new Rect(drawerPosX, 0, 48, 800), "", "Other");
GUI.Box(new Rect(drawerBGPosX, 0, 1280, 800), "", "DrawerBG");
if(GUI.RepeatButton(new Rect((drawerPosX - 50), 0, drawerDragZone, 800), "", "ArrowUPHead"))
{
drawerDrag = true;
}
if(GUI.Button(new Rect((drawerPosX - 50), 0, drawerDragZone, 800), "", "ArrowUPHead"))
{
if(drawerPosX <= 640)
{
drawerDockRight = true;
drawerDockLeft = false;
}
else if(drawerPosX >= 1232)
{
drawerDockLeft = true;
drawerDockRight = false;
}
}
}
void Update ()
{
if(drawerPosX <= 640)
{
if(Input.GetMouseButtonUp(0))
{
drawerRight = true;
}
if(drawerRight == true)
{
drawerPosX += 20;
drawerBGPosX += 20;
if(drawerPosX >= 640)
{
drawerPosX = 640;
drawerBGPosX = 688;
}
}
else
{
}
}
if(drawerDrag == true)
{
mousePosX = Input.mousePosition.x;
drawerPosX = mousePosX;
drawerBGPosX = mousePosX + 48;
}
if(Input.GetMouseButtonUp(0) && drawerDrag == true)
{
drawerDrag = false;
}
if(drawerDockRight == true)
{
drawerPosX += 20;
drawerBGPosX += 20;
if(drawerPosX >= 1232)
{
drawerPosX = 1232;
drawerBGPosX = 1280;
}
}
if(drawerDockLeft == true)
{
drawerPosX -= 20;
drawerBGPosX -= 20;
if(drawerPosX <= 640)
{
drawerPosX = 640;
drawerBGPosX = 688;
}
}
}
}
Answer by robertbu · Mar 08, 2013 at 07:39 AM
GUI.Button() calls execute on the mouse up, so you can set a timer when the mouse goes down and check the duration to see if it is a click or something longer. Simple test script:
using UnityEngine;
using System.Collections;
public class GUIClick : MonoBehaviour {
float clickThreshold = 0.25f;
float clickTime = 0.0f;
void Update() {
if (Input.GetMouseButtonDown(0))
clickTime = Time.time;
}
void OnGUI() {
if (GUI.Button(new Rect(200,200,200,100),"Click Here")) {
if (clickTime + clickThreshold >= Time.time)
Debug.Log ("I was clicked");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613094024im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI Repeatbutton up? 1 Answer
Unity UI Button not working 1 Answer
OnMouseDown() Doesn't work 17 Answers
Stop clicking through a GUI window 2 Answers
Detect Swipe and GUI Button Press 1 Answer