Hello there,
i need your help. i have a script and a class in it like
[Serializable]
public class EnemyPointsClass
{
public Vector2 Position;
}
now i need a json format like:
[{“Position”:[48.89,28.25]]
but with the above class i get this:
[{“Position”: {“x”: 22.1599998474121,“y”: 14.8000001907349}]
i think vector 2 is false. what do i need to change to get the right format?
p.S x and y are the values of the Gameobjects transform, where scrip is attached
I’m guessing you need this because you need to conform to a JSON format that already exists and you can’t change? You would need it to be a float[ ] in order to get the format you’re looking for.
public float[] Position = new float[]{48.89, 28.25};
You can add to your class an accessor to make this easier to work with on the game code side:
public Vector2 positionVector {
get {
return new Vector2(Position[0], Position[1]);
}
set {
Position = new float[]{value.x, value.y};
}
}
(it might be double and not float, though. I have a vague recollection that Json readers seem to use double. If this doesn’t work, try that)
Multiply float by 100 and store as int.
Then set to JSON.
Then reverse when reading from JSON, by multiplying 0.01 and cast back to float.
i got it.
this solution throws the format i need:
public class EnemyPointsClass
{
public EnemyPointsClass()
{
Position = new List<double>();
}
}
then i can call it with this lines:
EnemyPointsClass _dataclass = new EnemyPointsClass();
_dataclass.Position.Add((float)System.Math.Round(transform.position.x, 2));
_dataclass.Position.Add((float)System.Math.Round(transform.position.y, 2));