Created
October 7, 2016 08:35
-
-
Save Wikzo/c1cdc114b41aa54a93b17e4cdd8afb71 to your computer and use it in GitHub Desktop.
Unity JsonUtility Serializer bug with Quaternions (formatting?)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using System.Collections; | |
using System.IO; | |
[Serializable] | |
public class DummyData : ISaveData | |
{ | |
[SerializeField] private Vector3 _position = new Vector3(); | |
[SerializeField] private Quaternion _rotation = new Quaternion(); // <----------- this sometimes doesn't get serialized properly! | |
private static FileStream _fs; | |
private static StreamWriter _writer; | |
public bool SavePosition(DummyDataHolder holder) | |
{ | |
Transform t = holder.transform; | |
_position = t.position; | |
_rotation = t.rotation; | |
return true; | |
} | |
public string OutputJsonData() | |
{ | |
return JsonUtility.ToJson(this); | |
} | |
public static bool WriteJsonFile(string data, string path) | |
{ | |
// nothing to write | |
if (data == "" || path == "") | |
return false; | |
using (_fs = new FileStream(path, FileMode.OpenOrCreate)) | |
{ | |
using (_writer = new StreamWriter(_fs)) | |
{ | |
_writer.Write(data); | |
} | |
} | |
// successfully wrote the file | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"_position":{"x":41.80579376220703,"y":43.82134246826172,"z":51.85480499267578},"_rotation":{"x":0.03028147667646408,"y":0.030189329758286477,"z":0.030189301818609239,"w":0.9986291527748108}}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class DummyDataHolder : MonoBehaviour | |
{ | |
public bool ShouldRotate = true; | |
private DummyData _data; | |
void Start() | |
{ | |
_data = LoadPreviousData(); | |
if (_data== null) | |
_data = new DummyData(); | |
StartCoroutine(AutoSave(_data)); | |
} | |
IEnumerator AutoSave(DummyData data) | |
{ | |
WriteDataToJson(data); | |
_data = LoadPreviousData(); | |
yield return new WaitForSeconds(0.2f); | |
StartCoroutine(AutoSave(data)); | |
} | |
void WriteDataToJson(DummyData data) | |
{ | |
data.SavePosition(this); | |
string json = data.OutputJsonData(); | |
// nothing to save | |
if (json == "") | |
return; | |
if (DummyData.WriteJsonFile(json, GetJsonPath())) | |
{ | |
string saveMessage = string.Format("Saved dummy data: {0}", json); | |
Debug.Log(saveMessage); | |
} | |
} | |
string GetJsonPath() | |
{ | |
return string.Format("{0}{1}", | |
"Assets/Resources/DummyData", | |
".json"); | |
} | |
private void Update() | |
{ | |
// the serialization bug happens when the transform's rotation starts changing! | |
if (ShouldRotate) | |
transform.Rotate(new Vector3(10,10,10) * Time.deltaTime, Space.Self); | |
} | |
DummyData LoadPreviousData() | |
{ | |
DummyData data = null; | |
string json; | |
json = DataSaver.RetriveText(GetJsonPath()); | |
if (json != "") | |
{ | |
Debug.Log("Loaded"); | |
data = JsonUtility.FromJson<DummyData>(json); | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment