using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class testt : MonoBehaviour
{
///
/// 线程
///
Thread tt;
///
/// 控制线程状态
///
bool issend = false;
// Start is called before the first frame update
void Start()
{
// Thread th = new Thread(new ThreadStart(testThread));//开启一个线程
Thread th = new Thread(testThread);//开启一个线程 两种写法
th.IsBackground = false;
th.Start();
tt = th;
//while (true)//如果放在主线程跑会死循环
//{
// Debug.Log(">>>>>>>>>>>");
// // Thread.Sleep(1000);
//}
}
void testThread()
{
while (true)
{
if (issend)//没有这个布尔值会发现 停止运行Unity 线程没有停止 即使设置了Thread.IsBackground = true
{
break;
}
Thread.Sleep(1000);
}
}
private void OnDestroy()
{
issend = true;
tt.Abort();//关闭线程
}
}