/// <summary> /// 在盘面空位置上随机添加GameCube /// </summary> private void AddCube() { //先获取到所有空位置 List <Node> tempList = new List <Node> (); for (int i = 0; i < mNodes.Length; i++) { if (!mNodes [i].mGameCube) { tempList.Add(mNodes [i]); } } int pos = Random.Range(0, (tempList.Count - 1)); Debug.Log("positon=" + tempList [pos].mPosX + "-" + tempList [pos].mPosY + ",index=" + pos); GameCube cube = Instantiate(mCubePrefab, tempList [pos].transform.position, Quaternion.identity) as GameCube; //乱入数,2或者4 int num = Random.value > 0.4?2:4; if (num == 2) { cube.mValue = 2; cube.GetComponent <SpriteRenderer> ().sprite = cube.mSprites [0]; } else { cube.mValue = 4; cube.GetComponent <SpriteRenderer> ().sprite = cube.mSprites [1]; } cube.mCurrentNode = tempList [pos]; tempList [pos].mGameCube = cube; }