#define FUNCELL_MODIFIED using Thousandto.Core.Base; using System.Collections.Generic; using UnityEngine; namespace Thousandto.Plugins.Common { //动画模块 public class UIAnimationModule { #region//私有变量 //动画列表 private Dictionary _animTable = new Dictionary(); // private bool _isUpdatedAnchors = false; //所属trans private Transform TransformInst = null; //缓存 private List _showTmpList = new List(); private List _hideTmpList = new List(); //子节点 private List _childList = new List(); #endregion #region//构造函数 public UIAnimationModule(Transform inst) { TransformInst = inst; } #endregion #region//公有函数 //添加子节点 public void AddChild(UIAnimationModule child) { if (child == null) return; if (child == this) return; if (_childList.Contains(child)) return; _childList.Add(child); } //删除子节点 public void RemoveChild(UIAnimationModule child) { _childList.Remove(child); } //从新刷新位移动画 public void RefreshAllPosAnimation() { var iter = _animTable.GetEnumerator(); try { while(iter.MoveNext()) { var trans = iter.Current.Value.HandleTrans; var widget = trans.GetComponent(); if(widget != null && widget.isAnchored) { //有锚点的才执行 var showAnimList = iter.Current.Value.ShowAnimList; for (int i = 0; i < showAnimList.Count; ++i) { if (showAnimList[i] is UIPositionAnimation) { var posAnim = showAnimList[i] as UIPositionAnimation; posAnim.StartValue = trans.localPosition + posAnim.OffsetPos; posAnim.EndValue = trans.localPosition; } } var hideAnimList = iter.Current.Value.HideAnimList; for (int i = 0; i < hideAnimList.Count; ++i) { if (hideAnimList[i] is UIPositionAnimation) { var posAnim = hideAnimList[i] as UIPositionAnimation; posAnim.StartValue = trans.localPosition; posAnim.EndValue = trans.localPosition + posAnim.OffsetPos; } } } } } finally { iter.Dispose(); } } //删除动画 public void RemoveTransAnimation(Transform trans) { _animTable.Remove(trans); UIAnimationManager.StopAnimation(trans); } //删除动画 public void RemoveChildTransAnimation(Transform trans) { if (trans == null) return; Transform childTrans = null; for (int i = 0; i < trans.childCount; ++i) { childTrans = trans.GetChild(i); _animTable.Remove(childTrans); UIAnimationManager.StopAnimation(childTrans); } } //停止动画 public void StopTransAnimation(Transform trans) { UIAnimationManager.StopAnimation(trans); } public UIFormAnimationData FindAnimationData(Transform trans) { UIFormAnimationData result = null; _animTable.TryGetValue(trans, out result); return result; } public void UpdateAnchor(Transform trans) { if (trans == null) return; UIRect rect = trans.GetComponent(); if (rect != null && rect.isAnchored) { #if FUNCELL_MODIFIED rect.UpdateAnchors(); #else rect.Update(); #endif } for (int i = 0; i < trans.childCount; ++i) { UpdateAnchor(trans.GetChild(i)); } } private void OnAnchorChanged(Transform trans) { UIFormAnimationData data = null; if(_animTable != null && _animTable.TryGetValue(trans, out data)) { //有锚点的才执行 var showAnimList = data.ShowAnimList; for (int i = 0; i < showAnimList.Count; ++i) { if (showAnimList[i] is UIPositionAnimation) { var posAnim = showAnimList[i] as UIPositionAnimation; posAnim.StartValue = trans.localPosition + posAnim.OffsetPos; posAnim.EndValue = trans.localPosition; } } var hideAnimList = data.HideAnimList; for (int i = 0; i < hideAnimList.Count; ++i) { if (hideAnimList[i] is UIPositionAnimation) { var posAnim = hideAnimList[i] as UIPositionAnimation; posAnim.StartValue = trans.localPosition; posAnim.EndValue = trans.localPosition + posAnim.OffsetPos; } } } } //增加默认界面动画 public void AddNormalAnimation(float duration = 0.5f) { var showAnims = new List(); showAnims.Add(UIAnimationManager.Alpha(0f, 1f, duration, null)); showAnims.Add(UIAnimationManager.Scale(new Vector3(0.85f, 0.85f, 1f), Vector3.one, duration, null)); showAnims.Add(UIAnimationManager.Position(Vector3.zero, Vector3.zero, Vector3.zero, duration, null)); var hideAnims = new List(); hideAnims.Add(UIAnimationManager.Alpha(1f, 0f, duration, null)); hideAnims.Add(UIAnimationManager.Position(Vector3.zero, new Vector3(0f, 60f, 0f), new Vector3(0f, 60f, 0f), duration, null)); AddAnimation(TransformInst, showAnims, hideAnims, true, true); } //增加子节点的默认界面动画 public void AddTransNormalAnimation(Transform trans, float posY = 50f, float duration = 0.5f) { if (trans == null) { return; } var oriPos = trans.localPosition; var tarPos = oriPos; tarPos.y = oriPos.y + posY; var showAnims = new List(); showAnims.Add(UIAnimationManager.Alpha(0f, 1f, duration, null)); showAnims.Add(UIAnimationManager.Scale(new Vector3(0.85f, 0.85f, 1f), Vector3.one, duration, null)); showAnims.Add(UIAnimationManager.Position(oriPos, oriPos, Vector3.zero, duration, null)); var hideAnims = new List(); hideAnims.Add(UIAnimationManager.Alpha(1f, 0f, duration, null)); hideAnims.Add(UIAnimationManager.Position(oriPos, tarPos, new Vector3(0f, posY, 0f), duration, null)); AddAnimation(trans, showAnims, hideAnims, false, false); } //添加动画 public void AddAnimation(Transform trans, List showAnims, List hideAnims, bool playOnEnable, bool playOnDisable) { //判断是否是无效动画 if (trans == null) return; if (!_isUpdatedAnchors) { UpdateAnchor(TransformInst); _isUpdatedAnchors = true; } bool needAdd = false; var animData = FindAnimationData(trans); if (animData == null) { animData = new UIFormAnimationData(trans); needAdd = true; } animData.PlayOnEnable = playOnEnable; animData.PlayOnDisable = playOnDisable; for(int i = 0; i < showAnims.Count; ++i) { animData.AddShowAnim(showAnims[i]); } for (int i = 0; i < hideAnims.Count; ++i) { animData.AddHideAnim(hideAnims[i]); } if (animData.HaveAnim && needAdd) { var rect = trans.GetComponent(); if (rect != null && rect.isAnchored) { rect.AnchorChanged = OnAnchorChanged; } _animTable.Add(trans, animData); } } //添加动画 public void AddAnimation(Transform trans, float startAlpha, float endAlpha, Vector3 startScale, Vector3 endScale, Vector3 posOffset, float duration, bool playOnEnable, bool playOnDisable, AnimationCurve curve) { //判断是否是无效动画 if (trans == null || duration <= 0f) return; if (!_isUpdatedAnchors) { UpdateAnchor(TransformInst); _isUpdatedAnchors = true; } _showTmpList.Clear(); _hideTmpList.Clear(); if (startAlpha < 1f || endAlpha < 1f) { var openAlpha = UIAnimationManager.Alpha(startAlpha, endAlpha, duration, curve); _showTmpList.Add(openAlpha); var closeAlpha = UIAnimationManager.Alpha(endAlpha, startAlpha, duration, curve); _hideTmpList.Add(closeAlpha); } if (startScale != Vector3.one || endScale != Vector3.one) { var openScale = UIAnimationManager.Scale(startScale, endScale, duration, curve); _showTmpList.Add(openScale); var closeScale = UIAnimationManager.Scale(endScale, startScale, duration, curve); _hideTmpList.Add(closeScale); } if (posOffset != Vector3.zero) { var openPos = UIAnimationManager.Position(trans.localPosition + posOffset, trans.localPosition, posOffset, duration, curve); _showTmpList.Add(openPos); var closePos = UIAnimationManager.Position(trans.localPosition, trans.localPosition + posOffset, posOffset, duration, curve); _hideTmpList.Add(closePos); } AddAnimation(trans, _showTmpList, _hideTmpList, playOnEnable, playOnDisable); } //添加alpha动画 public void AddAlphaAnimation(Transform trans = null, float startAlpha = 0f, float endAlpha = 1f, float duration = 0.2f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { trans = TransformInst; } AddAnimation(trans, startAlpha, endAlpha, Vector3.one, Vector3.one, Vector3.zero, duration, playOnEnable, playOnDisable, curve); } //添加缩放动画 public void AddScaleAnimation(Transform trans = null, float startScaleX = 0f, float startScaleY = 0f, float endScaleX = 1f, float endScaleY = 1f, float duration = 0.2f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { trans = TransformInst; } AddAnimation(trans, 1f, 1f, new Vector3(startScaleX, startScaleY, 1f), new Vector3(endScaleX, endScaleY, 1f), Vector3.zero, duration, playOnEnable, playOnDisable, curve); } //添加位移动画 public void AddPositionAnimation(float offsetX, float offsetY, Transform trans = null, float duration = 0.2f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { trans = TransformInst; } AddAnimation(trans, 1f, 1f, Vector3.one, Vector3.one, new Vector3(offsetX, offsetY, 0f), duration, playOnEnable, playOnDisable, curve); } //添加alpha和缩放动画 public void AddAlphaScaleAnimation(Transform trans = null, float startAlpha = 0f, float endAlpha = 1f, float startScaleX = 0f, float startScaleY = 0f, float endScaleX = 1f, float endScaleY = 1f, float duration = 0.2f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { trans = TransformInst; } AddAnimation(trans, startAlpha, endAlpha, new Vector3(startScaleX, startScaleY, 1f), new Vector3(endScaleX, endScaleY, 1f), Vector3.zero, duration, playOnEnable, playOnDisable, curve); } //添加alpha和位移动画 public void AddAlphaPosAnimation(Transform trans = null, float startAlpha = 0f, float endAlpha = 1f, float offsetX = 0f, float offsetY = 400f, float duration = 0.2f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { trans = TransformInst; } AddAnimation(trans, startAlpha, endAlpha, Vector3.one, Vector3.one, new Vector3(offsetX, offsetY, 0f), duration, playOnEnable, playOnDisable, curve); } //添加缩放和位移动画 public void AddScalePosAnimtion(Transform trans = null, float startScaleX = 0f, float startScaleY = 0f, float endScaleX = 1f, float endScaleY = 1f, float offsetX = 0f, float offsetY = 400f, float duration = 0.2f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { trans = TransformInst; } AddAnimation(trans, 1f, 1f, new Vector3(startScaleX, startScaleY, 1f), new Vector3(endScaleX, endScaleY, 1f), new Vector3(offsetX, offsetY, 0f), duration, playOnEnable, playOnDisable, curve); } //添加旋转动画 public void AddRotationAnimation(Transform trans = null, float start = 0f, float end = 90f, float duration = 0.2f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { trans = TransformInst; } var animData = FindAnimationData(trans); var needAdd = false; if (animData == null) { animData = new UIFormAnimationData(trans); needAdd = true; } animData.PlayOnEnable = playOnEnable; animData.PlayOnDisable = playOnDisable; var showAnim = UIAnimationManager.Rotaion(start, end, duration, curve); animData.AddShowAnim(showAnim); var hideAnim = UIAnimationManager.Rotaion(end, start, duration, curve); animData.AddHideAnim(hideAnim); if (needAdd) { _animTable.Add(trans, animData); } } //添加alpha动画 public void AddChildAlphaAnimation(Transform trans, float startAlpha = 0f, float endAlpha = 1f, float duration = 0.2f, float durationOffset = 0.1f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { return; } for (int i = 0; i < trans.childCount; ++i) { AddAnimation(trans.GetChild(i), startAlpha, endAlpha, Vector3.one, Vector3.one, Vector3.zero, duration + durationOffset * i, playOnEnable, playOnDisable, curve); } } //添加缩放动画 public void AddChildScaleAnimation(Transform trans, float startScaleX = 0f, float startScaleY = 0f, float endScaleX = 1f, float endScaleY = 1f, float duration = 0.2f, float durationOffset = 0.1f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { return; } for (int i = 0; i < trans.childCount; ++i) { AddAnimation(trans.GetChild(i), 1f, 1f, new Vector3(startScaleX, startScaleY, 1f), new Vector3(endScaleX, endScaleY, 1f), Vector3.zero, duration + durationOffset * i, playOnEnable, playOnDisable, curve); } } //添加位移动画 public void AddChildPositionAnimation(float offsetX, float offsetY, Transform trans = null, float duration = 0.2f, float durationOffset = 0.1f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { return; } for (int i = 0; i < trans.childCount; ++i) { AddAnimation(trans.GetChild(i), 1f, 1f, Vector3.one, Vector3.one, new Vector3(offsetX, offsetY, 0f), duration + durationOffset * i, playOnEnable, playOnDisable, curve); } } //添加alpha和缩放动画 public void AddChildAlphaScaleAnimation(Transform trans = null, float startAlpha = 0f, float endAlpha = 1f, float startScaleX = 0f, float startScaleY = 0f, float endScaleX = 1f, float endScaleY = 1f, float duration = 0.2f, float durationOffset = 0.1f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { return; } for (int i = 0; i < trans.childCount; ++i) { AddAnimation(trans.GetChild(i), startAlpha, endAlpha, new Vector3(startScaleX, startScaleY, 1f), new Vector3(endScaleX, endScaleY, 1f), Vector3.zero, duration + durationOffset * i, playOnEnable, playOnDisable, curve); } } //添加alpha和位移动画 public void AddChildAlphaPosAnimation(Transform trans = null, float startAlpha = 0f, float endAlpha = 1f, float offsetX = 0f, float offsetY = 400f, float duration = 0.2f, float durationOffset = 0.1f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { return; } for (int i = 0; i < trans.childCount; ++i) { AddAnimation(trans.GetChild(i), startAlpha, endAlpha, Vector3.one, Vector3.one, new Vector3(offsetX, offsetY, 0f), duration + durationOffset * i, playOnEnable, playOnDisable, curve); } } //添加缩放和位移动画 public void AddChildScalePosAnimtion(Transform trans = null, float startScaleX = 0f, float startScaleY = 0f, float endScaleX = 1f, float endScaleY = 1f, float offsetX = 0f, float offsetY = 400f, float duration = 0.2f, float durationOffset = 0.1f, bool playOnEnable = true, bool playOnDisable = true, AnimationCurve curve = null) { if (trans == null) { return; } for (int i = 0; i < trans.childCount; ++i) { AddAnimation(trans.GetChild(i), 1f, 1f, new Vector3(startScaleX, startScaleY, 1f), new Vector3(endScaleX, endScaleY, 1f), new Vector3(offsetX, offsetY, 0f), duration + durationOffset * i, playOnEnable, playOnDisable, curve); } } //播放所有开启动画 public void PlayEnableAnimation(MyAction callBack = null) { var iter = _animTable.GetEnumerator(); try { while (iter.MoveNext()) { UIAnimationManager.StopAnimation(iter.Current.Value.HandleTrans); if (iter.Current.Value.PlayOnEnable) { UIAnimationManager.PlayShowAnimation(iter.Current.Value.HandleTrans, callBack, iter.Current.Value.ShowAnimList); callBack = null; } } } finally { iter.Dispose(); } if (callBack != null) { callBack(); } } //播放所有关闭动画 public void PlayDisableAnimation(MyAction callBack = null, bool hideGo = true) { var iter = _animTable.GetEnumerator(); try { while (iter.MoveNext()) { UIAnimationManager.StopAnimation(iter.Current.Value.HandleTrans); if (iter.Current.Value.PlayOnDisable) { UIAnimationManager.PlayHideAnimAtion(iter.Current.Value.HandleTrans, callBack, iter.Current.Value.HideAnimList, hideGo); callBack = null; } } } finally { iter.Dispose(); } if (callBack != null) { callBack(); } } //播放某个节点的开启动画 public void PlayShowAnimation(Transform trans, MyAction finishCallBack = null) { var animData = FindAnimationData(trans); if (animData != null) { UIAnimationManager.PlayShowAnimation(trans, finishCallBack, animData.ShowAnimList); } } //播放某个节点的关闭动画 public void PlayHideAnimation(Transform trans, MyAction finishCallBack = null, bool hideGo = true) { var animData = FindAnimationData(trans); if (animData != null) { UIAnimationManager.PlayHideAnimAtion(trans, finishCallBack, animData.HideAnimList, hideGo); } } //播放某个节点的子节点开启动画 public void PlayChildShowAnimation(Transform trans, MyAction finishCallBack = null) { if (trans == null) return; for (int i = 0; i < trans.childCount; ++i) { var child = trans.GetChild(i); if (child.gameObject.activeSelf) PlayShowAnimation(child, finishCallBack); } } //播放某个节点的子节点关闭动画 public void PlayChildHideAnimation(Transform trans, MyAction finishCallBack = null) { if (trans == null) return; for (int i = 0; i < trans.childCount; ++i) { var child = trans.GetChild(i); if (child.gameObject.activeSelf) PlayHideAnimation(child, finishCallBack); } } //子类不能实现此函数 public void OnDestroy() { var iter = _animTable.GetEnumerator(); try { while (iter.MoveNext()) { var trans = iter.Current.Key; var rect = trans.GetComponent(); if(rect != null) { rect.AnchorChanged = null; } //将动画从管理器中移除 UIAnimationManager.StopAnimation(iter.Current.Value.HandleTrans); } } finally { iter.Dispose(); _animTable.Clear(); } for(int i = 0; i < _childList.Count; ++i) { _childList[i].OnDestroy(); } _childList.Clear(); } #endregion } }