【Unity】C#線程與Unity物件交互
由於之前用太習慣AS3的SetTimeOut,而C#與Unity竟然沒有這個功能。
於是自己弄了一個C#的CLASS。
使用額外的線程,來做Delay的延遲。
這個C# CLASS裡面是這樣
This file contains hidden or 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
public static Guid SetTimeout (int delay, Action action) | |
{ | |
Guid g = Guid.NewGuid (); | |
Thread t = new Thread (() => | |
{ | |
Thread.Sleep (delay); | |
_setTimeoutHandles.Remove (g); | |
action (); | |
}); | |
_setTimeoutHandles.Add (g, t); | |
t.Start (); | |
return g; | |
} | |
完成後開始與unity物件使用
_tmpSetTimeOut._setTimeOut (2000, delegate {
Debug.Log ("compelete");
_gameObject.SetActive (true);
});
log是會打印出來的,但是系統報出了以下的訊息
get_gameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
後來去查了 下,才發現Unity的物件似乎只能由Unity的線程來作管控…
張貼留言