hey , I just creating a double click class which allows me to make anything clickable…
the code is written below .
this is done just by checking i a mouse down event is triggered twice in under 0.25 seconds.
then i realised that while i am not able to “click” anything… it is not really clicking the way buttons handle clicks .
I am checking for a mouse up event over the GUI element , setting a float to be realtimeSinceStartup then checking for a mouse down event , check if realtimeSinceStartup - the previously saved realtimeSinceStartup is less than 0.25 thus completing the double click.
but no buttons in unities gui even registers a mouse up event .
this code does check for double clicks on GUI elements… but not on GUI elements that already registers or read a MouseDown event somewhere in the unity API
public class Click
{
static float time2;
static bool result ;
public static bool DoubleClick(Rect rect)
{
bool result = false;
float time = Time.realtimeSinceStartup / 10.0f;
if (rect.Contains(Event.current.mousePosition))
{
if (Event.current.rawType == EventType.MouseUp)
{
time2 = time;
}
if (Event.current.rawType == EventType.MouseDown && time - time2 < 0.025f)
{
time2 = 0;
result = true;
}
}
return result;
}
}
Implementation example
private Rect someRect =(new Rect(100,100,100,50));
private Rect someOtherRect = new Rect(250,100,100,50)
private Texture2D = (Texture2D) Resources.Load("someimage",typeof(Texture2D));
void OnGUI()
{
GUI.Button(someRect,"double click me") ;
if(Click.DoubleClick(someRect)) Debug.Log("double clicked")
GUI.DrawTexture(someOtherRect ,image);
if(Click.DoubleClick(someOtherRect )) Debug.Log("texture clicked")
}