シーンにPlayerとEnemyがあり、1人のプレイヤーが移動するとFirebaseが完全に速く正確にFirebaseを更新します。しかし、他のプレイヤーの敵を更新する場合、時間がかかります。敵アップデータのための
コード:UnityでFirebaseの値を取得するスピードアップ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
public class enemyUpdater : MonoBehaviour {
private DatabaseReference dataBase;
private bool gameStarted = false;
private float Z;
private float X;
// Use this for initialization
void Start() {
// Set up the Editor before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("Not Showing This");
// Get the root reference location of the database.
dataBase = FirebaseDatabase.DefaultInstance.RootReference;
setup();
}
// Update is called once per frame
void FixedUpdate() {
this.gameObject.transform.position = new Vector3(X, this.gameObject.transform.position.y, Z);
}
void HandleU1XChanged(object sender, ValueChangedEventArgs args)
{
if (args.DatabaseError != null)
{
Debug.LogError(args.DatabaseError.Message);
return;
}
string tmp = args.Snapshot.Value.ToString();
float.TryParse(tmp, out X);
}
void HandleU1ZChanged(object sender, ValueChangedEventArgs args)
{
if (args.DatabaseError != null)
{
Debug.LogError(args.DatabaseError.Message);
return;
}
string tmp = args.Snapshot.Value.ToString();
float.TryParse(tmp, out Z);
}
void HandleU2XChanged(object sender, ValueChangedEventArgs args)
{
if (args.DatabaseError != null)
{
Debug.LogError(args.DatabaseError.Message);
return;
}
string tmp = args.Snapshot.Value.ToString();
float.TryParse(tmp, out X);
}
void HandleU2ZChanged(object sender, ValueChangedEventArgs args)
{
if (args.DatabaseError != null)
{
Debug.LogError(args.DatabaseError.Message);
return;
}
string tmp = args.Snapshot.Value.ToString();
float.TryParse(tmp, out Z);
}
void setup()
{
if (multiSetup.isUserOne)
{
this.gameObject.transform.position = new Vector3(7.5f, this.gameObject.transform.position.y, -7.5f);
FirebaseDatabase.DefaultInstance
.GetReference("matches").Child("rooms").Child("room"+multiSetup.roomNum).Child("user2").Child("Xaxis")
.ValueChanged += HandleU1XChanged;
FirebaseDatabase.DefaultInstance
.GetReference("matches").Child("rooms").Child("room" + multiSetup.roomNum).Child("user2").Child("Zaxis")
.ValueChanged += HandleU1ZChanged;
}
else if (!multiSetup.isUserOne)
{
this.gameObject.transform.position = new Vector3(-7.5f, this.gameObject.transform.position.y, 7.5f);
FirebaseDatabase.DefaultInstance
.GetReference("matches").Child("rooms").Child("room" + multiSetup.roomNum).Child("user1").Child("Xaxis")
.ValueChanged += HandleU1XChanged;
FirebaseDatabase.DefaultInstance
.GetReference("matches").Child("rooms").Child("room" + multiSetup.roomNum).Child("user1").Child("Zaxis")
.ValueChanged += HandleU1ZChanged;
}
}
}
ここでのGIFアクションに
と同様に遅れFirebaseため、ローカルクライアントがすぐに更新した理由がある https://drive.google.com/open?id=1tv4BCfVsV6AIjEiKE1Umj4FfIyy-AG-R