Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem Windows events

Discussion in 'Scripting' started by Dm1try, Jul 25, 2011.

  1. Dm1try

    Dm1try

    Joined:
    Jul 25, 2011
    Posts:
    8
    I use Unity buttons to create menu for my game.

    The game is made for Laser shooting - shoot enemies that are drawn on projector using laser gun.
    Laser gun is something like that:
    http://www.pyramydair.com/p/gamo-p-23-pellet-gun-with-laser.shtml

    Laser gun events are converted to mouse events by third-party profuction software.
    Then I shoot using laser gun this software create two mouse events (OnMouseDown, OnMouseUp) during one game frame.

    I catch them by using code:

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     if (Input.GetMouseButtonDown(0))
    5.     {
    6.         Debug.Log( Time.frameCount + " MouseDown" );
    7.     }
    8.  
    9.     if (Input.GetMouseButtonUp(0))
    10.     {
    11.         Debug.Log( Time.frameCount + " MouseUp" );
    12.     }
    13. }
    And that's Ok, I catch them every time I shoot.

    But there is the problem with function GUI.Button().
    It works every even time I shoot.
    I shoot first time (GUI.Button doesn't work)
    I shoot second time (GUI.Button works!)
    I shoot third time (GUI.Button doesn't work)
    I shoot fourth time (GUI.Button works!)
    And so on ...

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
    5.     {
    6.         Debug.Log( Time.frameCount + " GUI.Button" );
    7.     }
    8. }
    The second thing I've tried was to log mouse position then I shoot

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     if (Input.GetMouseButtonDown(0))
    5.     {
    6.         Debug.Log( Time.frameCount + " | " + Input.mousePosition + " MouseDown" );
    7.     }
    8.  
    9.     if (Input.GetMouseButtonUp(0))
    10.     {
    11.         Debug.Log( Time.frameCount + " | " + Input.mousePosition + "MouseUp" );
    12.     }
    13. }
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
    5.     {
    6.         Debug.Log( Time.frameCount + " | " + Input.mousePosition + " GUI.Button" );
    7.     }
    8. }

    So I've shot again. This is the log:

    // first shoot
    61 | (17.0, 23.0, 0.0) MouseDown
    61 | (17.0, 23.0, 0.0) MouseUp

    // second shoot
    121 | (32.0, 28.0, 0.0) GUI.Button
    121 | (32.0, 28.0, 0.0) MouseDown
    121 | (32.0, 28.0, 0.0) MouseUp

    // third shoot
    157 | (19.0, 22.0, 0.0) MouseDown
    157 | (19.0, 22.0, 0.0) MouseUp

    // fourth shoot
    183 | (15.0, 31.0, 0.0) GUI.Button
    183 | (15.0, 31.0, 0.0) MouseDown
    183 | (15.0, 31.0, 0.0) MouseUp

    So, why mouse events (from laser gun) are detected by Input.GetMouseButtonDown and Input.GetMouseButtonUp, but sometimes couldn't be detected by GUI.Button() ???
     
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Please do not double post. Just bump your previous thread.

    It's a bit confusing to get what you are trying to do. Why do you need separate codes to detect mouse press?

    In your update function you use Get key down and get key up which will turn to true/false only when player release or press mouse however in your OnGUI, the button is only true for only one frame but your keydown is still true. So i guess you would need to use a repeat button. http://unity3d.com/support/documentation/ScriptReference/GUI.RepeatButton.html
     
  3. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    From dealing with alot of event sending/emulation its probably a issue with your third-party production software. It sounds like your software is not moving the mouse position over the rect in a way that unity picks up on it before the press. ushually with emulating mouse input you'll need to set the absolute position one frame behind the button press. Not sure what your working on but i think your going the wrong direction for inter-process communication? Unity should expose everything to make a named pipe, or even registry checking ( ie if you watch a registry value for changes by a external process act on them )