C# 查看FireBase中的数据并将其保存在LineRender.setposition()中

C# 查看FireBase中的数据并将其保存在LineRender.setposition()中,c#,list,firebase,unity3d,C#,List,Firebase,Unity3d,为什么我要做的是从linerender中获取所有位置,并将其保存在矢量列表中,而不是保存在firebase中我做到了,到目前为止,我要做的是获取从firebase保存的数据,而不是再次将其保存在矢量列表中,这样我就可以将其放置在setposition linerender中。我尝试这样做,但我总是得到一个空结果不知道为什么 问题是我没有弄清楚如何获取数据,我总是得到一个空结果 这是我的密码 using Firebase; using Firebase.Database; using Fireb

为什么我要做的是从linerender中获取所有位置,并将其保存在矢量列表中,而不是保存在firebase中我做到了,到目前为止,我要做的是获取从firebase保存的数据,而不是再次将其保存在矢量列表中,这样我就可以将其放置在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;
使用Firebase.Database;
使用Firebase.Unity.Editor;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类路径:单一行为
{
[SerializeField]私有列表检查点=新列表();
专用线条渲染器线条渲染器;
公共物资;线性物资;
公共静态bool_ispressed=false;
私有字符串数据\u URL=”https://kataraproject-a233a.firebaseio.com/";
私人数据库参考;
Player playerInstance=新玩家();
List tmpList=新列表();
//在第一帧更新之前调用Start
void Start()
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(数据URL);
reference=FirebaseDatabase.DefaultInstance.RootReference;
GameObject lineObject=新建GameObject();
this.linerenderer=lineObject.AddComponent();
this.linerenderer.startWidth=0.05f;
this.linerenderer.endWidth=0.05f;
this.linerenderer.positionCount=检查点.Count;
this.linerenderer.material=线性物料;
}
//每帧调用一次更新
无效更新()
{
这是一条绳();
}
专用抽绳()
{
Vector3[]checkpointsArray=newVector3[this.checkpoints.Count];
for(int i=0;i{
if(task.IsFaulted)
{
//处理错误。。。
}
else if(任务已完成)
{
DataSnapshot快照=task.Result;
foreach(snapshot.Children中的DataSnapshot用户)
{
IDictionary dictUser=(IDictionary)user.Value;
GetPosition newplayer=新GetPosition(用户);
tmpList.Add(newplayer);
foreach(dictUser.Values中的变量项)
{
调试日志(项);
Log(“+dictUser[“Position”]);
}
}
}
});
}
私有void writeNewPosition(字符串位置ID)
{
字符串json=JsonUtility.ToJson(playerInstance);
reference.Child(“Positions”).Child(positionId).SetRawJsonValueAsync(json);
}
}

有人能帮我一下吗?对不起,我的英语是

我找到了一种从firebase获取数据的方法这是我更新的新脚本,我使用了JSON.NET for Unity

using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SetPath : MonoBehaviour
{
    [SerializeField] private List<Transform> checkpoints = new List<Transform>();
    private LineRenderer linerenderer;
    public Material TheLineMateriel;


    //Firebase 
    public static bool _ispressed = false;
    private string DATA_URL = "https://kataraproject-a233a.firebaseio.com/";
    private DatabaseReference reference;
    Positions playerInstance = new Positions();
    List<Vector3> tmpList = new List<Vector3>();
    public static string Json;
    bool liste_done = false;
    Vector3 newPosition;
    public List<Vector3> listOfPosition = new List<Vector3>();
    // Start is called before the first frame update

    private void Awake()
    {
        //FireBase
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(DATA_URL);
        reference = FirebaseDatabase.DefaultInstance.RootReference;
    }
    void Start()
    {

        FirebaseDatabase.DefaultInstance.GetReference("Positions").GetValueAsync().ContinueWith(task => {
            if (task.IsFaulted)
            {
                Debug.Log("No snapshot");
                // Handle the error...
            }
            else if (task.IsCompleted)
            {
                DataSnapshot snapshot = task.Result;
                //Debug.Log("Snapshot:= " + task.Result.Value);
                int i = 0;

                foreach (DataSnapshot user in snapshot.Children)
                {
                    // IDictionary dictUser = (IDictionary)user.Value;
                    //Debug.Log(dictUser.Keys);
                    Positions client = JsonConvert.DeserializeObject<Positions>(user.GetRawJsonValue());
                    foreach (var item in client.Position)
                    {
                        newPosition.x = item.x;
                        newPosition.y = item.y;
                        newPosition.z = 0;
                        checkpoints[i].gameObject.SetActive(true);
                        checkpoints[i].position = newPosition;
                        i++;

                    }
                    //Debug.Log(user.GetRawJsonValue());
                }
                //DrawLine
                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;
            Debug.Log("Saving is done");
        }


    }
    //Save Data in fireBase
    private void writeNewPosition(string positionId)
    {

        string json = JsonUtility.ToJson(playerInstance);

        reference.Child("Positions").Child(positionId).SetRawJsonValueAsync(json);
    }
    //Button to save Data
    public void PostPosition()
    {
        _ispressed = true;
    }

    public void NextScene()
    {
        SceneManager.LoadScene("PlayerSelmaNew");
    }

}
使用Firebase;
使用Firebase.Database;
使用Firebase.Unity.Editor;
使用Newtonsoft.Json;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类SetPath:monoBehavior
{
[SerializeField]私有列表检查点=新列表();
专用线条渲染器线条渲染器;
公共物资;线性物资;
//火基
公共静态bool_ispressed=false;
私有字符串数据\u URL=”https://kataraproject-a233a.firebaseio.com/";
私人数据库参考;
位置playerInstance=新位置();
List tmpList=新列表();
公共静态字符串Json;
bool liste_done=false;
矢量3新位置;
公共列表listOfPosition=新列表();
//在第一帧更新之前调用Start
私人空间
{
//火基
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(数据URL);
reference=FirebaseDatabase.DefaultInstance.RootReference;
}
void Start()
{
FirebaseDatabase.DefaultInstance.GetReference(“位置”).GetValueAsync().ContinueWith(任务=>{
if(task.IsFaulted)
{
Debug.Log(“无快照”);
//处理错误。。。
}
else if(任务已完成)
{
DataSnapshot快照=task.Result;
//Debug.Log(“快照:=”+task.Result.Value);
int i=0;
foreach(snapshot.Children中的DataSnapshot用户)
{
//IDictionary dictUser=(IDictionary)user.Value;
//Debug.Log(dictUser.Keys);
Positions client=JsonConvert.DeserializeObject(user.GetRawJsonValue());
foreach(客户端位置中的var项)
{
newPosition.x=item.x;
newPosition.y=item.y;
newPosition.z=0;
检查点[i].gameObject.SetActive(true);
检查点[i]。位置=新位置;