Last active
May 26, 2022 01:31
-
-
Save popcron/6bc70756e31ec2b73314c4f69c4d0016 to your computer and use it in GitHub Desktop.
Custom code before play
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine; | |
namespace blablalblalmao | |
{ | |
[InitializeOnLoad] | |
public class CustomPlayBehaviour | |
{ | |
private static PlayStateProcess playStateProcess; | |
static CustomPlayBehaviour() | |
{ | |
EditorApplication.playModeStateChanged += OnPlayModeStateChanging; | |
EditorApplication.update += OnUpdate; | |
} | |
private static void OnUpdate() | |
{ | |
if (!EditorApplication.isPlayingOrWillChangePlaymode) | |
{ | |
if (playStateProcess == PlayStateProcess.PreStarting) | |
{ | |
float startTime = EditorPrefs.GetFloat("startTime"); | |
if (EditorApplication.timeSinceStartup > startTime) | |
{ | |
playStateProcess = PlayStateProcess.Starting; | |
OnAboutToEnterPlaymode(); | |
playStateProcess = PlayStateProcess.Ready; | |
EditorApplication.isPlaying = true; | |
} | |
} | |
else | |
{ | |
playStateProcess = PlayStateProcess.Editing; | |
} | |
} | |
} | |
private static void OnPlayModeStateChanging(PlayModeStateChange playModeStateChange) | |
{ | |
if (playModeStateChange == PlayModeStateChange.ExitingEditMode) | |
{ | |
if (playStateProcess != PlayStateProcess.Ready) | |
{ | |
EditorApplication.isPlaying = false; | |
} | |
if (playStateProcess == PlayStateProcess.Editing) | |
{ | |
playStateProcess = PlayStateProcess.PreStarting; | |
EditorPrefs.SetFloat("startTime", (float)EditorApplication.timeSinceStartup + 0.15f); | |
} | |
} | |
else if (playModeStateChange == PlayModeStateChange.EnteredEditMode) | |
{ | |
if (playStateProcess == PlayStateProcess.Ready) | |
{ | |
playStateProcess = PlayStateProcess.Editing; | |
} | |
} | |
} | |
private static void OnAboutToEnterPlaymode() | |
{ | |
//this will run just before play mode | |
} | |
public enum PlayStateProcess | |
{ | |
Editing, | |
PreStarting, | |
Starting, | |
Ready | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment