Last active
November 29, 2022 08:38
-
-
Save aprius/0b89b8dc7c312bd464374dad1fbb6031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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