Skip to content

Commit 5e12e73

Browse files
authored
Merge pull request #841 from PlayEveryWare/fix/quit-at-the-right-time
fix(eosmanager): Manage application shutdown only on Application.quitting
2 parents dc4ff13 + d807538 commit 5e12e73

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

com.playeveryware.eos/Runtime/Core/EOSManager.cs

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public partial class EOSManager : MonoBehaviour, IEOSCoroutineOwner
100100
// <value>If true, EOSManager initialized itself at startup.</value>
101101
public bool InitializeOnAwake = true;
102102

103-
/// <value>If true, EOSManager will shutdown the EOS SDK when Unity dispatches OnApplicationQuit. </value>
103+
/// <value>If true, EOSManager will shutdown the EOS SDK when Unity runs <see cref="Application.quitting"/>.</value>
104104
public bool ShouldShutdownOnApplicationQuit = true;
105105

106106
#if !EOS_DISABLE
@@ -1812,20 +1812,6 @@ void Update()
18121812
Instance.Tick();
18131813
}
18141814

1815-
//-------------------------------------------------------------------------
1816-
/// <summary>Unity [OnApplicationQuit](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html) is called before the application quits.
1817-
/// <list type="bullet">
1818-
/// <item><description>Calls <c>OnShutdown()</c></description></item>
1819-
/// </list>
1820-
/// </summary>
1821-
private void OnApplicationQuit()
1822-
{
1823-
if (ShouldShutdownOnApplicationQuit)
1824-
{
1825-
Instance.OnShutdown();
1826-
}
1827-
}
1828-
18291815
//-------------------------------------------------------------------------
18301816
/// <summary>Unity [OnApplicationFocus](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html) is called when the application loses or gains focus.
18311817
/// <list type="bullet">
@@ -1847,6 +1833,51 @@ void OnApplicationPause(bool pauseStatus)
18471833
{
18481834
Instance.OnApplicationPause(pauseStatus);
18491835
}
1836+
1837+
/// <summary>
1838+
/// Whenever the EOSManager becomes available and active, it subscribes
1839+
/// to [Application.quitting](https://docs.unity3d.com/ScriptReference/Application-quitting.html).
1840+
/// The event will only run when the application is definitely closing without the ability for it to be canceled.
1841+
/// </summary>
1842+
void OnEnable()
1843+
{
1844+
Application.quitting += OnApplicationQuitting;
1845+
}
1846+
1847+
/// <summary>
1848+
/// Whenever the EOSManager becomes inactive, it unsubscribes to
1849+
/// to [Application.quitting](https://docs.unity3d.com/ScriptReference/Application-quitting.html).
1850+
/// This is in case the manager is unloaded without the application ending.
1851+
/// </summary>
1852+
void OnDisable()
1853+
{
1854+
Application.quitting -= OnApplicationQuitting;
1855+
}
1856+
1857+
/// <summary>
1858+
/// Event that should be subscribed to Application.quitting, with the event
1859+
/// managed by <see cref="OnEnable"/> and <see cref="OnDisable"/>.
1860+
/// This is intentionally named to be different than "OnApplicationQuit", which is a Unity Message
1861+
/// that runs when Unity begins considering quitting.
1862+
/// Instead, this should be subscribed to <see cref="Application.quitting"/>, which is an event
1863+
/// that only fires when the Application is irreversably shutting down.
1864+
/// </summary>
1865+
void OnApplicationQuitting()
1866+
{
1867+
if (ShouldShutdownOnApplicationQuit)
1868+
{
1869+
#if EOS_CAN_SHUTDOWN
1870+
print($"{nameof(EOSManager)} ({nameof(OnApplicationQuitting)}): Application is quitting. {nameof(ShouldShutdownOnApplicationQuit)} is true, so the plugin is being shut down. EOS_CAN_SHUTDOWN is true, so the EOS SDK will now be shut down fully.");
1871+
#else
1872+
print($"{nameof(EOSManager)} ({nameof(OnApplicationQuitting)}): Application is quitting. {nameof(ShouldShutdownOnApplicationQuit)} is true, so the plugin is being shut down. EOS_CAN_SHUTDOWN is false, so the EOS SDK will not be shut down.");
1873+
#endif
1874+
Instance.OnShutdown();
1875+
}
1876+
else
1877+
{
1878+
print($"{nameof(EOSManager)} ({nameof(OnApplicationQuitting)}): Application is quitting. {nameof(ShouldShutdownOnApplicationQuit)} is false, so this manager will not shut down the EOS SDK.");
1879+
}
1880+
}
18501881
#endif
18511882

18521883
//-------------------------------------------------------------------------

0 commit comments

Comments
 (0)