当前位置:网站首页>Unity 数字跳字功能
Unity 数字跳字功能
2022-08-11 05:30:00 【canon_卡农】
使用场景
游戏中当玩家获得资源,或者战斗力提升的时候。数值直接变化往往不直观。
因此需要更直观的表现,比如增加一些动画效果,数字跳字等。
这里实现一个数字跳字的功能。
QQ录屏20220809150915
代码实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UpdateJumpText : MonoBehaviour
{
public Text m_JumpText;
public InputField m_inputField;
/// <summary>
/// 跳字计时
/// </summary>
public float mTimer = 0f;
/// <summary>
/// 跳字间隔
/// </summary>
public float mUpdateDeltaTime = 0.03f;
/// <summary>
/// 跳字时间
/// </summary>
public float mUpdateTime = 0.7f;
/// <summary>
/// 上一次显示数值
/// </summary>
int mLastValue;
/// <summary>
/// 当前显示数值
/// </summary>
int mCurValue = 0;
/// <summary>
/// 目标显示数值
/// </summary>
int mTargetValue = 0;
int mOffsetValue
{
get {
return mTargetValue - mLastValue; }
}
int mRangeParameter;
bool useRangeNum = true;
// Start is called before the first frame update
void Start()
{
m_inputField.onEndEdit.AddListener(OnInputFieldEndEdit);
mLastValue = mCurValue = mTargetValue = 0;
m_JumpText.text = mTargetValue.ToString();
}
public void OnInputFieldEndEdit(string input)
{
if (int.TryParse(input, out int inputNum))
{
mTargetValue += inputNum;
TextJump();
}
}
void TextJump()
{
if (mLastValue == mTargetValue)
return;
mTimer = 0;
if (mOffsetValue > 0)
{
if (!IsInvoking("UpdateValue"))
{
InvokeRepeating("UpdateValue", mUpdateDeltaTime, mUpdateDeltaTime);
}
}
else
{
m_JumpText.text = mTargetValue.ToString();
mLastValue = mCurValue = mTargetValue;
if (IsInvoking("UpdateValue"))
{
CancelInvoke("UpdateValue");
}
}
}
void UpdateValue()
{
mTimer += mUpdateDeltaTime;
if (mTimer < mUpdateTime)
{
// 是否使用随机数
if (useRangeNum)
{
mRangeParameter = (int)Mathf.Pow(10, mOffsetValue.ToString().Length);
mCurValue = (mTargetValue / mRangeParameter) * mRangeParameter + Random.Range(mRangeParameter / 10, mRangeParameter);
}
else
{
mCurValue = mLastValue + (int)((mOffsetValue) / mUpdateTime * mTimer);
}
m_JumpText.text = mCurValue.ToString();
}
else
{
m_JumpText.text = mTargetValue.ToString();
mLastValue = mCurValue = mTargetValue;
mTimer = 0f;
CancelInvoke("UpdateValue");
}
}
}
边栏推荐
- js learning advanced BOM part (pink teacher notes)
- 【LeetCode-147】对链表进行插入排序
- 【LeetCode-13】罗马数字转整数
- OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
- 【LeetCode-75】 颜色分类
- 【LeetCode-162】寻找峰值
- 127.0.0.1 已拒绝连接
- 微信小程序云开发项目wx-store代码详解
- JS case exercise (classic case of teacher pink)
- 【LeetCode-56】合并区间
猜你喜欢

The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly

The whole process of Tinker access --- configuration

8-byte standard request parsing during USB enumeration

C-自定义类型(结构体、枚举、联合)

无效的修订:3.18.1-g262b901-dirty

C语言-6月8日-求两个数的最小公倍数和最大公因数;判断一个数是否为完数,且打印出它的因子

Fourth Paradigm OpenMLDB optimization innovation paper was accepted by VLDB, the top international database association

stack stack

Jetpack之dataBinding

JVM学习四:垃圾收集器与内存回收策略
随机推荐
JVM学习四:垃圾收集器与内存回收策略
C语言-7月18日-二维数组的学习
第六届蓝帽杯 EscapeShellcode
Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
2021年vscode终端设置为bash模式
Some formulas for system performance and concurrency
【LeetCode-56】合并区间
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
【剑指offer系列】面试题日记(前期题)
JS case exercise (classic case of teacher pink)
2021-09-11 C language variables and memory allocation
Day 82
The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly
使用adb命令管理应用
Promise.race learning (judging the fastest execution of multiple promise objects)
本地服务配置内网穿透实现微信公众号整合
IndexError: index 9 is out of bounds for axis 0 with size 9;数组下标溢出问题
heap2 (tcache attack,house of orange)
C语言-6月10日-my_strcat函数的编写
Day 75