Game Development Reference
In-Depth Information
This is why Stack is especially useful for the undo or rewind functionality.
Refer to the following code sample 6-4 for an example on how to use Stack :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//------------------------------------------
[System.Serializable]
public class PlayingCard
{
public string Name;
public int Attack;
public int Defense;
}
//------------------------------------------
public class Using_Stack : MonoBehaviour
{
//------------------------------------------
//Stack of cards
public Stack<PlayingCard> CardStack = new Stack<PlayingCard>();
//------------------------------------------
// Use this for initialization
void Start ()
{
//Create card array
PlayingCard[] Cards = new PlayingCard[5];
//Create cards with sample data
for(int i=0; i<5; i++)
{
Cards[i] = new PlayingCard();
Cards[i].Name = "Card_0" + i.ToString();
Cards[i].Attack = Cards[i].Defense = i * 3;
//Push card onto stack
CardStack.Push(Cards[i]);
}
//Remove cards from stack
while(CardStack.Count > 0)
{
PlayingCard PickedCard = CardStack.Pop();
//Print name of selected card
 
Search WWH ::




Custom Search