1

I'm trying to get all my lines render positions and add it to a list of vector3 then covert it to Json, so I can save it in the firebase. I'm using JsonUtility, but the problem is my jsondata is always empty even the list of vecor3 is not.

This is my code then I'm using :

 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;

     public string hello = "khraaa" ;



     // 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);


         //for (int i = 0; i < linerenderer.positionCount ; i++)
         //{

         //    newPos[i] = i;
         //}

         if ( _ispressed == true)
         {
             string jsonData = JsonUtility.ToJson(checkpointsArray);

             //reference.Child("Position"  + Random.Range(0,1000000)).SetRawJsonValueAsync(jsonData);
             Debug.Log(jsonData);
             _ispressed = false;
         }
     }


    public void Save ()
     {
         _ispressed = true;
     }
 }

can someone help me please?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gara
  • 143
  • 1
  • 4
  • 12

1 Answers1

2

You could create a simple class to handle serialization of Unity types like such :

[Serializable]
public class SerializedVector3
{
    public float x;
    public float y;
    public float z;

    public SerializedVector3(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public SerializedVector3(Vector3 vector3)
    {
        x = vector3.x;
        y = vector3.y;
        z = vector3.z;
    }
}

public static class Vector3Extensions
{
    public static Vector3 ToVector3(this SerializedVector3 serializedVector3)
    {
        return new Vector3(serializedVector3.x, serializedVector3.y, serializedVector3.z);
    }

    public static SerializedVector3 FromVector3(this Vector3 vector3)
    {
        return new SerializedVector3(vector3);
    }
}
Jichael
  • 820
  • 6
  • 17
  • 1
    Instead of extension methods I would rather directly use implicit operators so you can use both types exchangeably. Also this still doesn't solve the (de)serialiazation of arrays in Unity. – derHugo Jan 02 '20 at 10:21
  • That's what I'm doing in a project where I instantiate/save things to json. You just convert before/after serialization. Doesn't matter if it is an array or not if you do it like that. At least that's why I'm doing and it's solving my problem – Jichael Jan 02 '20 at 10:51
  • I just create a class name Player [Serializable] public class Player { public Vector3[] Position; } then i use it hear Player playerInstance = new Player(); playerInstance.Position = newPos; string jsonData = JsonUtility.ToJson(playerInstance); reference.Child("Position" + Random.Range(0, 1000000)).SetRawJsonValueAsync(jsonData); – Gara Jan 02 '20 at 10:57
  • @derHugo Implicit operators was a great idea and incredibly easy to implement on top of this solution. – Horothenic Sep 07 '22 at 22:46