当前位置: 首页 > article >正文

Unity简易版成就系统

转自: 

 https://learn.u3d.cn/tutorial/achievements-onecredit?chapterId=63562b28edca72001f21d129#

脚本AchievementSystem 

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

public class AchievementSystem : MonoBehaviour
{
    public Achievement newVideoAmountAchievement;
    public Achievement subscriberAmountAchievement;

    public Transform achievementPanel;
    public Text achievementNameText;
    public Text achievementDescriptionText;

    int videoAmount;
    int subscriberAmount;

    private void Start()
    {
        FindObjectOfType<UIManager>().UploadNewVideoAction += UploadNewVideo;
        FindObjectOfType<UIManager>().GetNewSubscriberAction += GetNewSubscriber;
    }

    void UploadNewVideo()
    {
        if (newVideoAmountAchievement.unlocked)
            return;

        videoAmount += 5;
        if (videoAmount >= 100)
            PopNewAchievement(newVideoAmountAchievement);
    }

    void GetNewSubscriber()
    {
        if (subscriberAmountAchievement.unlocked)
            return;

        subscriberAmount += 5;
        if (subscriberAmount >= 100)
            PopNewAchievement(subscriberAmountAchievement);
    }

    void PopNewAchievement(Achievement ach)
    {
        achievementNameText.text = ach.achievementName;
        achievementDescriptionText.text = ach.achievementDescription;

        ach.unlocked = true;

        StartCoroutine(PopThePanel());
    }

    IEnumerator PopThePanel()
    {
        float percent = 0;
        float amount = 165f;

        while(percent < 1)
        {
            percent += Time.deltaTime / 1f;
            achievementPanel.position += Vector3.down * amount * Time.deltaTime / 1f;

            yield return null;
        }

        yield return new WaitForSeconds(1);

        percent = 0;
        while (percent < 1)
        {
            percent += Time.deltaTime / 1f;
            achievementPanel.position += Vector3.up * amount * Time.deltaTime / 1f;

            yield return null;
        }
    }
}

[System.Serializable]
public class Achievement
{
    public string achievementName;
    public string achievementDescription;
    public bool unlocked;
}

 脚本UIManager

using System;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    public Text newVideoText;
    public Text subscriberText;

    public int newVideoAmount;
    public int subscriberAmount;

    public event Action UploadNewVideoAction;
    public event Action GetNewSubscriberAction;

    public void UploadNewVideo()
    {
        newVideoAmount += 5;
        newVideoText.text = "VIDEO: " + newVideoAmount;

        if (UploadNewVideoAction != null)
            UploadNewVideoAction();
    }

    public void GetSubscriber()
    {
        subscriberAmount += 5;
        subscriberText.text = "SUBSCRIBER: " + subscriberAmount;

        if (GetNewSubscriberAction != null)
            GetNewSubscriberAction();
    }
}


http://www.kler.cn/a/379141.html

相关文章:

  • 使用正则表达式验证积累
  • 云轴科技ZStack在CID大会上分享VF网卡热迁移技术
  • 使用 Spring Boot 搭建 WebSocket 服务器实现多客户端连接
  • Puppeteer点击系统:解锁百度流量点击率提升的解决案例
  • Python代码解析:生成Jieba自定义词典
  • 九,数据类型存储
  • FPGA 开发相关的资源
  • 如何实现视频人声分离?实用方法分享
  • Windows 部署非安装版Redis
  • 代码随想录八股训练营 | 面试高频八股(测开部分)
  • MathType在Word中的安装与配置记录
  • IMU应用于监测进食
  • 【论文解读】Med-BERT: 用于疾病预测的大规模结构化电子健康记录的预训练情境化嵌入
  • 如何选择和利用低代码无代码平台实现企业数字化转型?
  • 【论文复现】ChatGPT多模态命名实体识别
  • 单例模式四种写法
  • 防抖与节流 - 2024最新版前端秋招面试短期突击面试题【100道】
  • 人脑与机器连接:神经科技的伦理边界探讨
  • 编程语言的设计模式
  • 使用 API 和离线库查询 IP 地址方法详解
  • Spring Boot 配置文件详解与最佳实践
  • WPF怎么通过RestSharp向后端发请求
  • [ 问题解决篇 ] 解决windows虚拟机安装vmtools报错-winserver2012安装vmtools及安装KB2919355补丁 (附离线工具)
  • uni-app发起请求以及请求封装,上传及下载功能(六)
  • 【Android 系统中使用CallStack类来追踪获取和操作调用栈信息】
  • ubuntu编译内核安装启动