使用DoTween制作打字机

This commit is contained in:
taosuqi 2024-08-28 16:05:35 +08:00
parent b2c9225ad4
commit dec8d77655
7 changed files with 99 additions and 13 deletions

View File

@ -1,4 +1,4 @@
// Author: Daniele Giardini - http://www.demigiant.com // Author: Daniele Giardini - http://www.demigiant.com
// Created: 2015/03/12 15:55 // Created: 2015/03/12 15:55
using System; using System;
@ -8,7 +8,7 @@ using UnityEngine;
#if true // UI_MARKER #if true // UI_MARKER
using UnityEngine.UI; using UnityEngine.UI;
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
using TMPro; using TMPro;
#endif #endif
@ -355,7 +355,7 @@ namespace DG.Tweening
tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration); tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration);
break; break;
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
case TargetType.TextMeshProUGUI: case TargetType.TextMeshProUGUI:
tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration); tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration);
break; break;
@ -398,7 +398,7 @@ namespace DG.Tweening
tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration); tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration);
break; break;
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
case TargetType.TextMeshProUGUI: case TargetType.TextMeshProUGUI:
tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration); tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration);
break; break;
@ -423,7 +423,7 @@ namespace DG.Tweening
break; break;
} }
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
switch (targetType) { switch (targetType) {
case TargetType.TextMeshProUGUI: case TargetType.TextMeshProUGUI:
tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);

View File

@ -1,11 +1,11 @@
// Author: Daniele Giardini - http://www.demigiant.com // Author: Daniele Giardini - http://www.demigiant.com
// Created: 2015/03/27 19:02 // Created: 2015/03/27 19:02
// //
// License Copyright (c) Daniele Giardini. // License Copyright (c) Daniele Giardini.
// This work is subject to the terms at http://dotween.demigiant.com/license.php // This work is subject to the terms at http://dotween.demigiant.com/license.php
#if false // MODULE_MARKER #if true // MODULE_MARKER
using System; using System;
using System.Globalization; using System.Globalization;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,4 +1,4 @@
// Author: Daniele Giardini - http://www.demigiant.com // Author: Daniele Giardini - http://www.demigiant.com
// Created: 2015/03/12 16:03 // Created: 2015/03/12 16:03
using System; using System;
@ -15,7 +15,7 @@ using DOTweenSettings = DG.Tweening.Core.DOTweenSettings;
#if true // UI_MARKER #if true // UI_MARKER
using UnityEngine.UI; using UnityEngine.UI;
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
using TMPro; using TMPro;
#endif #endif
@ -120,7 +120,7 @@ namespace DG.DOTweenEditor
{ DOTweenAnimation.AnimationType.Text, new[] { typeof(tk2dTextMesh) } } { DOTweenAnimation.AnimationType.Text, new[] { typeof(tk2dTextMesh) } }
}; };
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
static readonly Dictionary<DOTweenAnimation.AnimationType, Type[]> _TMPAnimationTypeToComponent = new Dictionary<DOTweenAnimation.AnimationType, Type[]>() { static readonly Dictionary<DOTweenAnimation.AnimationType, Type[]> _TMPAnimationTypeToComponent = new Dictionary<DOTweenAnimation.AnimationType, Type[]>() {
{ DOTweenAnimation.AnimationType.Color, new[] { typeof(TextMeshPro), typeof(TextMeshProUGUI) } }, { DOTweenAnimation.AnimationType.Color, new[] { typeof(TextMeshPro), typeof(TextMeshProUGUI) } },
{ DOTweenAnimation.AnimationType.Fade, new[] { typeof(TextMeshPro), typeof(TextMeshProUGUI) } }, { DOTweenAnimation.AnimationType.Fade, new[] { typeof(TextMeshPro), typeof(TextMeshProUGUI) } },
@ -141,7 +141,7 @@ namespace DG.DOTweenEditor
#if false // TK2D_MARKER #if false // TK2D_MARKER
"Text", "Text",
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
"Text", "Text",
#endif #endif
#if true // UI_MARKER #if true // UI_MARKER
@ -613,7 +613,7 @@ namespace DG.DOTweenEditor
} }
} }
#endif #endif
#if false // TEXTMESHPRO_MARKER #if true // TEXTMESHPRO_MARKER
if (_TMPAnimationTypeToComponent.ContainsKey(_src.animationType)) { if (_TMPAnimationTypeToComponent.ContainsKey(_src.animationType)) {
foreach (Type t in _TMPAnimationTypeToComponent[_src.animationType]) { foreach (Type t in _TMPAnimationTypeToComponent[_src.animationType]) {
srcTarget = targetGO.GetComponent(t); srcTarget = targetGO.GetComponent(t);

View File

@ -0,0 +1,54 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 16995157, guid: a811bde74b26b53498b4f6d872b09b6d, type: 3}
m_Name: DOTweenSettings
m_EditorClassIdentifier:
useSafeMode: 1
safeModeOptions:
logBehaviour: 2
nestedTweenFailureBehaviour: 0
timeScale: 1
unscaledTimeScale: 1
useSmoothDeltaTime: 0
maxSmoothUnscaledTime: 0.15
rewindCallbackMode: 0
showUnityEditorReport: 0
logBehaviour: 0
drawGizmos: 1
defaultRecyclable: 0
defaultAutoPlay: 3
defaultUpdateType: 0
defaultTimeScaleIndependent: 0
defaultEaseType: 6
defaultEaseOvershootOrAmplitude: 1.70158
defaultEasePeriod: 0
defaultAutoKill: 1
defaultLoopType: 0
debugMode: 0
debugStoreTargetId: 1
showPreviewPanel: 1
storeSettingsLocation: 0
modules:
showPanel: 0
audioEnabled: 1
physicsEnabled: 1
physics2DEnabled: 1
spriteEnabled: 1
uiEnabled: 1
textMeshProEnabled: 0
tk2DEnabled: 0
deAudioEnabled: 0
deUnityExtendedEnabled: 0
epoOutlineEnabled: 0
createASMDEF: 0
showPlayingTweens: 0
showPausedTweens: 0

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 62d0e6972be5fb6448d02abc8a47cfb6
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +1,4 @@
using DG.Tweening;
using System; using System;
using System.Collections; using System.Collections;
using TMPro; using TMPro;
@ -8,6 +9,10 @@ public class UI_CustomSessionPanel : BasePanel
{ {
private string triggerID; private string triggerID;
public Action<string> callBack; public Action<string> callBack;
private TextMeshProUGUI UItext;
private string fullText;
private float typingSpeed = 0.05f;
/// <summary> /// <summary>
/// 初始化,state 0 激活另一个操作 /// 初始化,state 0 激活另一个操作
/// </summary> /// </summary>
@ -16,6 +21,8 @@ public class UI_CustomSessionPanel : BasePanel
this.triggerID = triggerID; this.triggerID = triggerID;
callBack += _callBack; callBack += _callBack;
GetControl<TextMeshProUGUI>("ClientText_DialogBox").text = clientTalk /*"好的"*/; GetControl<TextMeshProUGUI>("ClientText_DialogBox").text = clientTalk /*"好的"*/;
UItext.text = GetControl<TextMeshProUGUI>("ClientText_DialogBox").text;
StartTypewriterEffect();
} }
//} //}
@ -33,5 +40,10 @@ public class UI_CustomSessionPanel : BasePanel
break; break;
} }
} }
void StartTypewriterEffect()
{
// 使用DoTween的DOText方法实现打字机效果
UItext.DOText(fullText, fullText.Length * typingSpeed)
.SetEase(Ease.Linear);
}
} }

View File

@ -1,3 +1,4 @@
using DG.Tweening;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -9,6 +10,9 @@ public class UI_PlayerSessionPanel : BasePanel
{ {
private string triggerID; private string triggerID;
public Action<string> callBack; public Action<string> callBack;
private TextMeshProUGUI UItext;
private string fullText;
private float typingSpeed = 0.05f;
/// <summary> /// <summary>
/// 初始化,state 0 激活另一个操作 /// 初始化,state 0 激活另一个操作
/// </summary> /// </summary>
@ -17,6 +21,8 @@ public class UI_PlayerSessionPanel : BasePanel
this.triggerID = triggerID; this.triggerID = triggerID;
callBack += _callBack; callBack += _callBack;
GetControl<TextMeshProUGUI>("PlayerText_DialogBox").text = clientTalk /*"好的"*/; GetControl<TextMeshProUGUI>("PlayerText_DialogBox").text = clientTalk /*"好的"*/;
UItext.text = GetControl<TextMeshProUGUI>("PlayerText_DialogBox").text;
StartTypewriterEffect();
} }
//} //}
@ -34,4 +40,10 @@ public class UI_PlayerSessionPanel : BasePanel
break; break;
} }
} }
void StartTypewriterEffect()
{
// 使用DoTween的DOText方法实现打字机效果
UItext.DOText(fullText, fullText.Length * typingSpeed)
.SetEase(Ease.Linear);
}
} }