Unity写多线程注意事项

在这里插入图片描述
第一步:
using System.Threading; //引入头文件
第二步:

private Thread threads;                     //子线程
void Start(){
    
    
	threads = new Thread(SendMsg);
	threads.Start();
}
private void SendMsg()                      //写入
{
    
    
    Thread.Sleep(500);					    //子线程延时0.5s
    Debug.log("hello   world!");
}
public void OnDestroy()                      //关闭或则退出时自动销毁子线程
{
    
    
    if (threads != null) {
    
     threads.Abort(); }
}

tips:子线程里不能获取Application.persistentDataPath;
可以这样:

 string persistent = "";
 void Start(){
    
    
 	persistent = Application.persistentDataPath;
 }
 ...
 ...
 void SendMsg(){
    
          //子线程运行的方法
 	string path=persistent+"/test.txt";
 	...
 	...
 }

猜你喜欢

转载自blog.csdn.net/kuilaurence/article/details/109646094