Unity Community


Results 1 to 15 of 15

  1. Location
    Denmark - Copenhagen
    Posts
    49

    Mouse input possible from different tab

    Hi, I hope you guy's and gals can help me, please bear with me I'm new.

    The problem:
    The web player listens to the mouse even if the browser is on a different tab.

    Ocurs:
    Windows - FireFox
    Windows - IE 8
    Windows - Chrome

    Reproduce problem:
    1. Open the tropical paradise demo, let it load and click to explore.
    2. Open a new tab.
    3. Return to the demo tab.
    4. Move your mouse to the tab you opened in step 2. now note the view direction of the camera.
    5. Click the tab, now move you mouse back to the demo tab, click it.
    6. Compare current view to view from step 4.

    comment: even thou you where on a different tab, the tropical paradise demo still registered your mouse movement and reacted. This also happens with scroll and even clicks.

    I have attached a project that hides the cursor(so it is possible to use custom mouse cursors as described in the documentation here). But the side effect is that the web player hides the cursor on all tabs! I have also attached an already buildt web player, open it and some other tabs and watch your mouse dissapear.
    Attached Files


  2. Location
    Denmark - Copenhagen
    Posts
    49

    WebPlayer massive bug!

    If you use a web player, You are affected by this! I have now reported a bug, so I hope that helps.

    I have made a new project, that logs mouse clicks.
    Open the web player, open a new tab, click around the empty tab and now go back to the web player, there will be a masssive log :/

    does this look okay?
    http://intra.unity3d.com/fogbugz/def...71462_3jg9tbng
    Code:  
    1. Unity web player mouse input ignores that user is on different tab!
    2.  
    3. Dear Sam
    4. I have a huge problem, if this bug report is in any way lacking please contact me. We are close to launching and it is impossible to have the web player open and use any other tab.
    5.  
    6. The problem:
    7. We hide the mouse and use a custom cursor, but the mouse is hidden in all other tabs. Open the attached project, run a web player and open some tabs, the mouse will disapear on the other tabs.
    8.  
    9. Unity web player always listens an reactes to the mouse, even if the user is on another tab. Mouse movement, clicks and scrolling all go directly to the web player, with no exeption!
    10.  
    11. In the attached project I made just for you :) you can build a web player, open some tabs and click around, the web player logs all you clicks, from the other tabs
    12.  
    13. I really hope you can fix this bug or in any other way help
    14.  
    15. Best Regards
    16.   Paul Usul Fluegel
    Attached Files


  3. Posts
    11
    Hi, I'm not completely sure about this but I think that you could check if the mouse is inside the game screen. So if it goes outside the game frame you could just make it autopause and bring the mouse back up, then when you continue the game just switch the mouse back to the gamemouse again and unpause it. As for the technical side of doing this... Try digging into the documentation. As I said though, I'm not completely sure about this.


  4. Location
    Denmark - Copenhagen
    Posts
    49
    Hi, thanks for the post.

    I hadn't thought of pausing, but disabling scripts. But I have tried from the browser side to call unity on onfocus/onblur.

    But to no avail, input is not affected by Time.timeScale or Time.fixedDeltaTime



  5. Location
    Denmark - Copenhagen
    Posts
    49
    Okay I have been working on this for a couple of days now, and have come up with the cleanest dirty hack I could.

    The idea is to have the browser tell unity when it's focus:

    Code:  
    1. <script language="javascript1.1" type="text/javascript">
    2.       function onBlur()
    3.       {
    4.       GetUnity().SendMessage("BrowserWindow", "WindowFocusOut", " ");
    5.       }
    6.  
    7.       function onFocus()
    8.       {
    9.       GetUnity().SendMessage("BrowserWindow", "WindowFocusIn", " ");
    10.       }
    11.  
    12.       // check for Internet Explorer
    13.       if (navigator.appName == "Microsoft Internet Explorer")
    14.       {
    15.       document.onfocusin = onFocus;
    16.       document.onfocusout = onBlur;
    17.       }
    18.       else
    19.       {
    20.       document.onfocus = onFocus;
    21.       document.onblur = onBlur;
    22.       }
    23.     </script>

    Now we have to have a game object named BrowserWindow, with a script with methods called WindowFocusIn and WindowFocusOut that take a string.

    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BrowserWindow : MonoBehaviour
    5. {
    6.     //gameobjects to get the scripts that need disabling!
    7.     public GameObject gameobject;
    8.  
    9.     private fooBarScript;
    10.  
    11.     private bool focus;
    12.     private bool debug;
    13.     private string logText;
    14.  
    15.     public bool Focus { get { return focus; } set { focus = value; } }
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         focus = true;
    21.         debug = false;
    22.  
    23.         fooBarScript= (fooBarScript)gameObject.GetComponent(typeof(fooBarScript));
    24.  
    25.         // to test that we actualy have something, for editor testing
    26.         fooBarScript.enabled = true;
    27.     }
    28.  
    29.     // OnGUI is called once per event
    30.     void OnGUI()
    31.     {
    32.         if (!focus)
    33.         {
    34.             //Gui magic to avoid gui events
    35.             GUI.depth = 0;
    36.             GUI.backgroundColor = new Color(1f, 1f, 1f, 0.25f);
    37.             GUI.color = Color.black;
    38.             GUI.Button(new Rect(0, 0, Screen.width, Screen.height), "Pause..");
    39.         }
    40.  
    41.         if (Debug)
    42.         {
    43.             GUI.Label(new Rect(0, 5, 140, Screen.height - 200), logText, "button");
    44.         }
    45.     }
    46.  
    47.     //this is called when the window we are in looses focus, but the window can loose focus to us the webplayer.
    48.     public void WindowFocusOut(string arg)
    49.     {
    50.         //if the mouse cords are inside us, and the window lost focus, we have the focus
    51.         if (new Rect(0, 0, Screen.width, Screen.height).Contains(new Vector2(Input.mousePosition.x, Input.mousePosition.y)))
    52.         {
    53.             WindowFocusIn(arg);
    54.             return;
    55.         }
    56.         logText += "Out\n";
    57.  
    58.         focus = false;
    59.  
    60.         //disable all your scripts.
    61.         foobarScript.enabled = false;
    62.     }
    63.  
    64.     public void WindowFocusIn(string arg)
    65.     {
    66.         logText += "In\n";
    67.  
    68.         focus = true;
    69.  
    70.         //enable all your scripts.
    71.         foobarScript.enabled = true;
    72.     }
    73. }

    tried to generalise the code, so it's useable by whom ever

    Now we just disable every thing that needs to be disabled, and no more shooting friends while in another tab :P

    But still, UT I really hope you come with a proper fix, keyboard kontrol trough tabs are fine, mouse should be to


  6. Location
    The sunken city of R'lyeh
    Posts
    2,061
    Quote Originally Posted by PaulUsul
    Hi, thanks for the post.

    I hadn't thought of pausing, but disabling scripts. But I have tried from the browser side to call unity on onfocus/onblur.

    But to no avail, input is not affected by Time.timeScale or Time.fixedDeltaTime

    Time.timeScale and the like only affect the physics engine. The rest, you have to either disable or simply bypass yourself appropriately when losing focus. So you're on the right track.

    Depending on how your game is set up, it might be just a matter of setting a bool to early exit OnGUI() and other routines where you're using mouse/keyboard input.

    *Edit : you beat me to it! Glad to see you figured it out.


  7. Location
    Denmark - Copenhagen
    Posts
    49
    Sad to say this does not fix the problem entirely. The bug still exists in some cases where the web player has focus and does not let it go to another tab

    Will post more when I find a fix.


  8. Location
    Zürich, Switzerland
    Posts
    26,572
    Disclaimer: Not tested
    But have you ensured that you disabled the "run in background" checkbox in the player settings / didn't enable it through script
    Because with run in background it won't care about not beeing the active tab, its enough for the browser window to be active for it to receive input as thats how plugins work in browsers.


  9. Location
    Denmark - Copenhagen
    Posts
    49
    Ya I have experimented with Run in background. But because the browser is still active(just in another tab), the web player still receives mouse activity, I'm guessing.

    Right now I'm experimenting with onmouseover/mouseout instead of onfocus events, because the latter really lacks cross browser compatibility.

    Thanks for all the posted ideas, they really do help inspire me to look in new places!


  10. Location
    Denmark - Copenhagen
    Posts
    49
    Okay this is a updated copy paste of my earlier post, this is the best possible solution I have got working. onfocus has been replaced with mouse over events and a single onfocus, so it's possible to click the web player to gain focus no matter what.

    [Edit] updated code to run with chrome+more
    Code:  
    1. <script language="javascript1.1" type="text/javascript">
    2.     function onBlur()
    3.     {
    4.         GetUnity().SendMessage("BrowserWindow", "WindowFocusOut", " ");
    5.     }
    6.  
    7.     function onFocus()
    8.     {
    9.         GetUnity().SendMessage("BrowserWindow", "WindowFocusIn", " ");
    10.     }
    11.  
    12.     // check for Internet Explorer, boo
    13.     if (navigator.appName == "Microsoft Internet Explorer")
    14.     {
    15.         // Internet Explorer
    16.         GetUnity().attachEvent('onmouseover', onFocus, false);
    17.         GetUnity().attachEvent('onfocusin', onFocus, false);
    18.         GetUnity().attachEvent('onmouseout', onBlur, false);
    19.     }
    20.     else
    21.     {
    22.         // Netscape, Firefox, Mozilla, Chrome and Safari.
    23.         GetUnity().addEventListener('mouseover', onFocus, false);
    24.         GetUnity().addEventListener('focus', onFocus, false);
    25.         GetUnity().addEventListener('mouseout', onBlur, false);
    26.     }
    27. </script>

    Now we have to have a game object named BrowserWindow, with a script with methods called WindowFocusIn and WindowFocusOut that take a string.

    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BrowserWindow : MonoBehaviour
    5. {
    6.     //gameobjects to get the scripts that need disabling!
    7.     public GameObject gameobject;
    8.  
    9.     private fooBarScript;
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         fooBarScript= (fooBarScript)gameObject.GetComponent(typeof(fooBarScript));
    15.  
    16.         // to test that we actualy have something, for editor testing
    17.         if (fooBarScript == null)
    18.         {
    19.             Debug.LogError("fooBarScript was null!");
    20.         }
    21.  
    22.         //so if the user opens the player and changes tab, with out any focus change
    23.         WindowFocusOut("");
    24.     }
    25.  
    26.     public void WindowFocusOut(string arg)
    27.     {
    28.         Screen.showCursor = true;
    29.         //disable all your scripts.
    30.         foobarScript.enabled = false;
    31.     }
    32.  
    33.     public void WindowFocusIn(string arg)
    34.     {
    35.         //enable all your scripts.
    36.         foobarScript.enabled = true;
    37.     }
    38. }

    I have removed all the debuggin to unclutter the code, hope this helps some one


  11. Posts
    52
    I carefully read this read and the solution you give globaly work.
    But in my case I need to get an event when a user click outside the unity object and not when the mouse is outside (not over)
    So I added the blur event (GetUnity().addEventListener('blur', onBlur, false)
    which does exactly that but the result is very strange.

    I don't receive the message in unity (I mean BrowserWindow/WindowFocusOut is not called) at the instant where the event occur but later when the object regain focus. So when i enter focus I get two event, the focus In and then the focus out i didn't have when it effectively occur ...
    Do you have the same trouble with this event, that maybe why he don't appear in you example ?

    Best Regards
    Jérôme Foucher


  12. Location
    Denmark - Copenhagen
    Posts
    49
    I know this sounds a bit weird, but have you checked run in background? since the window is not in focus, it could be that unity can't process the events. But all the browsers really suck at event propagation, but hope you get it working


  13. Posts
    52
    yes run in background is active.

    I just find strange that the mouseOut event work and not the blur event ...

    Does it work for you ?


  14. Location
    London
    Posts
    1,098
    Anyone know how to fix this same issue with the standalone app? On windows if you open another app on top of the unity one you can still click through, but in that situation there is no browser to send the message.


  15. Location
    London
    Posts
    1,098
    Has anyone got the mouseout event to work in firefox on windows?
    It works in firefox on the mac fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •