【问题标题】:Unity C# Passing Data Between Scenes [duplicate]Unity C#在场景之间传递数据[重复]
【发布时间】:2017-08-15 04:56:30
【问题描述】:

我目前正在为我的游戏开发一个应用内购买商店,该商店位于不同的场景中。我遇到的问题是,当我单击一个按钮时,例如针对白皮肤之类的新皮肤,在那个场景中,它会保存“didwhiteskin”的布尔值变为真的数据,尽管当我加载游戏场景时它不会保存这些数据因此不要在 if 语句中运行什么。感谢您的帮助,如果需要,我会回答任何问题。

附加信息:当按钮单击 StoreView 时调用函数 ChoseWhiteSkin()。

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class StoreScript : MonoBehaviour {

    bool didwhiteskin = false;

    public static StoreScript Instance {
        get;
        set;
    }

    void Awake () {
        DontDestroyOnLoad (transform.gameObject);
        Instance = this;
    }

    // Use this for initialization
    void Start () {
        Scene currentScene = SceneManager.GetActiveScene ();
        string sceneName = currentScene.name;

        if (sceneName == "GameScene") {
            if (didwhiteskin) {
                Debug.Log ("this ran");
                //This where I will put function to change skin but the issue is the if statement never being ran
            }
        }
        else if (sceneName == "StoreView") {

        }
    }

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

    }

    public void ChoseWhiteSkin() {
        PlayerPrefs.Save();
        didwhiteskin = true;
    }
}

【问题讨论】:

  • 在提问前谷歌您的问题。这不是一件难事。这个问题最糟糕的部分是它的标题与重复的问题相同。

标签: c# unity3d scene saving-data


【解决方案1】:

您可以像这样为所有场景定义一个全局变量:

public static int didwhiteskin = 0;

因此,您可以在游戏的第一个场景中声明和初始化此变量,然后在您可能创建的任何其他场景中引用它。

【讨论】:

  • 如何使该变量与 if 语句一起使用?
  • 想通了谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多