做个飞机大战(一)

第一部分先做UI框架吧。

做个飞机大战(一)_第1张图片

分为三层,hall放全屏界面,middle放弹出界面,load放loading界面及log。

ViewBase 基类,定义通用方法。弹出层和loading可直接继承。

public class ViewBase : MonoBehaviour
{
    public object m_object;
    void Awake()
    {
        if (!isInit)
            OnInit();
    }
    private bool isInit = false;
    public virtual void OnInit()
    {
        isInit = true;
    }
    public virtual void SetContent(object obj)
    {
        m_object = obj;
        this.Open();
    }
    public virtual void Open()
    {
        this.gameObject.SetActive(true);
    }
    protected virtual void OnClose()
    {
        this.gameObject.SetActive(false);
    }
    void Update()
    {
        this.OnUpdate();
    }

    public virtual void OnUpdate()
    {

    }
}

全屏界面基类,带堆栈。新界面打开,关闭老界面,老界面压栈,新界面关闭,去栈里取出老界面,出栈显示。

public class HeapViewBase : ViewBase
{
    public override void SetContent(object obj)
    {
        base.SetContent(obj);
    }
    public override void Open()
    {
        base.Open();
        UIControl.Instance.Push(this);
    }
    //隐藏切出栈
    protected override void OnClose()
    {
        Close();
        UIControl.Instance.Pop(this);
    }
    //只隐藏
    public void Close()
    {
        base.OnClose();
    }
}

UI控制器,压栈、出栈和对外的打开页面基础功能

public class UIControl
{
    private static UIControl m_instance;
    public static UIControl Instance
    {
        get
        {
            return m_instance;
        }
        set
        {
            m_instance = value;
        }
    }
    private Stack m_stack = new Stack();
    //private Dictionary all_view = new Dictionary();

    public void OnInit()
    {
        
    }
   
    public void Clear()
    {
        
    }

    public void Open(object obj = null) where T : ViewBase
    {
        var uiType = typeof(T);
        if (all_view.ContainsKey(uiType))
        {
            all_view[uiType].SetContent(obj);
        }
        else
        {
            Debug.LogError(string.Format("不存该该类型{0}", uiType.ToString()));
        }
    }

    public void Push(HeapViewBase view)
    {
        if (m_stack.Count > 0)
        {
            HeapViewBase deforBase = m_stack.Peek();
            if (deforBase != null)
            {
                deforBase.Close();
            }
        }
        m_stack.Push(view);
    }

    public void Pop(HeapViewBase view)
    {
        if (m_stack.Count > 0)
        {
            HeapViewBase viewBase = m_stack.Pop();
            if (viewBase != null && viewBase == view)
            {
                if (m_stack.Count > 0)
                {
                    HeapViewBase beforView = m_stack.Pop();
                    beforView.SetContent(beforView.m_object);
                }
            }
        }
    }
}

 

你可能感兴趣的:(Unity,小游戏)