首页 > 解决方案 > 如何在 C# 中将值从一个类发送到另一个类?

问题描述

我在其中声明了带有字符串 snapshotJson 的 void Start(),并且我有需要调用 snapshotJson 值的私有 void LoadGameData()。声明 snapshotJson public 不起作用,我认为是因为无效。从我读到的内容来看,我应该使用 getter 和 setter,但我不知道它们是如何工作的,而且我读过的每一个解释它的指南都让它看起来很简单,但它们解释得如此简单,我不明白我到底是怎么做到的我应该使用它,或者在使用 get/set 函数后如何调用该值。

谁能解释我如何将变量从一个类传递到另一个类?在我的代码中,LoadGameData 无法调用 snapshotJson 的值,我不确定我缺少什么。

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

using Firebase;
using Firebase.Unity.Editor;
using Firebase.Database;
using System;

public class DataController : MonoBehaviour
{

private RoundData[] allRoundData;
private PlayerProgress playerProgress;

[Serializable]
public class FirebaseStart : MonoBehaviour
{
    public string snapshotJson { get; set; }

    void Start()
    {


            // Set up the Editor before calling into the realtime database.

FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://FIREBASEDATABASE");

        // Get the root reference location of the database.
        DatabaseReference reference = 
FirebaseDatabase.DefaultInstance.RootReference;

        FirebaseDatabase.DefaultInstance
         .GetReference("allRoundData")
          .GetValueAsync().ContinueWith(task => {
              if (task.IsFaulted)
              {
                  // Handle the error...
              }
              else if (task.IsCompleted)
              {
                 //  DataSnapshot snapshot = task.Result;
                snapshotJson = JsonUtility.ToJson(task.Result);

              }
          });
    }
}

// Use this for initialization
void Start ()
{
    DontDestroyOnLoad(gameObject);
    LoadGameData();
    LoadPlayerProgress();

    SceneManager.LoadScene("MenuScreen");
}

public RoundData GetCurrentRoundData()
{
    return allRoundData [0];
}

public void SubmitNewPlayerScore(int newScore)
{
    if (newScore > playerProgress.highestScore)
    {
        playerProgress.highestScore = newScore;
        SavePlayerProgress();
    }
}

public int GetHighestPlayerScore()
{
    return playerProgress.highestScore;
}
// Update is called once per frame
void Update () {

}

private void LoadPlayerProgress()
{
    playerProgress = new PlayerProgress();

    if (PlayerPrefs.HasKey("highestScore"))
    {
        playerProgress.highestScore = PlayerPrefs.GetInt("highestScore");
    }
}
private void SavePlayerProgress()
{
    PlayerPrefs.SetInt("highestScore", playerProgress.highestScore);
}

public void LoadGameData()
{

    GameData loadedData = JsonUtility.FromJson<GameData>(snapshotJson);
    Console.WriteLine(snapshotJson);
   allRoundData = loadedData.allRoundData;


}

标签: c#

解决方案


LoadGameData()方法无法从该Main()方法访问它,因为它在该函数范围内是本地的。但是,您可以使用以下代码将值从Main()方法传递给:LoadGameData()

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    private static void LoadGameData(String input) {
        // Do something inside your input here.
    }

    public static void Main() {
      Start();
    }

    public static void Start()
    {
        string snapshotJson = "Sample data";

        // Same Class Call
        LoadGameData(snapshotJson);

        // Other Class Call
        var otherClassInstance = new TestClass();

        otherClassInstance.LoadGameData(snapshotJson);
    }
}

public class TestClass {
    public void LoadGameData(String input) {
        // Do something inside your input here.
    }
}

温馨提示,如果您觉得答案有帮助,请点击复选框将其标记为已接受答案。这将帮助其他遇到与您在 atm 遇到相同问题的人。


推荐阅读