Skip to content

Instantly share code, notes, and snippets.

@aprius
Last active November 29, 2022 08:38
Show Gist options
  • Save aprius/0b89b8dc7c312bd464374dad1fbb6031 to your computer and use it in GitHub Desktop.
Save aprius/0b89b8dc7c312bd464374dad1fbb6031 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
/// <summary>
/// observer : đạt thành tựu thu thập đủ 10000 coin
/// </summary>
public class CoinArchivementObserver : MonoBehaviour
{
private void Awake()
{
GameCenter.CurrencyChangedEvent += OnCurrencyChangedValue;
}
private void OnCurrencyChangedValue()
{
if (UserData.CurrentCoin == 10000)
{
// play âm thanh chúc mừng
// pháo hoa các kiểu
// hiện thông báo nhận thưởng ...
}
}
private void OnDestroy()
{
GameCenter.CurrencyChangedEvent -= OnCoinChangedValue;
}
}
using System;
using TMPro;
using UnityEngine;
public class CoinObserver : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI txtCoin;
private void Awake()
{
GameCenter.CurrencyChangedEvent += OnCurrencyChangedValue;
}
private void OnCurrencyChangedValue()
{
txtCoin.text = $"{UserData.CurrentCoin}";
}
private void OnDestroy()
{
GameCenter.CurrencyChangedEvent -= OnCoinChangedValue;
}
}
using System;
public static class GameCenter
{
// Action tương đương với việc sử dụng delegate void không tham số
// public delegate void CurrencyChangedDelegate();
// public static event CurrencyChangedDelegate CurrencyChangedEvent;
public static event Action CurrencyChangedEvent;
public static void OnCurrencyChangedEvent()
{
CurrencyChangedEvent?.Invoke();
}
}
using System;
public static class GameCenter
{
// Action tương đương với việc sử dụng delegate void không tham số
// public delegate void CurrencyChangedDelegate();
// public static event CurrencyChangedDelegate CurrencyChangedEvent;
public static event Action CurrencyChangedEvent;
public static void OnCurrencyChangedEvent()
{
CurrencyChangedEvent?.Invoke();
}
public static void Register(Action action)
{
CurrencyChangedEvent += action;
}
public static void UnRegister(Action action)
{
CurrencyChangedEvent -= action;
}
}
using System;
using TMPro;
using UnityEngine;
public class GemObserver : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI txtGem;
private void Awake()
{
GameCenter.CurrencyChangedEvent += OnCurrencyChangedValue;
}
private void OnCurrencyChangedValue()
{
txtGem.text = $"{UserData.CurrentGem}";
}
private void OnDestroy()
{
GameCenter.CurrencyChangedEvent -= OnGemChangedValue;
}
}
public static class UserData
{
private static long currentCoin;
private static int currentGem;
public static long CurrentCoin
{
get => currentCoin;
set
{
currentCoin = value;
GameCenter.CurrencyChangedInvoke();
}
}
public static int CurrentGem
{
get => currentGem;
set
{
currentGem = value;
GameCenter.CurrencyChangedInvoke();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment