我的工程-关于测试程序的多线程
2016-08-11 20:57
330 查看
突然想到了StartCoroutine,于是使用了下,果然Unity不支持C#提供的多线程,但是可以用自身修改过的StartCoroutine开启,对自己的内部函数目前没看到什么限制,修改后的测试文件代码为
using UnityEngine;
using System.Collections;
using System.Threading;
public class BallInNetTest : MonoBehaviour {
static GameObject[] numberBalls;
static Vector3 fallInNetPos1;
// Use this for initialization
void Start () {
fallInNetPos1 = new Vector3 (-2.757f, 0.748f, 1.347f);
numberBalls = GameObject.FindGameObjectsWithTag("NumberBall");
if (numberBalls.Length != 15)
Debug.Log ("Less than 15 Balls?");
for (int i = 0; i < numberBalls.Length; i++)
{
for (int j = i + 1; j < numberBalls.Length; j++)
{
4000
if(int.Parse(numberBalls[i].name) > int.Parse(numberBalls[j].name))
{
GameObject temp = numberBalls[i];
numberBalls[i] = numberBalls[j];
numberBalls[j] = temp;
}
}
}
}
// Update is called once per frame
public void BallFallInNet()
{
StartCoroutine (WaitAndPrint (2.0F));
for(int i = 0; i < 8;i++)
{
GameObject tmpBall = numberBalls [i];
if (tmpBall != null && tmpBall.activeSelf == true) {
tmpBall.transform.position = fallInNetPos1;
break;
}
}
}
IEnumerator WaitAndPrint(float waitTime) {
int cnt = 3;
while (cnt-- > 0) {
yield return new WaitForSeconds (waitTime);
for (int i = 0; i < 8; i++) {
GameObject tmpBall = numberBalls [i];
if (tmpBall != null && tmpBall.activeSelf == true) {
tmpBall.transform.position = fallInNetPos1;
break;
}
}
}
}
}
这里要注意:
1.StartCoroutine
为MonoBehaviour
成员函数,不能在static中用,所以要把BallFallInNet改成非static函数,调用方式也要改。
2.有StopCoroutine可以停止,但是我们这里选择了简单的例子。其他可以自行研究,具体在Unity中多线程要遵守StartCoroutine的使用方式。
using UnityEngine;
using System.Collections;
using System.Threading;
public class BallInNetTest : MonoBehaviour {
static GameObject[] numberBalls;
static Vector3 fallInNetPos1;
// Use this for initialization
void Start () {
fallInNetPos1 = new Vector3 (-2.757f, 0.748f, 1.347f);
numberBalls = GameObject.FindGameObjectsWithTag("NumberBall");
if (numberBalls.Length != 15)
Debug.Log ("Less than 15 Balls?");
for (int i = 0; i < numberBalls.Length; i++)
{
for (int j = i + 1; j < numberBalls.Length; j++)
{
4000
if(int.Parse(numberBalls[i].name) > int.Parse(numberBalls[j].name))
{
GameObject temp = numberBalls[i];
numberBalls[i] = numberBalls[j];
numberBalls[j] = temp;
}
}
}
}
// Update is called once per frame
public void BallFallInNet()
{
StartCoroutine (WaitAndPrint (2.0F));
for(int i = 0; i < 8;i++)
{
GameObject tmpBall = numberBalls [i];
if (tmpBall != null && tmpBall.activeSelf == true) {
tmpBall.transform.position = fallInNetPos1;
break;
}
}
}
IEnumerator WaitAndPrint(float waitTime) {
int cnt = 3;
while (cnt-- > 0) {
yield return new WaitForSeconds (waitTime);
for (int i = 0; i < 8; i++) {
GameObject tmpBall = numberBalls [i];
if (tmpBall != null && tmpBall.activeSelf == true) {
tmpBall.transform.position = fallInNetPos1;
break;
}
}
}
}
}
这里要注意:
1.StartCoroutine
为MonoBehaviour
成员函数,不能在static中用,所以要把BallFallInNet改成非static函数,调用方式也要改。
2.有StopCoroutine可以停止,但是我们这里选择了简单的例子。其他可以自行研究,具体在Unity中多线程要遵守StartCoroutine的使用方式。
相关文章推荐
- 关于volatile——编写多线程程序的好帮手
- 关于在Java程序当中创建对象等基本操作的性能测试
- 关于android工程下不能运行java main程序的解决办法
- 关于孙鑫MFC视频第三章中this指针指向基类还是派生类的测试程序
- java中关于Socket类通信的基本测试程序
- 关于多线程程序中锁定内存的一些介绍
- 关于core_uses_pid标志对多线程程序无用的调查
- 关于RPC方式的C/S程序测试-01
- 【java多线程中测试同步的小程序】
- 关于类的继承测试程序
- 关于VB使用ActiveX.Exe创建多线程程序的奇怪问题
- Java语言深入:关于多线程程序模型研究
- 关于JUNIT4 中的多线程测试的疑惑
- 关于xcode工程中添加资源文件程序找不到的问题
- 关于一个多线程、断点续传的FTP服务程序!
- 关于EMI多线程分段测试方法的说明
- [MFC学习之C++基础] 孙鑫视频中第三讲中提到的小测试程序(关于基类和子类的继承关系)
- iOS关于数据库操作之二 将工程里的数据库文件在程序加载时移至沙盒
- FM并发编程练习:改写苏飞的C#多线程网站压力测试程序
- 编程菜鸟的日记-初学尝试编程-关于测试程序