Skip to content

Instantly share code, notes, and snippets.

@shane-harper
Last active June 4, 2019 09:37
Show Gist options
  • Save shane-harper/b198337cc67e063074045e7a4b44f166 to your computer and use it in GitHub Desktop.
Save shane-harper/b198337cc67e063074045e7a4b44f166 to your computer and use it in GitHub Desktop.
An object for Unity mobile to keep the screen awake while doing something
using System;
using System.Collections;
using UnityEngine;
public struct ScreenNeverSleepScope : IDisposable
{
public ScreenNeverSleepScope(bool neverSleep)
{
if (neverSleep)
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
public void Dispose()
{
Screen.sleepTimeout = SleepTimeout.SystemSetting;
}
}
public class ExampleUse : MonoBehaviour
{
private IEnumerator Start()
{
using (new ScreenNeverSleepScope(true))
{
// Do some things
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment