老师,我比较严格的按照你的代码写了一遍,结果只生成了4个card,大概会是什么错误?谢谢!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Deck : MonoBehaviour
{
public static Deck Instance { get; set; }
public int cardWidth=62;
public int cardHeight=86;
public GameObject cardGo;
private int row=7;//行
private int col=7;//列
private int layer=8;//层
private List<Card> cards=new List<Card>();
public RectTransform deckTrans;//需要生成到卡牌的引用
public Transform[] pickDeckPos;
public RectTransform centerDeckTrans;//中间牌堆的基础位置
public Transform[] pickDeckPosTrans;
//[SerializeField]
private int[,,] centerDeck = new int[,,] {
//一维是层
//二维是行,三维是列
{
//最下面一层
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
},
{
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
},
{
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
},
{
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
},
{
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
},
{
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
},
{
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
},
{
//最上面一层
{1,1,1,1 },
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1},
{ 1,1,1,1}
}
};//层 行 列
private void Awake()
{
Instance = this;
}
void Start()
{
//层
for (int k=0;k<layer;k++)
{
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
//随机当前列是否偏移
bool ifMoveX = Convert.ToBoolean(UnityEngine.Random.Range(0,2));
int dirX = 0;
if (ifMoveX)
{
dirX = UnityEngine.Random.Range(0, 2)==0?1:-1;
}
//随机当前行是否偏移
bool ifMoveY = Convert.ToBoolean(UnityEngine.Random.Range(0, 2));
int dirY = 0;
if (ifMoveY)
{
dirY = UnityEngine.Random.Range(0, 2) == 0 ? 1 : -1;
}
GameObject go = null;
CreateState cs= (CreateState)centerDeck[k,j,i];
switch (cs)
{
case CreateState.None:
break;
case CreateState.Create:
go = CreateCardGo(i, j, dirX, dirY);
break;
case CreateState.Random:
if (UnityEngine.Random.Range(0,2)==0?true:false)
{
//随机生成
go = CreateCardGo(i,j,dirX,dirY);
}
break;
case CreateState.OnlyCreate:
go = CreateCardGo(i, j, 0, 0);
break;
default:
break;
}
if (go)
{
Card card = go.GetComponent<Card>();
card.SetCardSprite();
SetCoverState(card);
cards.Add(card);
go.name = "I:" + i.ToString() + " J:" + j.ToString() + " K:" + k.ToString();
}
}
}
}
}
// Update is called once per frame
void Update()
{
}
//产生卡牌游戏物体
private GameObject CreateCardGo(int col,int row,int dirX,int dirY)
{
GameObject go= Instantiate(cardGo,deckTrans);
go.GetComponent<RectTransform>().anchoredPosition = centerDeckTrans.anchoredPosition + new Vector2(cardWidth * (col+0.5f*dirX), -cardHeight * (row+0.5f*dirY));
return go;
}
private void SetCoverState(Card card)
{
for (int i=0;i<cards.Count;i++)
{
card.SetCoverCardState(cards[i]);
}
}
public Transform GetEmptyPickDeckTargetTrans()
{
}
}