我是Unity新手,我正在尝试在Card堆栈中实现滑动手势,已经设法通过使用预制脚本编写脚本来创建堆栈,该堆栈是动态的,最多可以容纳220张卡。
问题是手势,当我从堆栈正面向左滑动时,该卡应从堆栈中取出,而另一张卡应添加到堆栈的背面,总共总共只能显示 4张卡。
代码段1 ,我在其中创建我的游戏对象并将其设置为有效/无效
public void SetCardNumber ()
{
Positions = new Vector3[4];
CardScales = new Vector3[4];
Positions [0] = new Vector3 (Screen.width / 2, (Screen.height / 2) + 135f, 0f);
Positions [1] = new Vector3 (Screen.width / 2, (Screen.height / 2) + 90f, 0f);
Positions [2] = new Vector3 (Screen.width / 2, (Screen.height / 2) + 45f, 0f);
Positions [3] = new Vector3 (Screen.width / 2, Screen.height / 2, 0f);
CardScales [0] = new Vector3 (0.85f, 0.85f, 0f);
CardScales [1] = new Vector3 (0.9f, 0.9f, 0f);
CardScales [2] = new Vector3 (0.95f, 0.95f, 0f);
CardScales [3] = new Vector3 (1f, 1f, 0f);
InActivePositions = new Vector3 (0f, 0f, 0f);
InActiveCardScales = new Vector3 (1f, 1f, 0f);
Cards = new GameObject[words.Length];
CardNumber = new GameObject[words.Length];
CardText = new GameObject[words.Length];
ShowSentenceBulb = new GameObject[words.Length];
for (int i = 0, j = words.Length; i <= words.Length - 1; i++,j--) {
GameObject obj = (GameObject)Instantiate (CardPrefab) as GameObject;
Cards [i] = obj;
CurrentCardCounter++;
Cards [i].transform.SetParent (panel.transform, false);
foreach (Transform child in obj.transform) {
if (child.tag == "TextCardNumber")
CardNumber [i] = child.gameObject;
if (child.tag == "TextCard")
CardText [i] = child.gameObject;
if (child.tag == "ShowSentenceBulb")
ShowSentenceBulb [i] = child.gameObject;
}
if (i == 0) {
CardText [i].GetComponent<Text> ().text = CurrentWordList [j - 1];
CardText [i].GetComponent<Text> ().fontSize = 70;
CardText [i].GetComponent<Text> ().color = new Color (255, 215, 0);
} else
CardText [i].GetComponent<Text> ().text = CurrentWordList [j - 1];
CardNumber [i].GetComponent<Text> ().text = j + "";
ShowSentenceBulb [i].GetComponent<Button> ().onClick.AddListener (ShowSentenceOnBulbClick);
}
var quotient = CurrentCardCounter / 4;
for (int k = 3; CurrentCardCounter > 0; CurrentCardCounter--,k--) {
if (CurrentCardCounter <= (4 * (quotient - 1))) {
Cards [CurrentCardCounter - 1].SetActive (false);
}
else {
Cards [CurrentCardCounter-1].transform.position = Positions [k];
Cards [CurrentCardCounter-1].transform.localScale = CardScales [k];
Cards [CurrentCardCounter-1].SetActive (true);
}
}
CurrentCardCounter=words.Length-1;
swipeAnim= Cards[CurrentCardCounter].GetComponent<Animator>();
}
我处理此问题的方式是在场景开始时创建所有gameObject,但仅使4个处于活动状态,每当我向左滑动时,都会将一张新卡设置为“活动”。
代码段2 :向左滑动手势从堆栈中移除正面卡
if (swipeLeft && CurrentCardCounter > 0 && !isCardShowingSentence) {
swipeAnim.enabled = true;
swipeAnim.Play ("swipe");
if (CurrentCardCounter > 0) {
CurrentCardCounter--;
swipeAnim = Cards [CurrentCardCounter].GetComponent<Animator> ();
if (CurrentCardCounter > 3) {
Cards [CurrentCardCounter].transform.position = Positions [3];
Cards [CurrentCardCounter].transform.localScale = CardScales [3];
Cards [CurrentCardCounter - 1].transform.position = Positions [2];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
Cards [CurrentCardCounter - 2].transform.position = Positions [1];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales [1];
Cards [CurrentCardCounter - 3].SetActive (true);
Cards [CurrentCardCounter - 3].transform.position = Positions [0];
Cards [CurrentCardCounter - 3].transform.localScale = CardScales [0];
} else if (CurrentCardCounter > 2) {
Cards [CurrentCardCounter].transform.position = Positions [3];
Cards [CurrentCardCounter].transform.localScale = CardScales [3];
Cards [CurrentCardCounter - 1].transform.position = Positions [2];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
Cards [CurrentCardCounter - 2].transform.position = Positions [1];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales [1];
Cards [CurrentCardCounter - 3].SetActive (true);
Cards [CurrentCardCounter - 3].transform.position = Positions [0];
Cards [CurrentCardCounter - 3].transform.localScale = CardScales [0];
} else if (CurrentCardCounter > 1) {
Cards [CurrentCardCounter].transform.position = Positions [3];
Cards [CurrentCardCounter].transform.localScale = CardScales [3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
Cards [CurrentCardCounter - 1].transform.position = Positions [2];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales [1];
Cards [CurrentCardCounter - 2].transform.position = Positions [1];
} else if (CurrentCardCounter > 0) {
Cards [CurrentCardCounter].transform.localScale = CardScales [3];
Cards [CurrentCardCounter].transform.position = Positions [3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
Cards [CurrentCardCounter - 1].transform.position = Positions [2];
}
} else {
//swipeAnim = Cards [0].GetComponent<Animator> ();
//swipeAnim.enabled = true;
//swipeAnim.Play ("shake");
//swipeAnim.enabled = false;
}
}
这一切都得到了很好的处理,但是还有一个正确的滑动手势(要取回被刷过的卡,将被刷过的卡添加到堆栈的正面,然后将添加到背面的卡再次设为无效)弄乱了游戏对象的位置,我不知道为什么。
代码段3 :向右滑动手势可将刷回的卡取回并从堆栈中取出背面的卡
if (swipeRight && CurrentCardCounter < words.Length - 1 && !isCardShowingSentence) {
if (CurrentCardCounter < words.Length) {
swipeAnim = Cards [CurrentCardCounter+1].GetComponent<Animator> ();
swipeAnim.enabled = true;
swipeAnim.Play ("swipeBack");
CurrentCardCounter++;
}
if (CurrentCardCounter > 6) {
Cards [CurrentCardCounter - 1].transform.position = Positions[3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
Cards [CurrentCardCounter - 2].transform.position = Positions[2];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
Cards [CurrentCardCounter - 3].transform.position = Positions[1];
Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
Cards [CurrentCardCounter - 4].SetActive (false);
Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
}else if (CurrentCardCounter > 5) {
Cards [CurrentCardCounter - 1].transform.position = Positions[3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
Cards [CurrentCardCounter - 2].transform.position = Positions[2];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
Cards [CurrentCardCounter - 3].transform.position = Positions[1];
Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
Cards [CurrentCardCounter - 4].SetActive (false);
Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
}else if (CurrentCardCounter > 4) {
Cards [CurrentCardCounter - 1].transform.position = Positions[3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
Cards [CurrentCardCounter - 2].transform.position = Positions[2];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
Cards [CurrentCardCounter - 3].transform.position = Positions[1];
Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
Cards [CurrentCardCounter - 4].SetActive (false);
Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
} else if (CurrentCardCounter > 3) {
Cards [CurrentCardCounter - 1].transform.position = Positions[3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
Cards [CurrentCardCounter - 2].transform.position = Positions[2];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
Cards [CurrentCardCounter - 3].transform.position = Positions[1];
Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
Cards [CurrentCardCounter - 4].SetActive (false);
Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
} else if (CurrentCardCounter > 2) {
Cards [CurrentCardCounter - 1].transform.position = Positions[3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
Cards [CurrentCardCounter - 2].transform.position = Positions[2];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
Cards [CurrentCardCounter - 3].transform.position = Positions[1];
Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
} else if (CurrentCardCounter > 1) {
Cards [CurrentCardCounter - 1].transform.position = Positions[3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
Cards [CurrentCardCounter - 2].transform.position = Positions[2];
Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
} else if (CurrentCardCounter > 0) {
Cards [CurrentCardCounter - 1].transform.position = Positions[3];
Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
}
}
这个问题让我头疼,我不知道要多长时间,任何帮助将不胜感激