123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using UnityEngine;
- using System.Collections;
- using DG.Tweening;
- using System.Collections.Generic;
- /// <summary>
- /// UI的doTween插件,搞一个UI的弹出或者效果
- /// </summary>
- public class UIDoTweenAnimation : MonoBehaviour
- {
- /// <summary>
- ///
- /// </summary>
- private RectTransform panelTransform;
- /// <summary>
- /// 动画
- /// </summary>
- private Tweener tweener = null;
- /// <summary>
- /// 特效种类,0就是从下往上,1是从左往右,2是从右往左.
- /// </summary>
- private int KindOfEffect;
-
- /// <summary>
- ///
- /// </summary>
- public void OnEnable()
- {
- ////开始播放动画
- ////启动动画播放
-
- }
- private void Awake()
- {
- children = new Dictionary<GameObject, Vector3>();
- for (int i = 0; i < this.transform.childCount; i++)
- {
- GameObject go = this.transform.GetChild(i).gameObject;
- children.Add(go, go.transform.localPosition);
- }
- }
- /// <summary>
- ///
- /// </summary>
- private Dictionary<GameObject, Vector3> children = new Dictionary<GameObject, Vector3>();
- /// <summary>
- ///
- /// </summary>
- private void StartPlayDoTween()
- {
- if (tweener != null && tweener.IsPlaying())
- {
- tweener.Kill();
- ResetToDefaultPos();
- }
- for (int i = 0; i < this.transform.childCount; i++)
- {
- GameObject g = this.transform.GetChild(i).gameObject;
- RectTransform panelTransform = g.GetComponent<RectTransform>();
- Vector3 destPos = children[g];
- if (KindOfEffect == 0)
- {
- transform.localPosition = new Vector3(destPos.x, -1920, destPos.z);
- tweener = panelTransform.DOLocalMove(destPos, 0.3f);
- tweener.SetEase(Ease.Linear);//动画曲线
- tweener.SetDelay(i * 0.01f);
- }
- else if (KindOfEffect == 1)
- {
- transform.localPosition = new Vector3((destPos.x - 1080), destPos.y, destPos.z);
- tweener = panelTransform.DOLocalMove(destPos, 0.2f);
- tweener.SetEase(Ease.InOutQuad);//动画曲线
- tweener.SetDelay(i * 0.02f);
- }
- else if (KindOfEffect == 2)
- {
- transform.localPosition = new Vector3((destPos.x + 1080), destPos.y, destPos.z);
- tweener = panelTransform.DOLocalMove(destPos, 0.2f);
- tweener.SetEase(Ease.InOutQuad);//动画曲线
- tweener.SetDelay(i * 0.1f);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public void OnDisable()
- {
- if (tweener != null && tweener.IsPlaying())
- {
- tweener.Kill();
- }
- ResetToDefaultPos();
- }
- /// <summary>
- ///
- /// </summary>
- public void OnDestory()
- {
- panelTransform.DOKill();
- }
- /// <summary>
- /// 设置UI到默认位置
- /// </summary>
- private void ResetToDefaultPos()
- {
- foreach (GameObject g in children.Keys)
- {
- RectTransform tc = g.GetComponent<RectTransform>();
- if (tc != null)
- {
- tc.anchoredPosition3D = children[g];
- }
- }
- }
- }
|