Skip to content

Instantly share code, notes, and snippets.

@JakubNei
Last active April 12, 2022 01:54
Show Gist options
  • Save JakubNei/74bb5eba12a91a7d5f6334e7af365b11 to your computer and use it in GitHub Desktop.
Save JakubNei/74bb5eba12a91a7d5f6334e7af365b11 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
public static class ApplicationState
{
/// <summary>
/// Is editor either currently in play mode, or about to switch to it?
/// </summary>
public static bool isPlayingOrWillChangePlaymode { get { return Application.isEditor ? InternalEditorState.isPlayingOrWillChangePlaymode : true; } }
/// <summary>
/// Is editor currently paused?
/// </summary>
public static bool isPaused { get { return Application.isEditor ? InternalEditorState.isPaused : false; } }
/// <summary>
/// Is editor currently compiling scripts?
/// </summary>
public static bool isCompiling { get { return Application.isEditor ? InternalEditorState.isCompiling : false; } }
/// <summary>
/// This property is true if the Editor is currently refreshing the AssetDatabase.
/// During this time, the editor checks to see if any files have changed, whether
/// they need to be reimported, and reimports them.
/// </summary>
public static bool isUpdating { get { return Application.isEditor ? InternalEditorState.isUpdating : false; } }
/// <summary>
/// Is editor currently connected to Unity Remote 4 client app.
/// </summary>
public static bool isRemoteConnected { get { return Application.isEditor ? InternalEditorState.isRemoteConnected : false; } }
/// <summary>
/// Returns the path to the Unity editor application.
/// </summary>
public static string applicationPath { get { return Application.isEditor ? InternalEditorState.applicationPath : AppDomain.CurrentDomain.BaseDirectory; } }
/// <summary>
/// The time since the editor was started.
/// </summary>
public static double timeSinceStartup { get { return Application.isEditor ? InternalEditorState.timeSinceStartup : Time.realtimeSinceStartup; } }
/// <summary>
/// Is editor currently in play mode?
/// </summary>
public static bool isPlaying { get { return Application.isEditor ? InternalEditorState.isPlaying : true; } }
/// <summary>
/// Path to the Unity editor contents folder.
/// </summary>
public static string applicationContentsPath { get { return Application.isEditor ? InternalEditorState.applicationContentsPath : Application.dataPath; } }
public static bool isAboutToEnterPlayMode { get { return Application.isEditor ? !isPlaying && timeSinceStartup == 0 : false; } }
public static string ToDebugString()
{
return
"Application.isEditor = " + Application.isEditor + Environment.NewLine +
"isPlayingOrWillChangePlaymode = " + isPlayingOrWillChangePlaymode + Environment.NewLine +
"isPaused = " + isPaused + Environment.NewLine +
"isCompiling = " + isCompiling + Environment.NewLine +
"isUpdating = " + isUpdating + Environment.NewLine +
"isRemoteConnected = " + isRemoteConnected + Environment.NewLine +
"applicationPath = " + applicationPath + Environment.NewLine +
"timeSinceStartup = " + timeSinceStartup + Environment.NewLine +
"isPlaying = " + isPlaying + Environment.NewLine +
"applicationContentsPath = " + applicationContentsPath + Environment.NewLine +
"isAboutToEnterPlayMode = " + isAboutToEnterPlayMode + Environment.NewLine;
}
public static class InternalEditorState
{
public static bool isPlayingOrWillChangePlaymode;
public static bool isPaused;
public static bool isCompiling;
public static bool isUpdating;
public static bool isRemoteConnected;
public static string applicationPath;
public static double timeSinceStartup;
public static bool isPlaying;
public static string applicationContentsPath;
public static string ToDebugString()
{
return
"isPlayingOrWillChangePlaymode = " + isPlayingOrWillChangePlaymode + Environment.NewLine +
"isPaused = " + isPaused + Environment.NewLine +
"isCompiling = " + isCompiling + Environment.NewLine +
"isUpdating = " + isUpdating + Environment.NewLine +
"isRemoteConnected = " + isRemoteConnected + Environment.NewLine +
"applicationPath = " + applicationPath + Environment.NewLine +
"timeSinceStartup = " + timeSinceStartup + Environment.NewLine +
"isPlaying = " + isPlaying + Environment.NewLine +
"applicationContentsPath = " + applicationContentsPath + Environment.NewLine;
}
}
}
using UnityEditor;
[InitializeOnLoad]
public class UpdateApplicationState {
static UpdateApplicationState()
{
EditorApplication.update += Update;
EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
}
static void Update ()
{
ApplicationState.InternalEditorState.isPlayingOrWillChangePlaymode = EditorApplication.isPlayingOrWillChangePlaymode;
ApplicationState.InternalEditorState.isPaused = EditorApplication.isPaused;
ApplicationState.InternalEditorState.isCompiling = EditorApplication.isCompiling;
ApplicationState.InternalEditorState.isUpdating = EditorApplication.isUpdating;
ApplicationState.InternalEditorState.isRemoteConnected = EditorApplication.isRemoteConnected;
ApplicationState.InternalEditorState.applicationPath = EditorApplication.applicationPath;
ApplicationState.InternalEditorState.timeSinceStartup = EditorApplication.timeSinceStartup;
ApplicationState.InternalEditorState.isPlaying = EditorApplication.isPlaying;
ApplicationState.InternalEditorState.applicationContentsPath = EditorApplication.applicationContentsPath;
}
static void PlaymodeStateChanged()
{
Update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment