【发布时间】:2020-01-02 14:29:45
【问题描述】:
我要做的是从 linerender 获取所有位置并将其保存为 vector3 的列表,而不是将其保存在 firebase 中,到目前为止我所做的是获取数据我从firebase保存而不是再次将其保存在vector3列表中,所以我可以将它放在setposition linerender中我尝试这样做,但我总是得到一个空结果我不知道为什么!
问题是我不知道如何获取数据我总是得到一个空结果
这是我的代码
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Path : MonoBehaviour
{
[SerializeField] private List<Transform> checkpoints = new List<Transform>();
private LineRenderer linerenderer;
public Material TheLineMateriel;
public static bool _ispressed = false;
private string DATA_URL = "https://kataraproject-a233a.firebaseio.com/";
private DatabaseReference reference;
Player playerInstance = new Player();
List<GetPosition> tmpList = new List<GetPosition>();
// Start is called before the first frame update
void Start()
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(DATA_URL);
reference = FirebaseDatabase.DefaultInstance.RootReference;
GameObject lineObject = new GameObject();
this.linerenderer = lineObject.AddComponent<LineRenderer>();
this.linerenderer.startWidth = 0.05f;
this.linerenderer.endWidth = 0.05f;
this.linerenderer.positionCount = checkpoints.Count;
this.linerenderer.material = TheLineMateriel;
}
// Update is called once per frame
void Update()
{
this.DrawLine();
}
private void DrawLine()
{
Vector3[] checkpointsArray = new Vector3[this.checkpoints.Count];
for (int i = 0; i < this.checkpoints.Count; i++) {
Vector3 checkpointPos = this.checkpoints[i].position;
checkpointsArray[i] = new Vector3(checkpointPos.x, checkpointPos.y, 0f);
}
Vector3[] newPos = new Vector3[linerenderer.positionCount];
this.linerenderer.SetPositions(checkpointsArray);
linerenderer.GetPositions(newPos);
if ( _ispressed == true)
{
playerInstance.Position = newPos;
writeNewPosition("1");
_ispressed = false;
}
}
public void PostPosition ()
{
_ispressed = true;
}
public void GetPosition()
{
Firebase.Database.FirebaseDatabase dbInstance = Firebase.Database.FirebaseDatabase.DefaultInstance;
dbInstance.GetReference("Positions").GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
// Handle the error...
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
foreach (DataSnapshot user in snapshot.Children)
{
IDictionary dictUser = (IDictionary)user.Value;
GetPosition newplayer = new GetPosition(dictUser);
tmpList.Add(newplayer);
foreach (var item in dictUser.Values)
{
Debug.Log(item);
Debug.Log("" + dictUser["Position"] );
}
}
}
});
}
private void writeNewPosition(string positionId)
{
string json = JsonUtility.ToJson(playerInstance);
reference.Child("Positions").Child(positionId).SetRawJsonValueAsync(json);
}
}
有人可以帮我吗,对不起我的英语
【问题讨论】:
-
你最初是如何将它保存到 firebase 的
-
private void writeNewPosition(string positionId) { string json = JsonUtility.ToJson(playerInstance); reference.Child("Positions").Child(positionId).SetRawJsonValueAsync(json); }
-
writeNewPosition("1");
-
OK 所以你需要读回“1”,这将包含你存储的 playerInstance 的所有设置,如果你不想覆盖它并且只使用一些设置制作 Player 结构的新副本并将接收到的 json 设置为那个,然后你就可以挑选出你想要的 - 你检查了 firebase 以确认你期望的内容吗?