Created
October 26, 2013 14:39
-
-
Save Suzeep/7170157 to your computer and use it in GitHub Desktop.
Spicy Pixel のConcurrency Kitを使ったサンプルコード。
This file contains 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 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