using System; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using GameCore; using asap.core; public static class DoTweenExtension { public static void Popup(this Transform rec_trans, TweenCallback complete_action = null,bool isUnScale = false) { /*rec_trans.localScale = Vector3.one; rec_trans.DOKill(); var t = rec_trans.DOPunchScale(new Vector3(0.1f, 0.1f, 1), 0.3f);*/ rec_trans.localScale = new Vector3(0.5f, 0.5f, 1);// Vector3.zero; rec_trans.DOKill(); var t = rec_trans.DOScale(1, 0.3f); t.SetEase(Ease.OutBack); if (isUnScale) { t.SetUpdate(true); } if (complete_action != null) { t.OnComplete(complete_action); } } public static void ShrinkDown(this Transform rec_trans, TweenCallback complete_action = null,bool isUnScale = false) { rec_trans.DOKill(); var t = rec_trans.DOScale(0.5f, 0.3f).SetEase(Ease.InBack); if (isUnScale) { t.SetUpdate(true); } if (complete_action != null) { t.OnComplete(complete_action); } } public static void Popup(this Transform t, Image mask, TweenCallback fun = null) { t.localScale = new Vector3(0.5f, 0.5f, 1); t.DOKill(); var x = t.DOScale(1, 0.3f).SetEase(Ease.OutBack).SetUpdate(true); if(mask != null) { mask.DOFade(0f,0f).SetUpdate(true); mask.DOFade(0.9f,0.1f).SetUpdate(true); } if (fun != null) { x.OnComplete(fun); } } public static void ShrinkDown(this Transform t, Image mask, TweenCallback fun = null) { t.DOKill(); var x = t.DOScale(0.5f, 0.3f).SetEase(Ease.InBack).SetUpdate(true); if(mask != null) { mask.DOFade(0f,0.3f).SetUpdate(true); } if (fun != null) { x.OnComplete(fun); } } public static void PopupByCanvasGroup(this Transform rec_trans, TweenCallback complete_action = null) { CanvasGroup cg = rec_trans.GetComponent(); if(cg == null) cg = rec_trans.gameObject.AddComponent(); rec_trans.DOKill(); cg.alpha = 0.2f; rec_trans.localScale = new Vector3(0.33f, 0.33f, 1); // var t = rec_trans.DOScale(1, 0.3f); // t.SetEase(Ease.OutBack); // t.SetUpdate(true); // var tw = cg.DOFade(1f,0.3f); // tw.SetUpdate(true); // if (complete_action != null) // { // t.OnComplete(complete_action); // } } public static void ShrinkDownByCanvasGroup(this Transform rec_trans, TweenCallback complete_action = null) { CanvasGroup cg = rec_trans.GetComponent(); if(cg == null) cg = rec_trans.gameObject.AddComponent(); rec_trans.DOKill(); cg.alpha = 1; rec_trans.localScale = new Vector3(1f, 1f, 1); float f = 1f/60; // UnityEngine.Debug.Log("fff "+f + " " + Application.targetFrameRate); var tw = rec_trans.DOScale(1.1f,f * 11).OnComplete(()=> { var tw1 = rec_trans.DOScale(0.33f,f * 16); var tw2 = cg.DOFade(0f,f*16); tw1.SetUpdate(true); tw2.SetUpdate(true); if (complete_action != null) { tw2.OnComplete(complete_action); } }); tw.SetUpdate(true); } public static void Shake(this RectTransform rec_trans) { rec_trans.localScale = Vector3.one; rec_trans.DOKill(); rec_trans.DOPunchScale(Vector3.one, 0.3f, 5); } public static void Shake(this Transform rec_trans) { rec_trans.localScale = Vector3.one; rec_trans.DOKill(); rec_trans.DOPunchScale(Vector3.one, 0.3f, 5); } public static void FlyingOut(this Text fade_text, Vector3 default_local_pos, Action on_complete = null) { var rec_trans = fade_text.rectTransform; FlyingOutPosition(rec_trans, default_local_pos, on_complete); fade_text.DOFade(0, 1).From(1).SetEase(Ease.InOutSine); } public static void FlyingOut(this CanvasGroup canvas_group, Vector3 default_local_pos, Action on_complete = null) { var rec_trans = canvas_group.GetComponent(); if (rec_trans == null) return; FlyingOutPosition(rec_trans, default_local_pos, on_complete); canvas_group.DOFade(0, 1).From(1).SetEase(Ease.InOutSine); } public static void FlyingOutPosition(RectTransform rec_trans, Vector3 default_local_pos, Action on_complete = null) { rec_trans.gameObject.SetActiveAsNeed(true); rec_trans.DOKill(); var move_from = default_local_pos; var move_to = move_from; move_to.y += 200; var t = rec_trans.DOLocalMove(move_to, 1f).SetEase(Ease.InOutSine).From(move_from); t.OnComplete(() => { rec_trans.gameObject.SetActiveAsNeed(false); rec_trans.localPosition = move_from; if (on_complete != null) { try { on_complete.Invoke(); } catch (Exception ex) { GameDebug.LogError($"[DoTweenExtension]FlyingOutPosition: {ex.Message}"); } } }); } public static Tween WorldMoveTo(this Transform flyobj, Transform target, float duration) { var end_pos = target.position; var tw = flyobj.DOMove(end_pos, duration); if (tw != null) { tw.SetEase(Ease.InOutExpo); } return tw; } public static Tween MoveTo(this Transform move_obj, Transform target, float duration, Ease ease = Ease.InOutExpo) { var target_pos = target.position; return move_obj.MoveTo(target_pos, duration, ease); } public static Tween MoveTo(this Transform move_obj, Vector3 target_local_pos, float duration, Ease ease = Ease.InOutExpo) { var tw = move_obj.DOMove(target_local_pos, duration); if (tw != null) { tw.SetEase(ease); } return tw; } public static Tween LocalMoveTo(this Transform move_obj, Transform target, float duration, Ease ease = Ease.InOutExpo) { var target_pos = target.position; return move_obj.LocalMoveTo(target_pos, duration, ease); } public static Tween LocalMoveTo(this Transform move_obj, Vector3 target_local_pos, float duration, Ease ease = Ease.InOutExpo) { var tw = move_obj.DOLocalMove(target_local_pos, duration); // Note: DOMove seems not hit the target position after finished if (tw != null) { tw.SetEase(ease); } return tw; } public static Tween FlyTo(this Transform flyobj, Transform target, float duration, PathMode mode = PathMode.Full3D,bool isUnScale = false) { var start_pos = flyobj.position; start_pos.z = 0; var end_pos = target.position; end_pos.z = 0; var dir = end_pos - start_pos; var center = (end_pos + start_pos) * 0.5f; var off_v = Vector3.up; if (Mathf.Abs(start_pos.x) < 10f) { off_v = Vector3.right; } var ratio = Mathf.Abs(dir.y) / 650f; var middle_pos = center + off_v * 400f * ratio; middle_pos.z = 0; var path = new Vector3[] { start_pos, middle_pos, end_pos }; var tw = flyobj.DOPath(path, duration, PathType.CatmullRom, mode); if(isUnScale) tw.SetUpdate(true); if (tw != null) { tw.SetEase(Ease.Linear); } return tw; } public static Tween FlyTo(this Transform flyobj, Transform target, Transform middle, float duration, PathMode mode = PathMode.Full3D,bool isUnScale = false) { var start_pos = flyobj.position; start_pos.z = 0; var end_pos = target.position; end_pos.z = 0; var middle_pos = middle.position; middle_pos.z = 0; var path = new Vector3[] { start_pos, middle_pos, end_pos }; var tw = flyobj.DOPath(path, duration, PathType.CatmullRom, mode); if(isUnScale) tw.SetUpdate(true); if (tw != null) { tw.SetEase(Ease.Linear); } return tw; } public static Tween FlyTo(this Transform flyobj, Vector3 target, Vector3 middle, float duration, PathMode mode = PathMode.Full3D,bool isUnScale = false) { var start_pos = flyobj.position; start_pos.z = 0; var end_pos = target; end_pos.z = 0; var middle_pos = middle; middle_pos.z = 0; var path = new Vector3[] { start_pos, middle_pos, end_pos }; var tw = flyobj.DOPath(path, duration, PathType.CatmullRom, mode); if(isUnScale) tw.SetUpdate(true); if (tw != null) { tw.SetEase(Ease.Linear); } return tw; } public static Tween FlyTo(this Transform flyobj, Vector3 target_pos, Vector3 middle_pos, float duration,bool isUnScale = false) { var start_pos = flyobj.position; start_pos.z = 0; var path = new Vector3[] { start_pos, middle_pos, target_pos }; var tw = flyobj.DOPath(path, duration, PathType.CatmullRom, PathMode.Full3D); if(isUnScale) tw.SetUpdate(true); if (tw != null) { tw.SetEase(Ease.Linear); } return tw; } public static Tween Flash(this Graphic target, float duration) { var t = DOTween.ToAlpha(() => target.color, x => target.color = x, 0, duration); t.SetTarget(target); t.SetLoops(-1, LoopType.Yoyo); return t; } public static Tween DOFadeAlpha(this Graphic target, float endValue, float duration) { var t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration); t.SetTarget(target); return t; } public static Tween DOFadeAlpha(this SpriteRenderer target, float endValue, float duration) { var t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration); t.SetTarget(target); return t; } public static Tween DOFadeAlpha(this TextMesh target, float endValue, float duration) { var t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration); t.SetTarget(target); return t; } public static Tween DOFadeAlpha(this CanvasGroup target, float endValue, float duration) { var t = DOTween.To(() => target.alpha, x => target.alpha = x, endValue, duration); t.SetTarget(target); return t; } public static Tween DoCircularTop(this RectTransform target, float ridus, float duration, bool isIgnoreTimeScale = false) { float time = 0; Vector2 vector2 = new Vector2(); var t = DOTween.To(() => time, x => time = x, 1, duration).OnUpdate(() => { float b = time; b = (-b * 360 + 90) * Mathf.Deg2Rad; vector2.x = Mathf.Cos(b) * ridus; vector2.y = Mathf.Sin(b) * ridus; target.anchoredPosition = vector2; }); if (isIgnoreTimeScale) t.SetUpdate(true); return t; } }