Я новичок в Unity и пытаюсь реализовать жесты смахивания в моем стеке карт, мне удалось создать стек с использованием сценариев с использованием префабов, стек динамический и может содержать до 220 карт.
![enter image description here](https://i.stack.imgur.com/O7M9E.png)
Проблема заключается в жестах, когда я проведу пальцем влево, когда карточка с передней стороны стопки должна быть извлечена из стопки, а другая карта должнабыть добавлен в конец стека, в общей сложности должно быть только 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>();
}
Я справляюсь с этим, создавая все объекты gameObjects в начале сцены, но делая только 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];
}
}
Эта проблема вызывает у меня головную боль, я не знаю, как долго, любаяпомощь будет высоко ценится