| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 | using System;using System.Collections.Generic;using Fort23.Core;using Fort23.UTool;using UnityEngine;using Object = UnityEngine.Object;namespace Fort23.Mono{    /// <summary>    /// 打开一个UI的流程是:    /// 1.生成Entity脚本    /// 2.通过Init方法获取到数据    /// 3.如果数据获取失败了,则界面打开失败,直接调用Entity的Dispose方法销毁;如果数据获取成功则调用Show展示界面。    /// </summary>    public class UIBase : Entity    {        private RectTransform _transform;        public RectTransform transform => _transform;        public GameObject GObjectPoolInterface;        /// <summary>        /// 序列化数据,用于拖动使用        /// </summary>        protected ReferenceCollector _referenceCollector;        protected bool _isShow;        /// <summary>        /// 标记是否打开状态        /// </summary>        public bool isShow        {            get { return _isShow; }            set { _isShow = value; }        }        /// <summary>        /// 是否添加了button事件        /// </summary>        private bool _isAddButtonEvent;        /// <summary>        /// 是否启用动画        /// </summary>        public bool isActiveAnima;        public Animator Animator;        /// <summary>        /// 结束动画的长度        /// </summary>        protected long _closeAnimationTimeCount;        /// <summary>        /// 开始动画的长度        /// </summary>        protected long _openAnimationTimeCount;        /// <summary>        /// 是否展示完成        /// </summary>        public bool IsShowComplete = true;        public CTask hideCtask;                        protected CTask showcCTask;        /// <summary>        /// 这个方法可以用来替代Awake,直接重写就可以不注册Awake系统了。        /// </summary>        /// <param name="gObjectPoolInterface"></param>        public virtual async CTask SetUIGameObject(GameObject gObjectPoolInterface)        {            GObjectPoolInterface = gObjectPoolInterface;            _transform = gObjectPoolInterface.GetComponent<RectTransform>();                      _referenceCollector = gObjectPoolInterface.GetComponent<ReferenceCollector>();            Animator = gObjectPoolInterface.GetComponent<Animator>();            // this.isActiveAnima = isActiveAnima;            if (!_isAddButtonEvent)            {                AddButtonEvent();                _isAddButtonEvent = true;            }            if (Animator != null && Animator.runtimeAnimatorController != null )            {                Animator.updateMode = AnimatorUpdateMode.UnscaledTime;                foreach (var animatorClip in Animator.runtimeAnimatorController.animationClips)                {                    if (animatorClip.name.Contains("close") || animatorClip.name.Contains("Close"))                    {                        _closeAnimationTimeCount = (long)(animatorClip.length * 1000);                    }                    if (animatorClip.name.Contains("open") ||  animatorClip.name.Contains("Open"))                    {                        _openAnimationTimeCount = (long)(animatorClip.length * 1000);                    }                }            }        }        /// <summary>        /// UI一次打开的时候调用一次,后面就不会再调用        /// </summary>        public virtual void AddButtonEvent()        {        }        public override bool IsUpdate()        {            if (GObjectPoolInterface == null)            {                return false;            }            return GObjectPoolInterface.activeInHierarchy;        }        /// <summary>        /// 获取UI拖动的部件信息        /// </summary>        /// <param name="key"></param>        /// <typeparam name="T"></typeparam>        /// <returns></returns>        public T GetUIUnit<T>(string key) where T : class        {            return _referenceCollector.Get<T>(key);        }        /// <summary>        /// 初始化相关数据(针对内部请求初始化) 可使用异步等待 与自动化Init 区分开        /// 先初始化数据,然后再打开界面,可以避免某些界面先打开后才被初始化        /// </summary>        /// <returns></returns>        public virtual async CTask<bool> AsyncInit(object[] uiData)        {            try            {                CTask<bool> cTask = CTask<bool>.Create();                cTask.SetResult(true);                return await cTask;            }            catch (Exception e)            {                LogTool.Exception(e);            }            return default;        }        /// <summary>        /// 打开界面        /// </summary>        public virtual async CTask Open()        {            await Show();        }        public virtual  CTask Show()        {            if (GObjectPoolInterface == null)            {                return null;            }            CTask cTask = CTask.Create();            _isShow = true;            GObjectPoolInterface.SetActive(true);            if (timerEntity != null)            {                TimerComponent.Instance.Remove(timerEntity);            }            if (openTimerEntity != null)            {                TimerComponent.Instance.Remove(openTimerEntity);            }            ShowAnimator();            cTask.SetResult();            return cTask;        }        public virtual void ShowAnimator()        {            showcCTask = CTask.Create(false);            TimerComponent.Instance.Remove(openTimerEntity);            if (Animator != null && isActiveAnima)            {                if (_openAnimationTimeCount > 0)                {                    Animator.Play("open", 0, 0.01f);                    Animator.Update(0.01f);                    openTimerEntity = TimerComponent.Instance.AddTimer(_openAnimationTimeCount, delegate                    {                        showcCTask.SetResult();                    });                }                else                {                    showcCTask.SetResult();                }            }            else            {                showcCTask.SetResult();            }        }        protected TimerEntity timerEntity;        protected TimerEntity openTimerEntity;        //等大show动画播放完毕        public async CTask WaitShowAnimOver()        {            await showcCTask;        }                /// <summary>        /// 隐藏界面        /// </summary>        public virtual async void Hide()        {            hideCtask = CTask.Create(false);            _isShow = false;            if (Animator != null && isActiveAnima)            {                Animator.Play("close");                if (timerEntity != null)                {                    TimerComponent.Instance.Remove(timerEntity);                }                timerEntity = TimerComponent.Instance.AddTimer(_closeAnimationTimeCount,                    () =>                    {                        if (GObjectPoolInterface != null)                        {                            GObjectPoolInterface.SetActive(false);                        }                                             hideCtask.SetResult();                    });            }            else            {                if (GObjectPoolInterface != null)                {                    GObjectPoolInterface.SetActive(false);                }                hideCtask.SetResult();            }        }        public async CTask PlayAnim(string anmiName)        {            CTask cTask = CTask.Create();            long animaTimeCount = 0;            if (Animator != null && Animator.runtimeAnimatorController != null && isActiveAnima == true)            {                Animator.updateMode = AnimatorUpdateMode.UnscaledTime;                foreach (var animatorClip in Animator.runtimeAnimatorController.animationClips)                {                    if (animatorClip.name.Contains(anmiName))                    {                        animaTimeCount = (long)(animatorClip.length * 1000);                    }                }            }            Animator.Play(anmiName);            TimerComponent.Instance.AddTimer(animaTimeCount,                () => { cTask?.SetResult(); });            await cTask;        }        public override async void Dispose()        {            if (GObjectPoolInterface != null)            {                OnDestroy();                GameObject.Destroy(GObjectPoolInterface);                GObjectPoolInterface = null;            }            base.Dispose();        }        protected virtual void OnDestroy()        {        }    }}
 |