Unity 堆栈

using UnityEngine;
using System.Collections;

//堆栈
public class TestStack : MonoBehaviour {

    public delegate void DeleStackHandle(object senter,QueueEventArgs e);

    public event DeleStackHandle EventStack;

    Stack st;

    Stack St
    {
        get
        {
            return st;
        }
        set
        {
            st = value;
        }
    }

    void OnStackEvent(object sent,QueueEventArgs e)
    {
        if(e.count>0)
        {
            Debug.Log("堆栈中的数量不为空:" + e.count);
        }
        else
        {
            Debug.Log("堆栈中的元素为空");
        }
    }

    void Start()
    {
        st = new Stack();
        EventStack += new DeleStackHandle(OnStackEvent);

        st.Push(1);
        st.Push(2);
        st.Push(3);
     
    }

    void OnListentEventStack(int count,string con)
    {
        if(EventStack!=null)
        {
            EventStack(this, new QueueEventArgs(count, con));
        }
    }

    void OnGUI()
    {
        if(GUI.Button(new Rect(300,300,100,100),"显示堆栈"))
        {
            Debug.Log("显示堆栈中的元素");
            ShowSt();
            OnListentEventStack(st.Count, st.ToString());
        }
        if (GUI.Button(new Rect(400, 300, 100, 100), "删除堆栈"))
        {
            Debug.Log("删除堆栈中的元素");
            PopSt();
            OnListentEventStack(st.Count, st.ToString());
        }
    }

    //显示堆栈
    void ShowSt()
    {
        foreach(var temp in st)
        {
            Debug.Log(temp);
        }
    }

    void PopSt()
    {
        st.Pop();
    } 
}
public class QueueEventArgs : MonoBehaviour { 
    public int count;
    public string  context;
    public QueueEventArgs(int cou,string con)
    {
        count = cou;
        context = con;
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容