C# Unity2d:dontDestroyOnLoad是否只工作一次?
我正在做一个项目。我想有两种不同的场景。一个是商店场景,另一个是主要游戏场景。在主游戏场景中有一个显示我的钱的按钮。同样在这个场景中,我通过这个按钮挣钱和赚钱。然后我改变场景。在购物现场,我可以看到我赚的钱,我可以买东西。当然,我的钱减少了。然而,当我再次改变场景时,当我打开主游戏场景时,我的钱变成了0 如何使ShopScene和Maingame场景之间的金额相同C# Unity2d:dontDestroyOnLoad是否只工作一次?,c#,unity3d,C#,Unity3d,我正在做一个项目。我想有两种不同的场景。一个是商店场景,另一个是主要游戏场景。在主游戏场景中有一个显示我的钱的按钮。同样在这个场景中,我通过这个按钮挣钱和赚钱。然后我改变场景。在购物现场,我可以看到我赚的钱,我可以买东西。当然,我的钱减少了。然而,当我再次改变场景时,当我打开主游戏场景时,我的钱变成了0 如何使ShopScene和Maingame场景之间的金额相同 主游戏的拖放脚本,ShopScene的商店经理。Alsıclose按钮关闭商店并打开主游戏。在代码的末尾,我添加了不起作用的单例代码
主游戏的拖放脚本,ShopScene的商店经理。Alsıclose按钮关闭商店并打开主游戏。在代码的末尾,我添加了不起作用的单例代码看起来您有两张画布。一个在主游戏场景中,一个在商店场景中。如果你的游戏先在主游戏上开始,然后再在你的商店场景中开始。我建议您在唤醒方法中查找对象,而不是使用
GameObject.find()
创建一个完全独立的对象。这将确保选择正确的画布并将其放入DontDestroyOnLoad
,因为您似乎在主屏幕上用您不想要的0来保存主屏幕画布。
唤醒方法可能如下所示:
public class DragandDrop : MonoBehaviour
{
private static DragandDrop instance;
public GameObject box;
public GameObject table;
public Text scoreText;
public Button moneyBarButton;
public GameObject canvas;
public int money;
private bool isFit;
private bool isMoving;
private float startPosX;
private float startPosY;
private GameObject [] boxCountArray;
private GameObject[] moneyCountArray;
private Vector3 startPosition;
void Awake()
{
canvas = (GameObject.Find("DontDestroyCanvas"));
DontDestroyOnLoad(canvas);
if (instance == null)
{
instance = this;
DontDestroyOnLoad(canvas);
}
else if (instance != this)
Destroy(canvas);
}
那么你的代码就错了。除非您手动销毁,否则加载时不销毁对象将保留。我建议您使用Unity的PlayerPrefs来存储这些类型的值,因为我猜您不希望在刷新游戏后重置金钱。也请分享你的代码。@Harry我添加了我的代码。在主游戏场景中,一切正常。也在运行的时候我打开店景OK。但是,当我再次打开Maingame时,everyting变为0。如果您使用
DontDestroyOnLoad
,您应该同时确保a)没有多个实例同时出现(单例模式)和b)场景中的所有其他内容都可以在场景重新加载后再次获得引用->您可以“滥用”单例模式,并作为publicstatic
提供参考。当主游戏加载时,如果游戏中还没有画布,则有一个游戏管理器将实例化画布的预置。
public class DragandDrop : MonoBehaviour
{
public GameObject box;
public GameObject table;
public Text scoreText;
public Button moneyBarButton;
public Canvas canvas;
public int money;
private bool isFit;
private bool isMoving;
private float startPosX;
private float startPosY;
private GameObject [] boxCountArray;
private GameObject[] moneyCountArray;
private Vector3 startPosition;
void Awake()
{
DontDestroyOnLoad(canvas);
}
// Start is called before the first frame update
void Start()
{
startPosition = this.transform.localPosition;
moneyBarButton= GameObject.Find("MoneyBar").GetComponent<Button>();
}
// Update is called once per frame
void Update()
{
// moneyBarButton = GameObject.Find("MoneyBar").GetComponent<Button>();
SceneManager.SetActiveScene(SceneManager.GetSceneByName("Maingame"));
PlayerPrefs.GetInt("money");
moneyBarButton.GetComponentInChildren<Text>().text = Convert.ToString(money);
scoreText.text = "Score: " + money / 10;
boxCountArray = GameObject.FindGameObjectsWithTag("Box");
control();
scoreText.text = "Score: " + (boxCountArray.Length-1);//score artışı
money = (boxCountArray.Length - 1) * 10;
//moneyBarButton = GameObject.Find("MoneyBar").GetComponent<Button>();
}
public bool control() {
if (!isFit)
{
if (isMoving)
{
Vector3 mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.transform.localPosition.z);
return true;
}
return false;
}
return false;
}
void moneyIncrease() {
while (control() == true)
{
money += 10;
moneyBarButton.GetComponentInChildren<Text>().text = money.ToString();//para artışı
PlayerPrefs.SetInt("money", money);
break;
}
}
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
startPosX = mousePos.x - this.transform.localPosition.x;
startPosY = mousePos.y - this.transform.localPosition.y;
isMoving = true;
}
}
private void OnMouseUp()
{
isMoving = false;
if (Mathf.Abs(this.transform.localPosition.x - table.transform.localPosition.x) <= 0.5f &&
Mathf.Abs(this.transform.localPosition.y - table.transform.localPosition.y) <= 0.5f)
{
Vector3 position = new Vector3(UnityEngine.Random.Range(-11,1 ), UnityEngine.Random.Range(-3, 4), transform.position.z);//Yeni oluşacak boxun pozisyonunu random belirler.
this.transform.position = new Vector3(box.transform.position.x, box.transform.position.y, box.transform.position.z);
Instantiate(box,position ,Quaternion.identity);//Her kutu masaya yerleştiğinde yeni bir kutu oluşturur
isFit = true;
}
else
{
this.transform.localPosition = new Vector3(startPosition.x, startPosition.y, startPosition.z);
}
}
}
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);
}
}
public class DragandDrop : MonoBehaviour
{
private static DragandDrop instance;
public GameObject box;
public GameObject table;
public Text scoreText;
public Button moneyBarButton;
public GameObject canvas;
public int money;
private bool isFit;
private bool isMoving;
private float startPosX;
private float startPosY;
private GameObject [] boxCountArray;
private GameObject[] moneyCountArray;
private Vector3 startPosition;
void Awake()
{
canvas = (GameObject.Find("DontDestroyCanvas"));
DontDestroyOnLoad(canvas);
if (instance == null)
{
instance = this;
DontDestroyOnLoad(canvas);
}
else if (instance != this)
Destroy(canvas);
}
public void Awake()
{
canvas = GameObect.Find('Canvas');//Canvas would be replaced by the gameobjects name
DontDestroyOnLoad(canvas);
}