写一个简单的测试程序,免得我们自己手动打球,首先用一个cube放在某一个洞的上面来取得可掉入洞位置的世界坐标
static Vector3 fallInNetPos1;
然后建立一个空物体,绑定如下脚本
using UnityEngine;
using System.Collections;
using System.Threading;
public class BallInNetTest : MonoBehaviour {
static Vector3 fallInNetPos1;
// Use this for initialization
void Start () {
fallInNetPos1 = new Vector3 (-5.6736f, -2.4619f, 7.71290f);
}
// Update is called once per frame
public static void BallFallInNet()
{
Thread t = new Thread(new ThreadStart(BallFallInNetThread));
t.Start();
}
static void BallFallInNetThread()
{
for(int i = 1; i < 9;i++)
{
GameObject tmpBall = GameObject.Find(i.ToString());
if(tmpBall != null && tmpBall.activeSelf == true)
tmpBall.transform.position = fallInNetPos1;
Thread.Sleep(1000);
}
}
}
但是报错说只能在主线程中执行Find方法,就是GameObject.Find,修改之后又依次告诉我get_activeSelf,get_transform方法都只能从主线程调用,于是只能放弃
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++)
{
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 static void BallFallInNet()
{
for(int i = 0; i < 8;i++)
{
GameObject tmpBall = numberBalls [i];
if (tmpBall != null && tmpBall.activeSelf == true) {
tmpBall.transform.position = fallInNetPos1;
break;
}
}
}
}
改成每按击球进一个
然后 在StickControl中,注释掉tableStickRigidBody.AddForce (hitVector * hitSpeed, ForceMode.Force);,添加 BallInNetTest.BallFallInNet ();
if (Input.GetKeyUp (KeyCode.Space)) {
startSlider = false;
ifHitReady = true;
hitVector = (whiteBall.transform.position - tableStick.transform.position).normalized;
//tableStickRigidBody.AddForce (hitVector * hitSpeed, ForceMode.Force);
}
就可以了,如果需要一次进多球,可放在不同袋口,或是有高度差,这也是一种方便的测试方法,不然人工测这种程序确实比较累。
然后发现了2个bug,一个是2号和5号的背景没透明,一个是8号打进后没显示。
NetAndInBallController中对应段落改为,应该就好了。图片自己改下吧。
else if (tempBallType == 0) {
if (sceneManager.nowPlayer == 1) {
if (sceneManager.player1.ballInOrder.Count >= 7) {
sceneManager.player1.ballInOrder.Add (other.name);
sceneManager.SetLastBallFallInNet (1);
sceneManager.player1.ballInThisTurn.Add (other.name);
sceneManager.Player1Win ();
other.gameObject.SetActive (false);
}
else {
sceneManager.resetBlackBall = true;
SceneManager.changePlayer = true;
other.gameObject.SetActive (false);
}
} else if (sceneManager.nowPlayer == 2) {
if (sceneManager.player2.ballInOrder.Count >= 7) {
sceneManager.player2.ballInOrder.Add (other.name);
sceneManager.SetLastBallFallInNet (2);
sceneManager.player2.ballInThisTurn.Add (other.name);
sceneManager.Player2Win ();
other.gameObject.SetActive (false);
}
else {
sceneManager.resetBlackBall = true;
SceneManager.changePlayer = true;
other.gameObject.SetActive (false);
}
}