201710032350->unity中ui窗体基本生命周期

来源:互联网 发布:点对点视频软件 编辑:程序博客网 时间:2024/06/07 00:08

目的:根据构建窗体时搭载的基本数据生成窗体

执行窗体的初始化,进入和刷新

关键点

1.初始化

2.进入

3.刷新

4.退出隐藏


详细代码

public class AppBasePages : IOnFormUI
    {
        protected AppBasePages() { }
        protected AppBasePages(UIType type, UIMode mode, string path = "")
        {
            this.type = type;
            this.mode = mode;
            this.path = path;
        }
        public void Show()
        {
            if (this.CacheGameObject == null && this.path.IsNonNullOrEmpty())
            {
                CreateUI();
            }
            OnEnter();
            OnRefresh();
            AdjustDeep();
            CheakPageNodes(this);
        }

        private void AdjustDeep()
        {
            UIPanel[] panels = CacheTransform.TryGetChildComponent<UIPanel>(true);
            Array.ForEach(panels, p =>
            {
                p.depth = p.depth - panels[0].depth + UIDeep;
            });
            panels[0].depth = UIDeep;
            UIDeep += 1;
        }
        private Transform SetUIParent()
        {
            switch (type)
            {
                case UIType.Normal: return AppBootStrap.Instance.NormalRoot;
                case UIType.PopUp: return AppBootStrap.Instance.PopupRoot;
            }
            return null;
        }
        private void CreateUI()
        {
            this.CacheGameObject = GameObject.Instantiate(Resources.Load(path), SetUIParent()) as GameObject;
            this.CacheTransform = this.CacheGameObject.transform;
            OnInitUI();
        }
        public void Close()
        {
            CacheGameObject.SetActiveSafe(false);
            content = null;
            OnExit();
        }
        public virtual void OnEnter()
        {
            CacheGameObject.SetVisible(true);
        }
        public virtual void OnRefresh()
        {
            CacheGameObject.SetActiveSafe(true);
        }
        public virtual void OnExit() { }
        public virtual void OnInitUI() { }

        private UIType type;
        private UIMode mode;
        private string path;
        protected object content;
        protected GameObject CacheGameObject;
        protected Transform CacheTransform;

   }