// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using UltimateXR.Animation.Interpolation; using UltimateXR.Extensions.Unity; using UnityEngine; using UnityEngine.UI; #if ULTIMATEXR_UNITY_TMPRO using TMPro; #endif namespace UltimateXR.Animation.UI { /// /// Tweening component to animate a UI text component programatically or using the inspector. Supports both Unity's /// Text and TMPro. /// The text interpolation can be used to create a typewriter kind of effect. /// Programatically it also offers the possibility to interpolate parameters in a text string. /// [DisallowMultipleComponent] public class UxrTextContentTween : UxrTween { #region Inspector Properties/Serialized Fields [SerializeField] private string _startText; [SerializeField] private string _endText; #endregion #region Public Types & Data /// /// Gets the component whose string will be interpolated. /// public Text TargetText => GetCachedComponent(); #if ULTIMATEXR_UNITY_TMPRO /// /// Gets the component whose string will be interpolated. /// public TextMeshProUGUI TargetTextTMPro => GetCachedComponent(); #endif /// /// Gets or sets the text value. /// public string Text { get { if (TargetText != null) { return TargetText.text; } #if ULTIMATEXR_UNITY_TMPRO if (TargetTextTMPro != null) { return TargetTextTMPro.text; } #endif return null; } set { if (TargetText != null) { TargetText.text = value; } #if ULTIMATEXR_UNITY_TMPRO if (TargetTextTMPro != null) { TargetTextTMPro.text = value; } #endif } } /// /// Gets whether the interpolation uses format string parameters. /// /// /// false: Interpolation will be a plain typewriter effect from to /// /// /// /// true: Interpolation will use and . For more /// information on how these are used see /// UxrInterpolator.InterpolateText /// /// /// public bool UsesFormatString { get; private set; } /// /// Animation start text /// public string StartText { get => _startText; set => _startText = value; } /// /// Animation end text /// public string EndText { get => _endText; set => _endText = value; } /// /// Animation format string, when is true. /// public string FormatString { get; set; } /// /// Animation format string parameter list, when is true. /// public List FormatStringArgs { get; set; } #endregion #region Public Methods /// /// Creates and starts a tweening animation for a Unity UI Text component or TMPro text component. /// /// Target GameObject with either a Unity UI Text component or Text Mesh Pro text component /// Start text /// End text /// Interpolation settings that control the animation /// Optional callback when the animation finished /// /// Tweening component that will update itself automatically. Can be used to stop the animation prematurely or /// change parameters on the fly. /// public static UxrTextContentTween Animate(GameObject gameObject, string startText, string endText, UxrInterpolationSettings settings, Action finishedCallback = null) { UxrTextContentTween textContentTween = gameObject.GetOrAddComponent(); textContentTween.UsesFormatString = false; textContentTween.StartText = startText; textContentTween.EndText = endText; textContentTween.InterpolationSettings = settings; textContentTween.FinishedCallback = finishedCallback; textContentTween.Restart(); return textContentTween; } /// /// Creates and starts a tweening animation for a Unity UI Text component or TMPro text component. See /// UxrInterpolator.InterpolateText for /// information on how and work. /// /// Target GameObject with either a Unity UI Text component or Text Mesh Pro text component /// Interpolation settings that control the animation /// Optional callback when the animation finished. Use null to ignore. /// /// Format string. See /// UxrInterpolator.InterpolateText /// /// /// Format string arguments. See /// UxrInterpolator.InterpolateText /// /// /// Tweening component that will update itself automatically. Can be used to stop the animation prematurely or /// change parameters on the fly. /// public static UxrTextContentTween Animate(GameObject gameObject, UxrInterpolationSettings settings, Action finishedCallback, string formatString, object[] formatStringArgs) { UxrTextContentTween textContentTween = gameObject.GetOrAddComponent(); textContentTween.UsesFormatString = true; textContentTween.InterpolationSettings = settings; textContentTween.FinishedCallback = finishedCallback; textContentTween.FormatString = formatString; textContentTween.FormatStringArgs = formatStringArgs.ToList(); textContentTween.Restart(); return textContentTween; } #endregion #region Protected Overrides UxrTween /// protected override Behaviour TargetBehaviour { get { if (TargetText != null) { return TargetText; } #if ULTIMATEXR_UNITY_TMPRO if (TargetTextTMPro != null) { return TargetTextTMPro; } #endif return null; } } /// protected override void StoreOriginalValue() { _originalText = Text; } /// protected override void RestoreOriginalValue() { if (HasOriginalValueStored) { Text = _originalText; } } /// protected override void Interpolate(float t) { if (StartText == null || EndText == null) { return; } if (!UsesFormatString) { Text = UxrInterpolator.InterpolateText(StartText, EndText, t, true); } else { Text = UxrInterpolator.InterpolateText(t, true, FormatString, FormatStringArgs); } } #endregion #region Private Types & Data private string _originalText; #endregion } }