Skip to content

Instantly share code, notes, and snippets.

@Suzeep
Created October 26, 2013 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Suzeep/7170157 to your computer and use it in GitHub Desktop.
Save Suzeep/7170157 to your computer and use it in GitHub Desktop.
Spicy Pixel のConcurrency Kitを使ったサンプルコード。
using UnityEngine;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
using SpicyPixel.Threading;
using SpicyPixel.Threading.Tasks;
public class SpicyTest : ConcurrentBehaviour
{
// Awake
public virtual void Awake()
{
base.Awake();
m_Thread = new Thread( procOnThread );
m_Thread.Start();
}
// OnApplicationQuit
void OnApplicationQuit()
{
m_Thread.Abort();
}
// Update
void Update()
{
if( m_IsActive ){
m_CountByMainThread ++ ;
}
if( Input.GetKeyDown(KeyCode.A) ){
m_IsActive = true;
}
}
// work on thread
private void procOnThread()
{
while( true )
{
Thread.Sleep(0);
if( m_IsActive )
{
Task.WaitAll(
taskFactory.StartNew( callUnityAPI )
);
m_CountByOtherThread ++ ;
}
}
}
// call Unity API
private void callUnityAPI()
{
m_RealTime = Time.realtimeSinceStartup;
}
// member
private Thread m_Thread;
private bool m_IsActive;
public int m_CountByMainThread;
public int m_CountByOtherThread;
public float m_RealTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment