UIDoTweenAnimation.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using UnityEngine;
  2. using System.Collections;
  3. using DG.Tweening;
  4. using System.Collections.Generic;
  5. /// <summary>
  6. /// UI的doTween插件,搞一个UI的弹出或者效果
  7. /// </summary>
  8. public class UIDoTweenAnimation : MonoBehaviour
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. private RectTransform panelTransform;
  14. /// <summary>
  15. /// 动画
  16. /// </summary>
  17. private Tweener tweener = null;
  18. /// <summary>
  19. /// 特效种类,0就是从下往上,1是从左往右,2是从右往左.
  20. /// </summary>
  21. private int KindOfEffect;
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. public void OnEnable()
  26. {
  27. ////开始播放动画
  28. ////启动动画播放
  29. }
  30. private void Awake()
  31. {
  32. children = new Dictionary<GameObject, Vector3>();
  33. for (int i = 0; i < this.transform.childCount; i++)
  34. {
  35. GameObject go = this.transform.GetChild(i).gameObject;
  36. children.Add(go, go.transform.localPosition);
  37. }
  38. }
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. private Dictionary<GameObject, Vector3> children = new Dictionary<GameObject, Vector3>();
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. private void StartPlayDoTween()
  47. {
  48. if (tweener != null && tweener.IsPlaying())
  49. {
  50. tweener.Kill();
  51. ResetToDefaultPos();
  52. }
  53. for (int i = 0; i < this.transform.childCount; i++)
  54. {
  55. GameObject g = this.transform.GetChild(i).gameObject;
  56. RectTransform panelTransform = g.GetComponent<RectTransform>();
  57. Vector3 destPos = children[g];
  58. if (KindOfEffect == 0)
  59. {
  60. transform.localPosition = new Vector3(destPos.x, -1920, destPos.z);
  61. tweener = panelTransform.DOLocalMove(destPos, 0.3f);
  62. tweener.SetEase(Ease.Linear);//动画曲线
  63. tweener.SetDelay(i * 0.01f);
  64. }
  65. else if (KindOfEffect == 1)
  66. {
  67. transform.localPosition = new Vector3((destPos.x - 1080), destPos.y, destPos.z);
  68. tweener = panelTransform.DOLocalMove(destPos, 0.2f);
  69. tweener.SetEase(Ease.InOutQuad);//动画曲线
  70. tweener.SetDelay(i * 0.02f);
  71. }
  72. else if (KindOfEffect == 2)
  73. {
  74. transform.localPosition = new Vector3((destPos.x + 1080), destPos.y, destPos.z);
  75. tweener = panelTransform.DOLocalMove(destPos, 0.2f);
  76. tweener.SetEase(Ease.InOutQuad);//动画曲线
  77. tweener.SetDelay(i * 0.1f);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. ///
  83. /// </summary>
  84. public void OnDisable()
  85. {
  86. if (tweener != null && tweener.IsPlaying())
  87. {
  88. tweener.Kill();
  89. }
  90. ResetToDefaultPos();
  91. }
  92. /// <summary>
  93. ///
  94. /// </summary>
  95. public void OnDestory()
  96. {
  97. panelTransform.DOKill();
  98. }
  99. /// <summary>
  100. /// 设置UI到默认位置
  101. /// </summary>
  102. private void ResetToDefaultPos()
  103. {
  104. foreach (GameObject g in children.Keys)
  105. {
  106. RectTransform tc = g.GetComponent<RectTransform>();
  107. if (tc != null)
  108. {
  109. tc.anchoredPosition3D = children[g];
  110. }
  111. }
  112. }
  113. }