语言:C#4 软体:Unity 2018.1 数据库:Firebase数据库
问题:我正在尝试使用插件“简单的Firebase Unity(用于实时数据库)”将预制件推送到Firebase数据库中
我的数据库已成功与我的Unity项目通信;开箱即用的好插件。
我假设我需要'firebaseQueue.AddQueuePush'来推送预制的'TextMesh',但是我不确定所涉及的代码。我应该使用“脚本对象”类而不是“ MonoBehaviour”类吗?任何帮助表示赞赏。
代码如下:
// Last update: 2018-05-20 (by Dikra)
using UnityEngine;
using SimpleFirebaseUnity;
using SimpleFirebaseUnity.MiniJSON;
using System.Collections.Generic;
using System.Collections;
using System;
public class SampleScript : MonoBehaviour {
static int debug_idx = 0;
[SerializeField]
TextMesh textMesh;
// Use this for initialization
void Start()
{
textMesh.text = "";
StartCoroutine(Tests());
}
void GetOKHandler(Firebase sender, DataSnapshot snapshot)
{
DebugLog("[OK] Get from key: <" + sender.FullKey + ">");
DebugLog("[OK] Raw Json: " + snapshot.RawJson);
Dictionary<string, object> dict = snapshot.Value<Dictionary<string, object>>();
List<string> keys = snapshot.Keys;
if (keys != null)
foreach (string key in keys)
{
DebugLog(key + " = " + dict[key].ToString());
}
}
void GetFailHandler(Firebase sender, FirebaseError err)
{
DebugError("[ERR] Get from key: <" + sender.FullKey + ">, " + err.Message + " (" + (int)err.Status + ")");
}
void SetOKHandler(Firebase sender, DataSnapshot snapshot)
{
DebugLog("[OK] Set from key: <" + sender.FullKey + ">");
}
void SetFailHandler(Firebase sender, FirebaseError err)
{
DebugError("[ERR] Set from key: <" + sender.FullKey + ">, " + err.Message + " (" + (int)err.Status + ")");
}
void UpdateOKHandler(Firebase sender, DataSnapshot snapshot)
{
DebugLog("[OK] Update from key: <" + sender.FullKey + ">");
}
void UpdateFailHandler(Firebase sender, FirebaseError err)
{
DebugError("[ERR] Update from key: <" + sender.FullKey + ">, " + err.Message + " (" + (int)err.Status + ")");
}
void DelOKHandler(Firebase sender, DataSnapshot snapshot)
{
DebugLog("[OK] Del from key: <" + sender.FullKey + ">");
}
void DelFailHandler(Firebase sender, FirebaseError err)
{
DebugError("[ERR] Del from key: <" + sender.FullKey + ">, " + err.Message + " (" + (int)err.Status + ")");
}
void PushOKHandler(Firebase sender, DataSnapshot snapshot)
{
DebugLog("[OK] Push from key: <" + sender.FullKey + ">");
}
void PushFailHandler(Firebase sender, FirebaseError err)
{
DebugError("[ERR] Push from key: <" + sender.FullKey + ">, " + err.Message + " (" + (int)err.Status + ")");
}
void GetRulesOKHandler(Firebase sender, DataSnapshot snapshot)
{
DebugLog("[OK] GetRules");
DebugLog("[OK] Raw Json: " + snapshot.RawJson);
}
void GetRulesFailHandler(Firebase sender, FirebaseError err)
{
DebugError("[ERR] GetRules, " + err.Message + " (" + (int)err.Status + ")");
}
void GetTimeStamp(Firebase sender, DataSnapshot snapshot)
{
long timeStamp = snapshot.Value<long>();
DateTime dateTime = Firebase.TimeStampToDateTime(timeStamp);
DebugLog("[OK] Get on timestamp key: <" + sender.FullKey + ">");
DebugLog("Date: " + timeStamp + " --> " + dateTime.ToString());
}
void DebugLog(string str)
{
Debug.Log(str);
if (textMesh != null)
{
textMesh.text += (++debug_idx + ". " + str) + "\n";
}
}
void DebugWarning(string str)
{
Debug.LogWarning(str);
if (textMesh != null)
{
textMesh.text += (++debug_idx + ". " + str) + "\n";
}
}
void DebugError(string str)
{
Debug.LogError(str);
if (textMesh != null)
{
textMesh.text += (++debug_idx + ". " + str) + "\n";
}
}
IEnumerator Tests()
{
// README
DebugLog("This plugin simply wraps Firebase's RealTime Database REST API.\nPlease read here for better understanding of the API: https://firebase.google.com/docs/reference/rest/database/\n");
// Inits Firebase using Firebase Secret Key as Auth
// The current provided implementation not yet including Auth Token Generation
// If you're using this sample Firebase End,
// there's a possibility that your request conflicts with other simple-firebase-c# user's request
Firebase firebase = Firebase.CreateNew("https://howardstern.com/", "AJ333669Een6BabaBooey!C66WTtJ3633366");
// Init callbacks
firebase.OnGetSuccess += GetOKHandler;
firebase.OnGetFailed += GetFailHandler;
firebase.OnSetSuccess += SetOKHandler;
firebase.OnSetFailed += SetFailHandler;
firebase.OnUpdateSuccess += UpdateOKHandler;
firebase.OnUpdateFailed += UpdateFailHandler;
firebase.OnPushSuccess += PushOKHandler;
firebase.OnPushFailed += PushFailHandler;
firebase.OnDeleteSuccess += DelOKHandler;
firebase.OnDeleteFailed += DelFailHandler;
// Get child node from firebase, if false then all the callbacks are not inherited.
Firebase temporary = firebase.Child("temporary", true);
Firebase lastUpdate = firebase.Child("lastUpdate");
lastUpdate.OnGetSuccess += GetTimeStamp;
// Make observer on "last update" time stamp
FirebaseObserver observer = new FirebaseObserver(lastUpdate, 1f);
observer.OnChange += (Firebase sender, DataSnapshot snapshot) =>
{
DebugLog("[OBSERVER] Last updated changed to: " + snapshot.Value<long>());
};
observer.Start();
DebugLog("[OBSERVER] FirebaseObserver on " + lastUpdate.FullKey + " started!");
// Print details
DebugLog("Firebase endpoint: " + firebase.Endpoint);
DebugLog("Firebase key: " + firebase.Key);
DebugLog("Firebase fullKey: " + firebase.FullKey);
DebugLog("Firebase child key: " + temporary.Key);
DebugLog("Firebase child fullKey: " + temporary.FullKey);
// Unnecessarily skips a frame, really, unnecessary.
yield return null;
// Create a FirebaseQueue
FirebaseQueue firebaseQueue = new FirebaseQueue(true, 3, 1f); // if _skipOnRequestError is set to false, queue will stuck on request Get.LimitToLast(-1).
// Test #1: Test all firebase commands, using FirebaseQueueManager
// The requests should be running in order
firebaseQueue.AddQueueSet(firebase, GetSampleScoreBoard(), FirebaseParam.Empty.PrintSilent());
firebaseQueue.AddQueuePush(firebase.Child("broadcasts", true), "{ \"name\": \"simple-firebase-csharp\", \"message\": \"awesome!\"}", true);
// ********这是将预制件推到Firebase的地方**********
firebaseQueue.AddQueuePush(firebase, ("textMesh", true));
firebaseQueue.AddQueueSetTimeStamp(firebase, "lastUpdate");
firebaseQueue.AddQueueGet(firebase, "print=pretty");
firebaseQueue.AddQueueUpdate(firebase.Child("layout", true), "{\"x\": 5.8, \"y\":-94}");
firebaseQueue.AddQueueGet(firebase.Child("layout", true));
firebaseQueue.AddQueueGet(lastUpdate);