54 lines
No EOL
1.3 KiB
C#
54 lines
No EOL
1.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Patterns_And_Principles.Patterns.The_Singleton_Pattern.AdvanceSingleton.Script
|
|
{
|
|
public class AdvancedSingleton : MonoBehaviour
|
|
{
|
|
private static AdvancedSingleton _instance;
|
|
|
|
public static AdvancedSingleton Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
SetupInstance();
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public int Gem = 0;
|
|
|
|
private void Awake()
|
|
{
|
|
if (_instance != null && _instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
_instance = this;
|
|
DontDestroyOnLoad(gameObject); // Sahne geçişlerinde yok olmaz
|
|
}
|
|
|
|
private static void SetupInstance()
|
|
{
|
|
_instance = FindAnyObjectByType<AdvancedSingleton>();
|
|
if (_instance == null)
|
|
{
|
|
GameObject gameObject = new GameObject(); // Yeni gameobject oluştur.
|
|
gameObject.name = "AdvancedSingleton";
|
|
_instance = gameObject.AddComponent<AdvancedSingleton>();
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
}
|
|
|
|
public void AddGem(int amount)
|
|
{
|
|
Gem += amount;
|
|
}
|
|
}
|
|
} |