Skip to content

Instantly share code, notes, and snippets.

@bluenex
Last active September 2, 2015 18:37
Show Gist options
  • Save bluenex/60b9e2fad9d86473b5d9 to your computer and use it in GitHub Desktop.
Save bluenex/60b9e2fad9d86473b5d9 to your computer and use it in GitHub Desktop.
Snippet to allow using thread in Unity3d. This snippet including dependency, where should threading code be, and editor events detection to kill thread.

Dependencies

using System.Threading;
using UnityEditor;

Declare classes

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);
		
	}

Using thread

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();
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment