在规定时间内,继续完成某个指令
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreKeep : MonoBehaviour
{
public int score { get; private set; }//外部可以访问,不能更改
[SerializeField] float streakExpiredTime = 2f;//可连击的持续时间
[SerializeField] int streakCount;
[SerializeField] Text combomText;
[SerializeField] Text scoreText;
[SerializeField] Image image;
float lastTime;
private void Awake()
{
combomText.gameObject.SetActive(false);
}
private void Start()
{
score = 0;
streakCount= 0;
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
KillEnemy();
}
ScoreUpdateUI();
}
//连击事件 更新数据
private void KillEnemy()
{
combomText.gameObject.SetActive(true);
StartCoroutine(nameof(UpDataCombomCoroutine));
if(Time.time <lastTime+streakExpiredTime)
{
streakCount++;
}
else
{
streakCount=1;
image.fillAmount= 1;
}
combomText.text = "Combom"+streakCount*100;
score+=streakCount*100;
lastTime= Time.time;
}
//连击协程
IEnumerator UpDataCombomCoroutine()
{
float percent = 1;
int currentStreakCount = streakCount;
while(percent> 0)
{
if(currentStreakCount==streakCount)//没有继续连击
{
percent-=Time.deltaTime/streakExpiredTime;
image.fillAmount= percent;
}
else
{
StopCoroutine(nameof(UpDataCombomCoroutine));
StartCoroutine(nameof(UpDataCombomCoroutine));
}
yield return null;
}
combomText.gameObject.SetActive(false);
}
//分数显示
void ScoreUpdateUI()
{
scoreText.text=score.ToString();
}
}
详细看
Unity简单的ComBo连击与分数的视觉滚动(附:Action委托与Lambda表达式、协程的开启与中止,LeanTween缓动函数曲线以及解决人物穿墙问题)_哔哩哔哩_bilibili