Skip to content

Instantly share code, notes, and snippets.

@popcron
Last active May 26, 2022 01:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save popcron/6bc70756e31ec2b73314c4f69c4d0016 to your computer and use it in GitHub Desktop.
Save popcron/6bc70756e31ec2b73314c4f69c4d0016 to your computer and use it in GitHub Desktop.
Custom code before play
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