does anyone know if there is a way to make a unity app to play a video if nothing has happened on the app for a while? trying to create a screen saver for the current application I’m working on.
Here is a script that should work :
[SerializeField]
private float resetTime = 30.0f;
private float lastInputTime ;
private Vector3 lastMousePosition;
private bool videoIsPlaying ;
private void Update()
{
// Detect if :
// - A key has been pressed
// - A finger touched the screen
// - The mouse has been moved
// If so, save when the current time
if ( Input.anyKey || Input.touchCount > 0 || (Input.mousePosition - lastMousePosition).sqrMagnitude > Mathf.Epsilon )
{
lastInputTime = Time.time;
StopVideo();
}
// Save the current mouse position in the current frame to compare with the mouse position the nextframe
lastMousePosition = Input.mousePosition;
// Compute the time between now and the last time Unity received an input
// and play the video if the time is greater than the time you defined in the inspector
// Also, make sure the video is not already playing
if( !videoIsPlaying && ( Time.time - lastInputTime ) > resetTime )
{
PlayVideo();
}
}
private void PlayVideo()
{
// Your code
videoIsPlaying = true ;
}
private void StopVideo()
{
// Your code
videoIsPlaying = false ;
}