KaiClavier

STMDOTweenExtensions.cs

Aug 18th, 2025
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. //Copyright (c) 2025 Kai Clavier [kaiclavier.com] Do Not Distribute
  2. //DOTween belongs to DEMIGIANT [dotween.demigiant.com]
  3. using DG.Tweening;
  4. using DG.Tweening.Core;
  5. using DG.Tweening.Plugins.Options;
  6. using UnityEngine;
  7.  
  8. /*
  9.  * superTextMesh.DOFade(endValue, duration);
  10.  * superTextMesh.DOColor(endValue, duration);
  11.  * superTextMesh.DOBlendableColor(endValue, duration);
  12.  * superTextMesh.DORead(duration, drawAnimName (optional), startIndex (optional));
  13.  * superTextMesh.DOSize(endValue, duration);
  14.  */
  15.  
  16. namespace Clavian.STM.Extensions
  17. {
  18.     public static class STMDOTweenExtensions
  19.     {
  20.         public static TweenerCore<float, float, FloatOptions> DOFade(
  21.             this SuperTextMesh target,
  22.             float endValue,
  23.             float duration)
  24.         {
  25.             TweenerCore<float, float, FloatOptions> alpha = DOTween.To(
  26.                 (DOGetter<float>)(() => target.fade),
  27.                 (DOSetter<float>)(x => target.fade = x),
  28.                 endValue,
  29.                 duration);
  30.  
  31.             return alpha;
  32.         }
  33.        
  34.         public static TweenerCore<Color, Color, ColorOptions> DOColor(
  35.             this SuperTextMesh target,
  36.             Color endValue,
  37.             float duration)
  38.         {
  39.             TweenerCore<Color, Color, ColorOptions> t = DOTween.To(
  40.                 (DOGetter<Color>) (() => target.color),
  41.                 (DOSetter<Color>) (x => target.color = x),
  42.                 endValue,
  43.                 duration);
  44.             //turns out, this isn't needed?
  45.             //.OnUpdate(() => target.SetMesh(target.currentReadTime))
  46.             t.SetTarget<TweenerCore<Color, Color, ColorOptions>>((object) target);
  47.             return t;
  48.         }
  49.        
  50.         public static Tweener DOBlendableColor(
  51.             this SuperTextMesh target,
  52.             Color endValue,
  53.             float duration)
  54.         {
  55.             endValue -= target.color;
  56.             Color to = new Color(0.0f, 0.0f, 0.0f, 0.0f);
  57.             return (Tweener) DOTween.To((DOGetter<Color>) (() => to), (DOSetter<Color>) (x =>
  58.             {
  59.                 Color color = x - to;
  60.                 to = x;
  61.                 target.color += color;
  62.             }), endValue, duration).Blendable<Color, Color, ColorOptions>().SetTarget<TweenerCore<Color, Color, ColorOptions>>((object) target);
  63.         }
  64.        
  65.         //this does not make sounds or call events vs. calling SuperTextMesh.Read()
  66.         public static TweenerCore<float, float, FloatOptions> DORead(
  67.             this SuperTextMesh target,
  68.             float duration,
  69.             string drawAnimName = "",
  70.             float startIndex = 0f)
  71.         {
  72.             if(target.characterCount == 0) return null;
  73.             if(drawAnimName?.Length > 0) target.drawAnimName = drawAnimName;
  74.             target.readDelay = duration / target.characterCount;
  75.             target.Rebuild(startIndex * target.readDelay, false); //sets current read time...
  76.             TweenerCore<float, float, FloatOptions> read = DOTween.To(
  77.                 (DOGetter<float>)(() => target.currentReadTime),
  78.                 (DOSetter<float>)(x =>
  79.                 {
  80.                     target.reading = true;
  81.                     target.currentReadTime = x;
  82.                     target.SetMesh(target.currentReadTime);
  83.                 }),
  84.                 duration,
  85.                 duration).OnComplete(target.ShowAllText); //sets reading to false
  86.             return read;
  87.         }
  88.        
  89.         //a bit heavier than the other methods, as Rebuild() is called when the tween updates.
  90.         public static TweenerCore<float, float, FloatOptions> DOSize(
  91.             this SuperTextMesh target,
  92.             float endValue,
  93.             float duration)
  94.         {
  95.             TweenerCore<float, float, FloatOptions> size = DOTween.To(
  96.                 (DOGetter<float>)(() => target.size),
  97.                 (DOSetter<float>)(x =>
  98.                 {
  99.                     target.size = x;
  100.                     target.Rebuild();
  101.                 }),
  102.                 endValue,
  103.                 duration);
  104.  
  105.             return size;
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment