Detect mouse inputs when the scene is changing to another scene.
Hello,
I need to detect if the player is still pressing the mouse or not when the Scene is changing. If you reproduce these steps:
Press the mouse button
Change scene
Release the button < This event is not detected with
Input.GetMouseButtonUp(0)
EDIT: Input.Get$$anonymous$$ouseButton(0) is not triggered too.
Answer by BenoitFreslon · Feb 06, 2017 at 12:07 PM
I found a workaround:
On a mobile game we can use:
Input.touchCount
So we can detect if the user is still touching the screen during the scene transition.
Answer by HenryStrattonFW · Feb 02, 2017 at 09:29 PM
You could try altering your logic a bit, instead of using GetMouseButtonUp and GetMouseButtonDown instead use GetMouseButton (returns true if mouse currently down, false otherwise) and then set your own flag based on that to allow you to respond to the discrete up/down moments instead of the continuous up or down state.
EDIT: Of course you'd need to make sure that the flag you store is being stored somewhere that will persist scene changes.
EDIT2:
So I tried to write up a test case for this, and here's what I found, when you do a scene load with single mode, be it sycnronous or asynchronous, Input manager seems to undergo some sort of reset event, triggering making it impossible to reliably track the mouse press/release over the scene load. However if you load the new scene additively, then the Input manager does not get the same reset event and you can track the mouse events over the load.
Not sure if that information is useful at all in any way, perhaps you can fashion a scenario where you additively load the next scene, and then unload the previous once the mouse up event has occurred, little bit of an ugly workaround true but it might be the only way. If anyone is interested here was the code for my test case.
using UnityEngine;
using UnityEngine.SceneManagement;
public class MouseTest : MonoBehaviour
{
private bool m_MouseDown;
void Awake()
{
DontDestroyOnLoad(gameObject);
}
void Update()
{
if (Input.GetMouseButton(0))
{
if (!m_MouseDown)
{
m_MouseDown = true;
_OnMouseDown();
}
}
else
{
if (m_MouseDown)
{
m_MouseDown = false;
_OnMouseUp();
}
}
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadSceneAsync("TestSceneB", LoadSceneMode.Additive);
}
}
private void _OnMouseDown()
{
Debug.Log("Mouse Down");
}
private void _OnMouseUp()
{
Debug.Log("Mouse Up");
}
}
I already tried to use Get$$anonymous$$ouseButton and store the status in a static variable.
I use this code in the Update method in a persistant GameObject
GameObject.DontDestroyOnLoad(gameObject);
in the Update method:
if ( Input.Get$$anonymous$$ouseButton ( 0 ) )
Debug.Log ( "$$anonymous$$ouseButton pressed" );
if ( Input.Get$$anonymous$$ouseButtonDown ( 0 ) )
Debug.Log ( "$$anonymous$$ouse button down" );
if ( Input.Get$$anonymous$$ouseButtonUp ( 0 ) )
Debug.Log ( "$$anonymous$$ouse button up" );
But the Input.get$$anonymous$$ouseButton is never triggered when I release the mouse button in the new scene.
I've updated my answer with some more information I've found out from testing. Seems the only way to achieve reliable mouse event tracking over the load is to load the scene additively.