经验条动画(连续升级动画播放)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ExperienceBarAnimation : MonoBehaviour
{
    public Image fillImage;
    private float nowExperience;//当前经验
    private float currentExperience;//增长的经验
    private int level;//当前等级
    float nowFill;//当前进度
    float lateFill;//目标进度

    void Start()
    {
        level = 1;
        nowExperience = 40;
        currentExperience = 0;
        nowFill = nowExperience / CalculateNextLevelExperience();
        fillImage.fillAmount = nowFill;
    }

    public void UpdateExperience(float addExperience)
    {
        currentExperience = addExperience;

        if (currentExperience > 0)
        {
            nowFill = fillImage.fillAmount == 1 ? 0 : fillImage.fillAmount;
            //够不够升一级
            if (currentExperience >= CalculateNextLevelExperience() * (1 - nowFill))
            {
                currentExperience -= CalculateNextLevelExperience() * (1 - nowFill);

                lateFill = 1;
                level++;
                StartCoroutine(AnimateExperienceBar(nowFill, lateFill));
            }
            else
            {
                nowExperience = CalculateNextLevelExperience() * (nowFill);
                lateFill = nowFill == 0 ? currentExperience / CalculateNextLevelExperience() : (currentExperience + nowExperience) / CalculateNextLevelExperience();
                currentExperience = 0;
                StartCoroutine(AnimateExperienceBar(nowFill, lateFill));
            }
        }
    }

    private float CalculateNextLevelExperience()
    {
        // 这里可以根据您的需求自定义每个等级所需的经验值
        return level * 100f;
    }

    private IEnumerator AnimateExperienceBar(float now, float late)
    {
        float fillAmount = now;
        float timeElapsed = 0;
        float duration = 1.0f; //动画持续时间,可以根据需要调整
        while (timeElapsed < duration)
        {
            timeElapsed += Time.deltaTime;
            fillImage.fillAmount = Mathf.Lerp(fillAmount, late, timeElapsed / duration);
            yield return null;
        }
        if (currentExperience > 0)
        {
            UpdateExperience(currentExperience);
        }
        Debug.Log("fillImage.fillAmount+" + late);
        fillImage.fillAmount = late;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            UpdateExperience(400);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值