// Author: Daniele Giardini - http://www.demigiant.com // Created: 2014/08/30 19:03 // // License Copyright (c) Daniele Giardini. // This work is subject to the terms at http://dotween.demigiant.com/license.php using DG.Tweening.Core; using DG.Tweening.Core.Easing; using UnityEngine; namespace DG.Tweening { /// /// This class serves only as a utility class to store tween settings to apply on multiple tweens. /// It is in no way needed otherwise, since you can directly apply tween settings to a tween via chaining /// public class TweenParams { /// A variable you can eventually Clear and reuse when needed, /// to avoid instantiating TweenParams objects public static readonly TweenParams Params = new TweenParams(); internal object id; internal object target; internal UpdateType updateType; internal bool isIndependentUpdate; internal TweenCallback onStart; internal TweenCallback onPlay; internal TweenCallback onRewind; internal TweenCallback onUpdate; internal TweenCallback onStepComplete; internal TweenCallback onComplete; internal TweenCallback onKill; internal TweenCallback onWaypointChange; internal bool isRecyclable; internal bool isSpeedBased; internal bool autoKill; internal int loops; internal LoopType loopType; internal float delay; internal bool isRelative; internal Ease easeType; internal EaseFunction customEase; internal float easeOvershootOrAmplitude; internal float easePeriod; // *********************************************************************************** // CONSTRUCTOR // *********************************************************************************** /// Creates a new TweenParams object, which you can use to store tween settings /// to pass to multiple tweens via myTween.SetAs(myTweenParms) public TweenParams() { Clear(); } #region Methods /// Clears and resets this TweenParams instance using default values, /// so it can be reused without instantiating another one public TweenParams Clear() { id = target = null; updateType = DOTween.defaultUpdateType; isIndependentUpdate = DOTween.defaultTimeScaleIndependent; onStart = onPlay = onRewind = onUpdate = onStepComplete = onComplete = onKill = null; onWaypointChange = null; isRecyclable = DOTween.defaultRecyclable; isSpeedBased = false; autoKill = DOTween.defaultAutoKill; loops = 1; loopType = DOTween.defaultLoopType; delay = 0; isRelative = false; easeType = Ease.Unset; customEase = null; easeOvershootOrAmplitude = DOTween.defaultEaseOvershootOrAmplitude; easePeriod = DOTween.defaultEasePeriod; return this; } #endregion #region Settings : Tweeners + Sequences /// Sets the autoKill behaviour of the tween. /// Has no effect if the tween has already started /// If TRUE the tween will be automatically killed when complete public TweenParams SetAutoKill(bool autoKillOnCompletion = true) { this.autoKill = autoKillOnCompletion; return this; } /// Sets an ID for the tween, which can then be used as a filter with DOTween's static methods. /// The ID to assign to this tween. Can be an int, a string, an object or anything else. public TweenParams SetId(object id) { this.id = id; return this; } /// Sets the target for the tween, which can then be used as a filter with DOTween's static methods. /// IMPORTANT: use it with caution. If you just want to set an ID for the tween use SetId instead. /// When using shorcuts the shortcut target is already assigned as the tween's target, /// so using this method will overwrite it and prevent shortcut-operations like myTarget.DOPause from working correctly. /// The target to assign to this tween. Can be an int, a string, an object or anything else. public TweenParams SetTarget(object target) { this.target = target; return this; } /// Sets the looping options for the tween. /// Has no effect if the tween has already started /// Number of cycles to play (-1 for infinite - will be converted to 1 in case the tween is nested in a Sequence) /// Loop behaviour type (default: LoopType.Restart) public TweenParams SetLoops(int loops, LoopType? loopType = null) { if (loops < -1) loops = -1; else if (loops == 0) loops = 1; this.loops = loops; if (loopType != null) this.loopType = (LoopType)loopType; return this; } /// Sets the ease of the tween. /// If applied to Sequences eases the whole sequence animation /// Eventual overshoot or amplitude to use with Back or Elastic easeType (default is 1.70158) /// Eventual period to use with Elastic easeType (default is 0) public TweenParams SetEase(Ease ease, float? overshootOrAmplitude = null, float? period = null) { this.easeType = ease; this.easeOvershootOrAmplitude = overshootOrAmplitude != null ? (float)overshootOrAmplitude : DOTween.defaultEaseOvershootOrAmplitude; this.easePeriod = period != null ? (float)period : DOTween.defaultEasePeriod; this.customEase = null; return this; } /// Sets the ease of the tween using an AnimationCurve. /// If applied to Sequences eases the whole sequence animation public TweenParams SetEase(AnimationCurve animCurve) { this.easeType = Ease.INTERNAL_Custom; this.customEase = new EaseCurve(animCurve).Evaluate; return this; } /// Sets the ease of the tween using a custom ease function. /// If applied to Sequences eases the whole sequence animation public TweenParams SetEase(EaseFunction customEase) { this.easeType = Ease.INTERNAL_Custom; this.customEase = customEase; return this; } /// Sets the recycling behaviour for the tween. /// If TRUE the tween will be recycled after being killed, otherwise it will be destroyed. public TweenParams SetRecyclable(bool recyclable = true) { this.isRecyclable = recyclable; return this; } /// Sets the update type to the one defined in DOTween.defaultUpdateType (UpdateType.Normal unless changed) /// and lets you choose if it should be independent from Unity's Time.timeScale /// If TRUE the tween will ignore Unity's Time.timeScale public TweenParams SetUpdate(bool isIndependentUpdate) { this.updateType = DOTween.defaultUpdateType; this.isIndependentUpdate = isIndependentUpdate; return this; } /// Sets the type of update (default or independent) for the tween /// The type of update (default: UpdateType.Normal) /// If TRUE the tween will ignore Unity's Time.timeScale public TweenParams SetUpdate(UpdateType updateType, bool isIndependentUpdate = false) { this.updateType = updateType; this.isIndependentUpdate = isIndependentUpdate; return this; } /// Sets the onStart callback for the tween. /// Called the first time the tween is set in a playing state, after any eventual delay public TweenParams OnStart(TweenCallback action) { this.onStart = action; return this; } /// Sets the onPlay callback for the tween. /// Called when the tween is set in a playing state, after any eventual delay. /// Also called each time the tween resumes playing from a paused state public TweenParams OnPlay(TweenCallback action) { this.onPlay = action; return this; } /// Sets the onRewind callback for the tween. /// Called when the tween is rewinded, /// either by calling Rewind or by reaching the start position while playing backwards. /// Rewinding a tween that is already rewinded will not fire this callback public TweenParams OnRewind(TweenCallback action) { this.onRewind = action; return this; } /// Sets the onUpdate callback for the tween. /// Called each time the tween updates public TweenParams OnUpdate(TweenCallback action) { this.onUpdate = action; return this; } /// Sets the onStepComplete callback for the tween. /// Called the moment the tween completes one loop cycle, even when going backwards public TweenParams OnStepComplete(TweenCallback action) { this.onStepComplete = action; return this; } /// Sets the onComplete callback for the tween. /// Called the moment the tween reaches its final forward position, loops included public TweenParams OnComplete(TweenCallback action) { this.onComplete = action; return this; } /// Sets the onKill callback for the tween. /// Called the moment the tween is killed public TweenParams OnKill(TweenCallback action) { this.onKill = action; return this; } /// Sets the onWaypointChange callback for the tween. /// Called when a path tween reaches a new waypoint public TweenParams OnWaypointChange(TweenCallback action) { this.onWaypointChange = action; return this; } #endregion #region Settings : Tweeners-only /// Sets a delayed startup for the tween. /// Has no effect on Sequences or if the tween has already started public TweenParams SetDelay(float delay) { this.delay = delay; return this; } /// If isRelative is TRUE sets the tween as relative /// (the endValue will be calculated as startValue + endValue instead than being used directly). /// Has no effect on Sequences or if the tween has already started public TweenParams SetRelative(bool isRelative = true) { this.isRelative = isRelative; return this; } /// If isSpeedBased is TRUE sets the tween as speed based /// (the duration will represent the number of units the tween moves x second). /// Has no effect on Sequences, nested tweens, or if the tween has already started public TweenParams SetSpeedBased(bool isSpeedBased = true) { this.isSpeedBased = isSpeedBased; return this; } #endregion } }