UNITY2D:如何用Singleton模式销毁以前的对象



我正在做一个项目。它包括两个场景:主游戏和商店场景。首先,我运行主游戏,有一个计时器,我赚钱。一天结束时,ShopScene会打开。我在ShopScene花钱。然后我点击退出按钮,主游戏再次打开。我用了"不要破坏物体"one_answers"辛格尔顿模式"。然而,我的问题是,一开始,第1天。我挣了一些钱。我在ShopScene花了一些钱。在这里,第二天是第二天,因为第一天结束了。又一次,我来到主游戏现场,但天又变成了1。麻生我的钱变成了0。我以为我的辛格尔顿图案毁了我的新画布。然而,我希望我的辛格尔顿图案破坏之前的画布。我该怎么做?我的密码在这里。

public class EarnMoney : MonoBehaviour
{
private static EarnMoney instance;
private int score;
public Button moneyBarButton;
public GameObject canvas;
public int money;
private GameObject[] boxCountArray;

// Start is called before the first frame update
void Awake()
{
canvas = (GameObject.Find("DontDestroyCanvas"));
//  DontDestroyOnLoad(canvas);
if (instance == null)
{
instance = this;
DontDestroyOnLoad(canvas);
}
else if (instance != this) {
Destroy(canvas);
}


}
void Start()
{
//  moneyBarButton = GameObject.Find("MoneyBar").GetComponent<Button>();
}
// Update is called once per frame
void Update()
{
SceneManager.SetActiveScene(SceneManager.GetSceneByName("Maingame"));
PlayerPrefs.GetInt("money");
moneyBarButton.GetComponentInChildren<Text>().text = Convert.ToString(money);
score = money / 10;
boxCountArray = GameObject.FindGameObjectsWithTag("Box");
// control();
score =(boxCountArray.Length - 1);//score artışı
money = (boxCountArray.Length - 1) * 10;
PlayerPrefs.SetInt("money", money);
}
}
public class ShopManager : MonoBehaviour
{
// Start is called before the first frame update
public Button itemBuyButton; //Almak istediğim itemin butonu.
public Button moneyBar; //Para kutucuğu
int price; //İslem yapabilmek için para kutucuğumdaki text i double a çeviricem. Bu değişken onu tutmak için.
public Transform floatingText; //item aldığımda aldığım itemin fiyatının ekranda çıkmasını temsil eden object
public Transform infoAboutShop; // item aldığımda itemden kaç tane aldığımı ekrana yazdıran object.
public GameObject canvas;


int n = 0;

void Start()
{
price = PlayerPrefs.GetInt("money");
moneyBar = GameObject.Find("MoneyBar").GetComponent<Button>();

} 
// Update is called once per frame
void Update()
{
//Butonların interaktifliği sürekli kontrol edilsin istediğimden ilgili methodu update metodumda çağırıyorum.

buttonInteractable();
PlayerPrefs.SetInt("money", price);
moneyBar.GetComponentInChildren<Text>().text = Convert.ToString(price);
}
public void buy()
{
//floatingText objemin TextMesh componentine ilgili itemin fiyatını atıyorum.
floatingText.GetComponent<TextMesh>().text = itemBuyButton.GetComponentInChildren<Text>().text;
/*
*Instantiate metodu clone yaratmaya yarıyor. floatingText objemden clone yaratıcam. Clonenun yaratılmasını istediğim yeri tıkladığım yer olarak belirttim.
*Quaternion identity ise rotation olmadan ilgili objenin clonelamasını sağlıyor. Bu cloneun TextMesh componentine de aynı şekilde ilgili itemin fiyatını atıyorum.
* Bu işlemleri buy metodunun içinde yapmamın nedeni de floatingText lerin item satın alındığında oluşacak olması.
*/
Instantiate(floatingText, new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z), Quaternion.identity).GetComponent<TextMesh>().text = itemBuyButton.GetComponentInChildren<Text>().text;

//Para kutucuk butonumun text componentini double a çevirip yukarıda oluşturduğum moneyBarPrice değişkenine atıyorum. İşlemleri bu değişken üzerinden yapacağım.
price = Convert.ToInt32(moneyBar.GetComponentInChildren<Text>().text);
//Butonun text componentine ulaşıp aynı şekilde o text i de kıyaslama yapabilmek için double a çeviriyorum.
if (price >= (Convert.ToDouble(itemBuyButton.GetComponentInChildren<Text>().text)))
{
// item aldıktan sonra itemin fiyatını total fiyatımdan düşüyorum.  
price -= (Convert.ToInt32(itemBuyButton.GetComponentInChildren<Text>().text));
// kalan fiyatımı string e çevirip para kutucuğuma yazıyorum.
moneyBar.GetComponentInChildren<Text>().text = Convert.ToString(price);

//infoAboutShop objemin TextMesh componentine ilgili itemin adını ve sayısını atıyorum.
infoAboutShop.GetComponent<TextMesh>().text = Convert.ToString(n + 1) + " " + itemBuyButton.name;
//floatingText teki mantıkla infoAboutShop objelerimi (200,200) konumunda clonelayıp ilgili nesnenin adı ve sayısını atıyorum.
Instantiate(infoAboutShop, new Vector2(200, 200), Quaternion.identity).GetComponent<TextMesh>().text = infoAboutShop.GetComponent<TextMesh>().text;
//her çağırıldığında ilgili objenin sayısını 1 artırıyorum.
n += 1;
}
else
{
// eğer iteme tıkladıktan sonra param tıkladığım itemi almaya yetmiyorsa itemin aktifliği engelleniyor.
itemBuyButton.interactable = false;
}
}
void buttonInteractable()
{
price= Convert.ToInt32(moneyBar.GetComponentInChildren<Text>().text);
// Debug.Log(price);
//Butonun text componentine ulaşıp aynı şekilde o text i de kıyaslama yapabilmek için double a çeviriyorum.
if (price >= (Convert.ToDouble(itemBuyButton.GetComponentInChildren<Text>().text)))
{
itemBuyButton.interactable = true; // Eğer  start butonu aktif eğilse ve param almak istediğim itemden fazlaysa butonun aktifliği devam eder.
}
else
{
itemBuyButton.interactable = false; //  param tıkladığım itemi almaya yetmiyorsa itemin aktifliği engelleniyor.
}
}


}
public class closedButton : MonoBehaviour
{
Scene scene;
public Button moneyBar;


// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{

}
public void ExitShop()
{
SceneManager.GetActiveScene();
SceneManager.LoadScene(0);


}
}

如果希望在场景加载时发生这种情况,请不要使用唤醒。而是使用OnSceneLoaded。

void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (string.equal(scene.name, "ShopScene")){
// Do whatever you want here when the scene loads
Destroy(canvas)
}
}

最新更新