JsonExtensions.cs 1008 B

123456789101112131415161718192021222324252627282930
  1. using Newtonsoft.Json.Linq;
  2. using UnityEngine;
  3. namespace LLM.Editor.Helper
  4. {
  5. /// <summary>
  6. /// Provides extension methods for converting JToken objects to Unity-specific types.
  7. /// </summary>
  8. public static class JsonExtensions
  9. {
  10. public static Vector3 ToVector3(this JToken token)
  11. {
  12. if (token is not JObject obj) return Vector3.zero;
  13. var x = obj["x"]?.Value<float>() ?? 0f;
  14. var y = obj["y"]?.Value<float>() ?? 0f;
  15. var z = obj["z"]?.Value<float>() ?? 0f;
  16. return new Vector3(x, y, z);
  17. }
  18. public static Quaternion ToQuaternion(this JToken token)
  19. {
  20. if (token is not JObject obj) return Quaternion.identity;
  21. var x = obj["x"]?.Value<float>() ?? 0f;
  22. var y = obj["y"]?.Value<float>() ?? 0f;
  23. var z = obj["z"]?.Value<float>() ?? 0f;
  24. var w = obj["w"]?.Value<float>() ?? 1f;
  25. return new Quaternion(x, y, z, w);
  26. }
  27. }
  28. }