【问题标题】:Review Data From FireBase and save it in a LineRender.setposition()查看来自 FireBase 的数据并将其保存在 LineRender.setposition()
【发布时间】: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 以确认你期望的内容吗?

标签: c# list firebase unity3d


【解决方案1】:

我找到了一种从 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");
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 2019-10-31
    相关资源
    最近更新 更多