LoginSignup
3
0

More than 5 years have passed since last update.

JsonUtilityでVector2, Vector3をシリアライズする

Posted at

はじめに

Unity標準のJsonパーサー JsonUtilityVector2,Vector3に対応しています。素晴らしいですね!

どんな感じか見てみましょう。

コード

// シリアライズ
var vec2 = new Vector2(33, 4);
var vec3 = new Vector3(11, 45, 14);
Debug.Log(JsonUtility.ToJson(vec2)); // 出力: {"x":33.0,"y":4.0}
Debug.Log(JsonUtility.ToJson(vec3)); // 出力: {"x":11.0,"y":45.0,"z":14.0}
// デシリアライズ
var vec2json = "{\"x\":33.0,\"y\":4.0}";
var vec3json = "{\"x\":11.0,\"y\":45.0,\"z\":14.0}";
Debug.Log(JsonUtility.FromJson<Vector2>(vec2json)); // 出力: (33.0, 4.0)
Debug.Log(JsonUtility.FromJson<Vector3>(vec3json)); // 出力: (11.0, 45.0, 14.0)
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0