Unity UI管理

先说理一下思路:首先需要一个UIBase,管理UI的生命周期;需要一个UIManager,用于操作UI;需要一个事件管理EventListener,控制点击、拖拽等事件。

UIBase:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class UIBase : MonoBehaviour {

    private bool isInit = false;

    //创建UI
    public virtual void OnCreate()
    {
        isInit = true;
        transform.SetParent(UIManager.Intance.UIParent);
        Canvas canvas = GetComponent();
        canvas.worldCamera = UIManager.Intance.UICamera;
        SetSortOrder();
        canvas.planeDistance = 1;
        transform.localScale = Vector3.one;
        transform.localRotation = Quaternion.identity;
        transform.localPosition = Vector3.zero;
        gameObject.layer = LayerMask.NameToLayer("UI");
    }
    //开启UI
    public virtual void OnShow(params object[] objects)
    {
        if (!isInit)
            OnCreate();
        SetSortOrder();
        gameObject.SetActive(true);
    }
    //隐藏UI
    public virtual void OnHide()
    {
        gameObject.SetActive(false);
    }

    //销毁UI
    public virtual void OnRelease()
    {
        GameObject.Destroy(gameObject);
        isInit = false;
    }
    //获取层级
    public int GetSortOrder()
    {
        return gameObject.GetComponent().sortingOrder;
    }

    public virtual bool IsHideFrontPage()
    {
        return true;
    }
    //设置层级
    private void SetSortOrder()
    {
        Canvas canvas = GetComponent();
        int zValueIndex = UIManager.Intance.GetZIndex();
        canvas.sortingOrder = zValueIndex;
    }
}

UIManager:(为了方便使用继承单例) 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : Singleton {

    private Transform uiParent = null;
    public Transform UIParent
    {
        get { return uiParent; }
    }

    private Camera uiCamera = null;
    public Camera UICamera
    {
        get { return uiCamera; }
    }

    /// 
    /// 存放UI缓冲
    /// 
    private Dictionary viewCache = new Dictionary();

    //UI开启接口
    public void OpenView(string uiName, params object[] objs)
    {
        UIBase newView = null;

        if (viewCache.ContainsKey(uiName) && null != viewCache[uiName])
        {
            
        }
        else
        {
            
        }
    }

    //隐藏UI接口
    public void HideView(string uiName, UIBase ui = null)
    {

    }

    //关闭UI接口
    public void CloseView(string uiName, UIBase ui = null)
    {

    }

    //获取层级
    public int GetZIndex()
    {
        return UISort;
    }

    //创建UI
    public GameObject CreateUI(string name)
    {
        string allPath = "Assets/ui/" + name + ".prefab";
        UnityEngine.Object prefabObj = AssetManager.Instance.LoadAsset(typeof(UnityEngine.Object), allPath) as UnityEngine.Object;
        GameObject ui = GameObject.Instantiate(prefabObj) as GameObject;
        ui.name = name;
        return ui;
    }
}

EventListener:

public class EventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler 
    {
        private static List staticEveList = null;
        private static List respondEveList = null;


        private bool mIsDrag_ = false;
        public Action onClick { get; set; }
        public Action onDragBegin { get; set; }
        public Action onDragEnd { get; set; }
        public Action onDrag { get; set; }
        public Action onEnter { get; set; }
        public Action onClickDown { get; set; }
        public Action onClickUp { get; set; }


        public void OnPointerClick(PointerEventData eventData)
        {
            
        }

        public static EventListener get(GameObject ob)
        {
            if (null == ob)
                return null;
            var ins = ob.GetComponent();
            if (null == ins)
                ins = ob.AddComponent();
            return ins;
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            
        }

        public void OnDrag(PointerEventData eventData)
        {
            
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            
        }

 
}   }

管理还可以更加的细化,例如按钮的使用频率如果很高,则可以添加一个button的管理等等。

你可能感兴趣的:(Unity归纳总结)