转自:
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();
}
}