JeCodeLeSoir

Exemple

Jul 1st, 2024
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. public enum GameColor
  6. {
  7.     Red,
  8.     Green,
  9.     Blue
  10. }
  11.  
  12. public class Singleton<T>: MonoBehaviour
  13. {
  14.     private static T instance;
  15.     public static T Instance
  16.     {
  17.         get
  18.         {
  19.             if (instance == null)
  20.             {
  21.                 instance = (T)(object)FindObjectOfType(typeof(T));
  22.             }
  23.             return instance;
  24.         }
  25.     }
  26. }
  27.  
  28. public class GameManager : Singleton<GameManager>
  29. {
  30.     [SerializeField] public GameColor Color;
  31.  
  32.     public void OnLost()
  33.     {
  34.         Debug.Log("You lost!");
  35.     }
  36.  
  37.     public void OnWin()
  38.     {
  39.         Debug.Log("You win!");
  40.     }
  41.  
  42.     public event Action OnLostEvent;
  43.     public event Action OnWinEvent;
  44.  
  45.     private void OnEnable()
  46.     {
  47.         OnLostEvent += OnLost;
  48.         OnWinEvent += OnWin;
  49.     }
  50.  
  51.     private void OnDisable()
  52.     {
  53.         OnLostEvent -= OnLost;
  54.         OnWinEvent -= OnWin;
  55.     }
  56.  
  57.     private bool CheckIsWin(GameColor color)
  58.     {
  59.         return Color == color;
  60.     }
  61.  
  62.     public void onClickCheckIsWin(GameColor color)
  63.     {
  64.         if (CheckIsWin(color))
  65.         {
  66.             OnWinEvent?.Invoke();
  67.             return;
  68.         }
  69.         OnLostEvent?.Invoke();
  70.     }
  71. }
  72.  
  73. public class ButtonColor : MonoBehaviour
  74. {
  75.     [SerializeField] GameColor color;
  76.     [HideInInspector] [SerializeField] Button Button;
  77.  
  78.     private void Reset()
  79.     =>
  80.         this.Button = GetComponent<Button>();
  81.  
  82.     private void OnEnable()
  83.     =>
  84.        this.Button.onClick.AddListener(() => GameManager.Instance.onClickCheckIsWin(this.color));
  85.    
  86.  
  87.     private void OnDisable()
  88.     =>
  89.         this.Button.onClick.RemoveListener(() => GameManager.Instance.onClickCheckIsWin(this.color));
  90. }
Advertisement
Add Comment
Please, Sign In to add comment