Skip to content

Commit

Permalink
fix(TextDeltaNumber): make it closer to delte number
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Aug 25, 2022
1 parent 6bccf01 commit 5a6c38d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
13 changes: 7 additions & 6 deletions Assets/JCSUnity/Scripts/UI/JCS_DeltaNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class JCS_DeltaNumber : MonoBehaviour
[SerializeField]
private JCS_TextAlign mTextAlign = JCS_TextAlign.RIGHT;

[Header("- Min/Max Settings (JCS_DeltaNumber)")]
[Header("- Min/Max (JCS_DeltaNumber)")]

[Tooltip("Maxinum number.")]
[SerializeField]
Expand All @@ -126,13 +126,13 @@ public class JCS_DeltaNumber : MonoBehaviour
[SerializeField]
private int mMinNumber = 0;

[Header("- Delta Animation Settings (JCS_DeltaNumber)")]
[Header("- Animation (JCS_DeltaNumber)")]

[Tooltip(@"This will make the number have the transition
between, setting to a new number. If you want the number set directly, you
should disable this effect for best purpose.")]
[SerializeField]
private bool mDeltaToCurrentNumber = false;
private bool mDeltaToCurrentNumber = true;

[Tooltip("Toward to this number, if mDeltaToCurrentScore active.")]
[SerializeField]
Expand All @@ -147,7 +147,8 @@ public class JCS_DeltaNumber : MonoBehaviour
private float mAnimNumberTimer = 0;

[Tooltip("How much the delta value add up.")]
[SerializeField] [Range(1, 1000)]
[SerializeField]
[Range(1, 1000)]
private int mDeltaProduct = 1;

/* Setter & Getter */
Expand Down Expand Up @@ -192,14 +193,14 @@ private void Awake()

private void Update()
{
#if (UNITY_EDITOR)
#if UNITY_EDITOR
Test();
#endif

DoDeltaCurrentNumber();
}

#if (UNITY_EDITOR)
#if UNITY_EDITOR
private void Test()
{
if (!mTestWithKey)
Expand Down
85 changes: 62 additions & 23 deletions Assets/JCSUnity/Scripts/UI/JCS_TextDeltaNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
#define TMP_PRO

using System;
using UnityEngine;
using UnityEngine.UI;

Expand Down Expand Up @@ -54,18 +55,10 @@ public class JCS_TextDeltaNumber : MonoBehaviour

[Header("** Check Variables (JCS_TextDeltaNumber) **")]

[Tooltip("Flag to check if is currently effecting.")]
[SerializeField]
private bool mActive = false;

[Tooltip("Full string to display.")]
[SerializeField]
private string mFullString = "";

[Tooltip("Target number to display, or to delta to.")]
[SerializeField]
private float mTargetNumber = 0.0f;

[Header("** Initialize Variables (JCS_TextDeltaNumber) **")]

[Tooltip("Target text renderer.")]
Expand Down Expand Up @@ -99,7 +92,29 @@ public class JCS_TextDeltaNumber : MonoBehaviour
[Tooltip("Place you want to round the decimal.")]
[SerializeField]
[Range(0, 15)]
private int mRoundPlace = 2;
private int mRoundPlace = 0;

[Header("- Min/Max (JCS_TextDeltaNumber)")]

[Tooltip("Maxinum number.")]
[SerializeField]
private int mMaxNumber = 999;

[Tooltip("Mininum number.")]
[SerializeField]
private int mMinNumber = 0;

[Header("- Animation (JCS_TextDeltaNumber)")]

[Tooltip(@"This will make the number have the transition
between, setting to a new number. If you want the number set directly, you
should disable this effect for best purpose.")]
[SerializeField]
private bool mDeltaToCurrentNumber = true;

[Tooltip("Target number to display, or to delta to.")]
[SerializeField]
private float mTargetNumber = 0.0f;

[Tooltip("How fast the number animate.")]
[SerializeField]
Expand All @@ -115,12 +130,23 @@ public class JCS_TextDeltaNumber : MonoBehaviour

/* Setter/Getter */

public bool Active { get { return this.mActive; } }
public float TargetNumber { get { return this.mTargetNumber; } }
public Text TextContainer { get { return this.mTextContainer; } set { this.mTextContainer = value; } }
#if TMP_PRO
public TextMeshPro TextMesh { get { return this.mTextMesh; } set { this.mTextMesh = value; } }
#endif
public bool DeltaToCurrentNumber { get { return this.mDeltaToCurrentNumber; } set { this.mDeltaToCurrentNumber = value; } }
public float TargetNumber
{
get { return this.mTargetNumber; }
set
{
this.mTargetNumber = value;

// by setting the delta number will enable the delta to current
// number effect.
this.mDeltaToCurrentNumber = true;
}
}
public string FullString { get { return this.mFullString; } }
public int RoundPlace { get { return this.mRoundPlace; } set { this.mRoundPlace = value; } }
public float CurrentNumber { get { return this.mCurrentNumber; } set { this.mCurrentNumber = value; } }
Expand Down Expand Up @@ -153,21 +179,37 @@ private void TestInput()
}
#endif

/// <summary>
/// Update the number GUI.
/// </summary>
public void UpdateNumber()
{
UpdateNumber(mCurrentNumber);
}

/// <summary>
/// Start the text delta number.
/// </summary>
/// <param name="targetNumber"> Number target to delt to. </param>
/// <param name="number"> Number target to delt to. </param>
/// <param name="anime"> Set the number directly. </param>
public void UpdateNumber(float targetNumber, bool anime = true)
public void UpdateNumber(float number, bool anime = false)
{
this.mTargetNumber = targetNumber;
float targetNumber = number;

if (targetNumber < mMinNumber)
targetNumber = mMinNumber;
else if (targetNumber > mMaxNumber)
targetNumber = mMaxNumber;

if (anime)
mActive = true;
if (mDeltaToCurrentNumber)
{
if (!anime)
mTargetNumber = targetNumber;
}
else
{
mActive = false; // To ensure, just deactive it.
this.mCurrentNumber = targetNumber;
// apply to current number
mCurrentNumber = targetNumber;
}
}

Expand All @@ -176,14 +218,11 @@ public void UpdateNumber(float targetNumber, bool anime = true)
/// </summary>
private void DoDeltaCurrentNumber()
{
if (!mActive)
if (!mDeltaToCurrentNumber)
return;

if (System.Math.Round(mTargetNumber, mRoundPlace) == System.Math.Round(mCurrentNumber, mRoundPlace))
{
mActive = false;
if (Math.Round(mTargetNumber, mRoundPlace) == Math.Round(mCurrentNumber, mRoundPlace))
return;
}

mAnimNumberTimer += Time.deltaTime;

Expand Down
4 changes: 4 additions & 0 deletions docs/ScriptReference/UI/JCS_TextDeltaNumber.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Like `JCS_DeltaNumber`, but instead of altering the sprite we alter text instead
| mPreString | String added before rendering the number." |
| mPostString | String added after rendering the number. |
| mRoundPlace | Place you want to round the decimal. |
| mMaxNumber | Maxinum number. |
| mMinNumber | Mininum number. |
| mDeltaToCurrentNumber | Show each digit between the number animation. |
| mTargetNumber | Current targeting number. |
| mAnimNumberTime | How fast the number animate. |
| mDeltaProduct | How much the delta value add up. |

Expand Down

0 comments on commit 5a6c38d

Please sign in to comment.