Skip to content

Commit d1abdcd

Browse files
committed
edit: LoadingCurtain convert text to tmp_pro and delete Coroutine
1 parent 4f938ad commit d1abdcd

File tree

3 files changed

+134
-64
lines changed

3 files changed

+134
-64
lines changed

Assets/Code/Extensions/DOTweenTMPExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ namespace Code.Extensions
66
{
77
public static class DOTweenTMPExtensions
88
{
9-
public static Tweener DOTextCounter(this TMP_Text target, int fromValue, int toValue, float duration, bool addThousandsSeparator = false)
9+
public static Tweener DOTextCounter(this TMP_Text target, int fromValue, int toValue, float duration,
10+
bool addThousandsSeparator = false)
1011
{
1112
int currentValue = fromValue;
1213
return DOTween.To(() => currentValue, x =>

Assets/Code/Infrastructure/LoadingCurtain.cs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
using System.Collections;
1+
using System;
2+
using System.Threading;
3+
using Cysharp.Threading.Tasks;
4+
using TMPro;
25
using UnityEngine;
3-
using UnityEngine.UI;
46

57
namespace Code.Infrastructure
68
{
79
public class LoadingCurtain : MonoBehaviour, ILoadingCurtain
810
{
911
private const float Delay = 1.75f;
1012
private const float AnimationDuration = 0.65f;
11-
13+
private const float TextUpdateInterval = 0.15f;
14+
1215
[SerializeField] private RectTransform _right;
1316
[SerializeField] private RectTransform _left;
14-
[SerializeField] private Text _loadingText;
17+
[SerializeField] private TMP_Text _loadingText;
1518

16-
private Coroutine _loadingTextCoroutine;
19+
private CancellationTokenSource _textAnimationCts;
1720

1821
private void Awake()
1922
{
@@ -32,64 +35,73 @@ public void Show()
3235

3336
public void Hide()
3437
{
35-
StartCoroutine(AnimationOpen());
38+
AnimationOpenAsync().Forget();
3639
StartLoadingTextAnimation();
3740
}
3841

39-
private IEnumerator AnimationOpen()
42+
private async UniTaskVoid AnimationOpenAsync()
4043
{
41-
yield return new WaitForSeconds(Delay);
42-
44+
await UniTask.Delay(TimeSpan.FromSeconds(Delay), cancellationToken: this.GetCancellationTokenOnDestroy());
45+
4346
float screenWidth = Screen.width;
4447
float elapsedTime = 0f;
4548

4649
Vector2 leftStart = _left.anchoredPosition;
47-
Vector2 leftTarget = new Vector2(-screenWidth / 2, leftStart.y);
48-
50+
Vector2 leftTarget = new Vector2(-screenWidth / 2f, leftStart.y);
51+
4952
Vector2 rightStart = _right.anchoredPosition;
50-
Vector2 rightTarget = new Vector2(screenWidth / 2, rightStart.y);
53+
Vector2 rightTarget = new Vector2(screenWidth / 2f, rightStart.y);
5154

5255
while (elapsedTime < AnimationDuration)
5356
{
5457
elapsedTime += Time.deltaTime;
55-
float t = elapsedTime / AnimationDuration;
58+
float t = Mathf.Clamp01(elapsedTime / AnimationDuration);
5659
_left.anchoredPosition = Vector2.Lerp(leftStart, leftTarget, t);
5760
_right.anchoredPosition = Vector2.Lerp(rightStart, rightTarget, t);
58-
yield return null;
61+
await UniTask.Yield(PlayerLoopTiming.Update);
5962
}
6063

6164
_left.anchoredPosition = leftTarget;
6265
_right.anchoredPosition = rightTarget;
63-
66+
6467
StopLoadingTextAnimation();
6568
gameObject.SetActive(false);
6669
IsActive = false;
6770
}
6871

6972
private void StartLoadingTextAnimation()
7073
{
71-
_loadingTextCoroutine = StartCoroutine(LoadingTextEffect());
74+
StopLoadingTextAnimation();
75+
_textAnimationCts = new CancellationTokenSource();
76+
AnimateLoadingTextAsync(_textAnimationCts.Token).Forget();
7277
}
7378

7479
private void StopLoadingTextAnimation()
7580
{
76-
if (_loadingTextCoroutine != null)
77-
StopCoroutine(_loadingTextCoroutine);
81+
if (_textAnimationCts != null && !_textAnimationCts.IsCancellationRequested)
82+
_textAnimationCts.Cancel();
83+
_textAnimationCts?.Dispose();
84+
_textAnimationCts = null;
7885
}
7986

80-
private IEnumerator LoadingTextEffect()
87+
private async UniTaskVoid AnimateLoadingTextAsync(CancellationToken token)
8188
{
8289
string baseText = "Loading";
83-
while (true)
90+
string[] dots = { "", ".", "..", "..." };
91+
int index = 0;
92+
93+
try
94+
{
95+
while (!token.IsCancellationRequested)
96+
{
97+
_loadingText.text = baseText + dots[index];
98+
index = (index + 1) % dots.Length;
99+
await UniTask.Delay(TimeSpan.FromSeconds(TextUpdateInterval), cancellationToken: token);
100+
}
101+
}
102+
catch (OperationCanceledException)
84103
{
85-
_loadingText.text = baseText + "";
86-
yield return new WaitForSeconds(0.15f);
87-
_loadingText.text = baseText + ".";
88-
yield return new WaitForSeconds(0.15f);
89-
_loadingText.text = baseText + "..";
90-
yield return new WaitForSeconds(0.15f);
91-
_loadingText.text = baseText + "...";
92-
yield return new WaitForSeconds(0.15f);
104+
93105
}
94106
}
95107
}

Assets/Resources/Huds/LoadingCurtain.prefab

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Canvas:
5959
m_OverridePixelPerfect: 0
6060
m_SortingBucketNormalizedSize: 0
6161
m_VertexColorAlwaysGammaSpace: 0
62-
m_AdditionalShaderChannelsFlag: 0
62+
m_AdditionalShaderChannelsFlag: 25
6363
m_UpdateRectTransformForStandalone: 0
6464
m_SortingLayerID: 0
6565
m_SortingOrder: 100
@@ -118,7 +118,7 @@ MonoBehaviour:
118118
m_EditorClassIdentifier:
119119
_right: {fileID: 6276509582930694241}
120120
_left: {fileID: 9210649380886456409}
121-
_loadingText: {fileID: 2674375570835396536}
121+
_loadingText: {fileID: 7500382623053582182}
122122
--- !u!225 &4943869930359128343
123123
CanvasGroup:
124124
m_ObjectHideFlags: 0
@@ -445,9 +445,9 @@ GameObject:
445445
m_Component:
446446
- component: {fileID: 8408788738794632932}
447447
- component: {fileID: 1794397984737047278}
448-
- component: {fileID: 2674375570835396536}
449448
- component: {fileID: 8245301671873328852}
450449
- component: {fileID: 1221078767789420150}
450+
- component: {fileID: 7500382623053582182}
451451
m_Layer: 5
452452
m_Name: Text_Loading
453453
m_TagString: Untagged
@@ -482,7 +482,7 @@ CanvasRenderer:
482482
m_PrefabAsset: {fileID: 0}
483483
m_GameObject: {fileID: 5851812557447389215}
484484
m_CullTransparentMesh: 1
485-
--- !u!114 &2674375570835396536
485+
--- !u!114 &8245301671873328852
486486
MonoBehaviour:
487487
m_ObjectHideFlags: 0
488488
m_CorrespondingSourceObject: {fileID: 0}
@@ -491,32 +491,13 @@ MonoBehaviour:
491491
m_GameObject: {fileID: 5851812557447389215}
492492
m_Enabled: 1
493493
m_EditorHideFlags: 0
494-
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
494+
m_Script: {fileID: 11500000, guid: e19747de3f5aca642ab2be37e372fb86, type: 3}
495495
m_Name:
496496
m_EditorClassIdentifier:
497-
m_Material: {fileID: 0}
498-
m_Color: {r: 1, g: 1, b: 1, a: 1}
499-
m_RaycastTarget: 1
500-
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
501-
m_Maskable: 1
502-
m_OnCullStateChanged:
503-
m_PersistentCalls:
504-
m_Calls: []
505-
m_FontData:
506-
m_Font: {fileID: 12800000, guid: c16eb7d7c3953489db00312b8b09df4b, type: 3}
507-
m_FontSize: 39
508-
m_FontStyle: 0
509-
m_BestFit: 1
510-
m_MinSize: 0
511-
m_MaxSize: 40
512-
m_Alignment: 4
513-
m_AlignByGeometry: 0
514-
m_RichText: 1
515-
m_HorizontalOverflow: 0
516-
m_VerticalOverflow: 0
517-
m_LineSpacing: 1
518-
m_Text: Loading . . .
519-
--- !u!114 &8245301671873328852
497+
m_EffectColor: {r: 0, g: 0, b: 0, a: 1}
498+
m_EffectDistance: {x: 2, y: 2}
499+
m_UseGraphicAlpha: 1
500+
--- !u!114 &1221078767789420150
520501
MonoBehaviour:
521502
m_ObjectHideFlags: 0
522503
m_CorrespondingSourceObject: {fileID: 0}
@@ -525,13 +506,13 @@ MonoBehaviour:
525506
m_GameObject: {fileID: 5851812557447389215}
526507
m_Enabled: 1
527508
m_EditorHideFlags: 0
528-
m_Script: {fileID: 11500000, guid: e19747de3f5aca642ab2be37e372fb86, type: 3}
509+
m_Script: {fileID: 11500000, guid: cfabb0440166ab443bba8876756fdfa9, type: 3}
529510
m_Name:
530511
m_EditorClassIdentifier:
531512
m_EffectColor: {r: 0, g: 0, b: 0, a: 1}
532-
m_EffectDistance: {x: 2, y: 2}
513+
m_EffectDistance: {x: 0, y: -2}
533514
m_UseGraphicAlpha: 1
534-
--- !u!114 &1221078767789420150
515+
--- !u!114 &7500382623053582182
535516
MonoBehaviour:
536517
m_ObjectHideFlags: 0
537518
m_CorrespondingSourceObject: {fileID: 0}
@@ -540,9 +521,85 @@ MonoBehaviour:
540521
m_GameObject: {fileID: 5851812557447389215}
541522
m_Enabled: 1
542523
m_EditorHideFlags: 0
543-
m_Script: {fileID: 11500000, guid: cfabb0440166ab443bba8876756fdfa9, type: 3}
524+
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
544525
m_Name:
545526
m_EditorClassIdentifier:
546-
m_EffectColor: {r: 0, g: 0, b: 0, a: 1}
547-
m_EffectDistance: {x: 0, y: -2}
548-
m_UseGraphicAlpha: 1
527+
m_Material: {fileID: 0}
528+
m_Color: {r: 1, g: 1, b: 1, a: 1}
529+
m_RaycastTarget: 1
530+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
531+
m_Maskable: 1
532+
m_OnCullStateChanged:
533+
m_PersistentCalls:
534+
m_Calls: []
535+
m_text: Loading . . .
536+
m_isRightToLeft: 0
537+
m_fontAsset: {fileID: 11400000, guid: 9a2157e1f9c4c4d38841fbb92558f587, type: 2}
538+
m_sharedMaterial: {fileID: -6360588970910393389, guid: 9a2157e1f9c4c4d38841fbb92558f587, type: 2}
539+
m_fontSharedMaterials: []
540+
m_fontMaterial: {fileID: 0}
541+
m_fontMaterials: []
542+
m_fontColor32:
543+
serializedVersion: 2
544+
rgba: 4294967295
545+
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
546+
m_enableVertexGradient: 0
547+
m_colorMode: 3
548+
m_fontColorGradient:
549+
topLeft: {r: 1, g: 1, b: 1, a: 1}
550+
topRight: {r: 1, g: 1, b: 1, a: 1}
551+
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
552+
bottomRight: {r: 1, g: 1, b: 1, a: 1}
553+
m_fontColorGradientPreset: {fileID: 0}
554+
m_spriteAsset: {fileID: 0}
555+
m_tintAllSprites: 0
556+
m_StyleSheet: {fileID: 0}
557+
m_TextStyleHashCode: -1183493901
558+
m_overrideHtmlColors: 0
559+
m_faceColor:
560+
serializedVersion: 2
561+
rgba: 4294967295
562+
m_fontSize: 39
563+
m_fontSizeBase: 39
564+
m_fontWeight: 400
565+
m_enableAutoSizing: 0
566+
m_fontSizeMin: 18
567+
m_fontSizeMax: 72
568+
m_fontStyle: 0
569+
m_HorizontalAlignment: 2
570+
m_VerticalAlignment: 512
571+
m_textAlignment: 65535
572+
m_characterSpacing: 0
573+
m_wordSpacing: 0
574+
m_lineSpacing: 0
575+
m_lineSpacingMax: 0
576+
m_paragraphSpacing: 0
577+
m_charWidthMaxAdj: 0
578+
m_TextWrappingMode: 1
579+
m_wordWrappingRatios: 0.4
580+
m_overflowMode: 0
581+
m_linkedTextComponent: {fileID: 0}
582+
parentLinkedComponent: {fileID: 0}
583+
m_enableKerning: 0
584+
m_ActiveFontFeatures: 6e72656b
585+
m_enableExtraPadding: 0
586+
checkPaddingRequired: 0
587+
m_isRichText: 1
588+
m_EmojiFallbackSupport: 1
589+
m_parseCtrlCharacters: 1
590+
m_isOrthographic: 1
591+
m_isCullingEnabled: 0
592+
m_horizontalMapping: 0
593+
m_verticalMapping: 0
594+
m_uvLineOffset: 0
595+
m_geometrySortingOrder: 0
596+
m_IsTextObjectScaleStatic: 0
597+
m_VertexBufferAutoSizeReduction: 0
598+
m_useMaxVisibleDescender: 1
599+
m_pageToDisplay: 1
600+
m_margin: {x: 0, y: 0, z: 0, w: 0}
601+
m_isUsingLegacyAnimationComponent: 0
602+
m_isVolumetricText: 0
603+
m_hasFontAssetChanged: 0
604+
m_baseMaterial: {fileID: 0}
605+
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}

0 commit comments

Comments
 (0)