我有一个在其中声明了字符串snapshotJson的void Start(),并且我有一个私有的void LoadGameData(),它需要调用snapshotJson的值。我假设由于无效,所以无法公开宣布快照Json。从我的阅读中,我应该使用getter和setter,但是我不知道它们如何工作,并且我阅读的每本解释它的指南都使它看起来非常简单,但是他们对它的解释如此简单,以至于我不明白我到底是怎么做的。 m应该使用它,或者使用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;
}
答案 0 :(得分:1)
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相同问题的人。
答案 1 :(得分:0)
如果在 var file = "valoreETH.txt";
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
var rightValueETH = allText.substring(0,allText.length-6);
document.getElementById("cellaValoreETH").innerHTML = "€ " + rightValueETH;
}
}
}
rawFile.send(null);
let price = require('crypto-price'); //libreria per prezzo ETH
var fs = require('fs'); //libreria file system
var res;
price.getCryptoPrice('EUR', 'ETH').then(obg => {
console.log(JSON.parse(obg.price)); // It will print ETH rate per BTC
res = JSON.parse(obg.price);
fs.writeFile("valoreETH.txt", res, function(err) { //salvataggio valore ETH in prova.txt
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}).catch(err => {
console.log(err);
});
方法的主体内声明了变量snapshotJson
,则只能在该方法内访问它。如果希望该变量在类的其他方法中可访问,则可能需要将其声明为成员变量。这就是它的样子。仅当您需要在类实例之外访问Start
的值时,才需要将其声明为公共属性。
snapshotJson
答案 2 :(得分:0)
假设您引用的是方法而不是类(或者是:在对象之间共享数据),那么您可以这样做。这是Game对象专用类的示例,其中SnaphopJson
是一个公共属性,可以从任何其他对象进行访问和更改。将设置器更改为private
将确保只能从此类对象以外的任何对象读取该设置器。
public class Game
{
public string SnapshotJson { get; set; }
private void LoadGameData()
{
// load the json, e.g. by deserialization
SnapshotJson = "{}";
}
public void Start()
{
// access the content of your snapshot
var s = SnapshotJson;
}
}