c# - 如何在 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;
}
解决方案
该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 遇到相同问题的人。
推荐阅读
- git - 使用 Visual Studio Code 中的颜色装饰探索源代码管理文件更改
- reactjs - url 正在更改,但在 react-router 中没有查看
- android - 使用 android 意图开始导航
- oracle - ORA-01653: 无法在表空间 SYSAUX 中将表 SYS.SQLOBJ$PLAN 扩展 128
- jquery - 使用 Flot 绘图:渲染的空白区域
- python-3.x - 如何在 AWS MWAA Airflow DAG 上获得 pyodbc 连接?
- spring-cloud-function - 带有 Apache Kafka 触发器的 AWS Lambda 返回“问题:Lambda 内部错误。请联系 Lambda 客户支持。”
- button - 谷歌表格提交按钮,用于根据列数据将数据行传输到适当的谷歌表格
- arduino - NodeMCU esp8266 不断断开我的笔记本电脑与路由器的连接
- swift - SwiftUI 按钮大小/形状/颜色不变