Search Unity

Button: OnClick, OnRelease,

Discussion in 'Immediate Mode GUI (IMGUI)' started by AaronC, Aug 3, 2008.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,544
    Hi,

    Imagine you click on a button, and an Image in a second popup window fades in. Then you release and it fades out again.

    I can do it by click once to fade in, click once to fade out, but there seems to be no OnMouseDown and OnMouseUp functionality for Unity GUI? Correct?

    Cheers
    AaronC
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
  3. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,544
    Yeah this is not helpful documentation at all. In fact documentation like this just makes me feel stupider.

    Found one javascript example on the forum by Nicholas, but I cant seem to plug it into a button in anyway
    Code (csharp):
    1. function OnGUI () {
    2.     GUI.depth = 2;
    3.     if (Event.current.type == EventType.MouseDown) {
    4.         print ("Got a click on my playfield");
    5.     }
    6. }
    So where to from here...I see Yoggy's got a complicated version in his graph editor scripts too.

    Can we please have stronger documentation?

    Thanks

    AC
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,544
    Bingo. Unfortunately converting this to javascript would be a career choice.

    appreciated though. I had found all the C# examples, but sadly, Nich's was the only javascript snippet this side of Saturn.

    So I'm another day older, been staring into unitron all day, with nothing to show for it. Why cant we just have:
    Code (csharp):
    1.  
    2. function OnGui(){
    3.  
    4. OnMouseDown(){
    5.  
    6. //OMG thats so simple!
    7.  
    8. }
    9.  
    10.  



    Unity is easy to use but it could also be infinitely easier

    :roll: AC
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    Don't worry Targos. I'm interested in trying out this code, I prefer the look of JS, and I know both C# and Javascript. It's 3 in the morning where I am, and I have to get up at 7:30. I'm going to take a nap, but after I get to work, I'll convert it for the both of us. (My job is going to consist of babysitting a lot of CDs that are ripping today, so I'm not just slacking off.)
     
  7. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,544
    That would be wicked Jessy.

    OK almost there, it might be beneficial to ask for help rather than whine and stare at Unitron. To explain what Im trying to do here, I created a complete example using the older GUI Elements. I timed myself, it took 12 minutes.

    That is called "OldSchool" note it does some things specifically:

    OnMouseDown fades in
    OnMouseUp fades out again

    I love the simplicity of using animations for this functionality, which UnityGUI doesnt support.

    theres also two scenes that use the new GUI method. One of them fades nicely, mostly thanks to Erics code, but it needs two seperate clicks, rather than mouseDown/mouseUp

    The other enables when clicked, disables when unclicked, but does not exhibit the fade feature.

    So if I could merge the two Newschool ones together to mimic the old school one, we'd be laughing. They seem like trivial differences, but are important in context.

    Luckily I've got a period of art production ahead. Looking forward to that.

    AaronC
     

    Attached Files:

  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    Well, I converted it to JS - not much work, really. There's still a bunch of junk in there that I don't understand, though. For starters, I would never have thought to use "GetHashCode()", because I've never heard of that before. Maybe the Unity documentation is lacking because it would be such a complicated undertaking to try to explain it? Still, other than that function, it's all Unity-specific, so I think there ought to be significantly more explaining done.

    http://forum.unity3d.com/viewtopic.php?p=88783#88783
     
  9. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    GetHashCode is just used to generate a magic number for the control and has to always be the same for a given control.

    This is GUI.Button as you use in Unity, the HoverButton just builds on this.
    Code (csharp):
    1. static int buttonHash = "Button".GetHashCode ();
    2. public static bool Button (Rect position, GUIContent content, GUIStyle style) {
    3.         int id = GUIUtility.GetControlID (buttonHash, FocusType.Native);
    4.         switch (Event.current.GetTypeForControl (id)) {
    5.         case EventType.MouseDown:
    6.             // If the mouse is inside the button, we say that we're the hot control
    7.             if (position.Contains (Event.current.mousePosition)) {
    8.                 GUIUtility.hotControl = id;
    9.                 Event.current.Use ();
    10.             }
    11.             return false;
    12.         case EventType.MouseUp:
    13.             if (GUIUtility.hotControl == id) {
    14.                 GUIUtility.hotControl = 0;
    15.  
    16.                 // If we got the mousedown, the mouseup is ours as well
    17.                 // (no matter if the click was in the button or not)
    18.                 Event.current.Use ();
    19.  
    20.                 // But we only return true if the button was actually clicked
    21.                 return position.Contains (Event.current.mousePosition);
    22.             }
    23.             return false;
    24.         case EventType.MouseDrag:
    25.             if (GUIUtility.hotControl == id)
    26.                 Event.current.Use ();
    27.             break;
    28.         case EventType.Repaint:
    29.             style.Draw (position, content, id);
    30.             return false;
    31.         }
    32.         return false;
    33.     }
     
  10. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,544
    Thanks- both of you, much appreciated!

    AaronC
     
  11. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,544
    After playing with this, I still think the GUI interface is way too complicated for implementing an

    Code (csharp):
    1. function OnMouseDown(){
    2.  
    3. DoStuff();
    4.  
    5. }
    event. Will it always be as complicated as Shauns/Jessy's versions or will these legacy easy-to-use-things make it into UnityGUI one day?

    I say this because even after Jessy's conversion I still couldnt succesfully call a oneShot function from a mouse down event, and I certainly have no idea whats going on in the script, with native events and so on. I've been coding in Unity for two years now, and to be struggling with a mouseDown seems nuts.

    Sorry to the developers who are waiting for me to get onto their projects, this is taking a heck of a lot longer than I'd planned.

    UT guys is this going to get easier?

    Cheers

    AaronC
     
  12. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I can't promise anything in particular around this issue, but I can definitely say that we will of course continue to move our GUI system forward and improve its functionality and usability as we do with all features.
     
  13. Erfan_Time

    Erfan_Time

    Joined:
    Jun 22, 2018
    Posts:
    8