using System.Threading;
using UnityEditor;
Above the public class ScriptFileName : MonoBehaviour
.
public enum PlayModeState
{
Stopped,
Playing,
Paused
}
[InitializeOnLoad]
public class EditorPlayMode
{
private static PlayModeState _currentState = PlayModeState.Stopped;
static EditorPlayMode()
{
EditorApplication.playmodeStateChanged = OnUnityPlayModeChanged;
}
public static event Action<PlayModeState, PlayModeState> PlayModeChanged;
public static void Play()
{
EditorApplication.isPlaying = true;
}
public static void Pause()
{
EditorApplication.isPaused = true;
}
public static void Stop()
{
EditorApplication.isPlaying = false;
}
private static void OnPlayModeChanged(PlayModeState currentState, PlayModeState changedState)
{
if (PlayModeChanged != null)
PlayModeChanged(currentState, changedState);
}
private static void OnUnityPlayModeChanged()
{
var changedState = PlayModeState.Stopped;
switch (_currentState)
{
case PlayModeState.Stopped:
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
changedState = PlayModeState.Playing;
}
break;
case PlayModeState.Playing:
if (EditorApplication.isPaused)
{
changedState = PlayModeState.Paused;
}
else
{
changedState = PlayModeState.Stopped;
}
break;
case PlayModeState.Paused:
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
changedState = PlayModeState.Playing;
}
else
{
changedState = PlayModeState.Stopped;
}
break;
default:
throw new ArgumentOutOfRangeException();
}
// Fire PlayModeChanged event.
OnPlayModeChanged(_currentState, changedState);
// Set current state.
_currentState = changedState;
}
}
Inside the public class ScriptFileName : MonoBehaviour
as fisrt class.
[InitializeOnLoad]
public class SingleEntryPoint
{
static SingleEntryPoint()
{
Debug.Log("SingleEntryPoint. Up and running");
EditorPlayMode.PlayModeChanged += OnPlayModeChanged;
}
private static void OnPlayModeChanged(PlayModeState currentMode, PlayModeState changedMode)
{
if(currentMode == PlayModeState.Playing && changedMode == PlayModeState.Stopped)
{
Debug.Log("Thread is killed! - Playing to Stopped");
threadFlag = false;
}
if(currentMode == PlayModeState.Playing && changedMode == PlayModeState.Paused)
{
Debug.Log("Editor is paused.");
}
if(currentMode == PlayModeState.Stopped && changedMode == PlayModeState.Playing)
{
Debug.Log("Start playing");
}
if(currentMode == PlayModeState.Paused && changedMode == PlayModeState.Playing)
{
Debug.Log("Back to play again!");
}
if(currentMode == PlayModeState.Paused && changedMode == PlayModeState.Stopped)
{
Debug.Log("Thread is killed! - Pause to Stopped");
threadFlag = false;
}
}
}
Inside the public class ScriptFileName : MonoBehaviour
as last class.
void OnApplicationQuit()
{
threadFlag = false;
Debug.Log("2 "+threadFlag);
}
Declare threadFlag
.
private static bool threadFlag = true;
Create method to run in another thread. This method will run endlessly until threadFlag
is set to false
.
private float needPosition;
private float finalPosition;
void doSomething()
{
while(threadFlag)
{
needPosition = Sensor.readPosition();
finalPosition = (needPosition * 1024)/18;
}
}
It is fine to call thread from start now!
void Start()
{
Thread readPosThread = new Thread(doSomething);
readPosThread.Start();
}