您的位置:首页 > 移动开发 > Unity3D

C#多线程与Unity中的使用

2015-06-26 17:42 316 查看
C#的多线程是一个非常有用的功能,测试如下:

(自动加i)

using UnityEngine;
using System.Collections;
using System.Threading;

public class Test : MonoBehaviour {
Thread myThread;//定义的线程
bool _myBool = true;//控制执行线程的变量
// Use this for initialization
void Start () {
this.myThread = new Thread(new ThreadStart(myStartingMethod));
myThread.Start();
}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.X))
{
_myBool = false;//如果按X,停止线程执行
}
}
void myStartingMethod()
{
int i = 0;
while (_myBool)
{
i++;
print(i);
if (i > 50000)
{
_myBool = false;
}
}
}
}


在这次测试中,对线程使用Above()方法并没有什么用,线程还是停不下来。最后使用上面的全局变量,轻松停下来了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity c# thread