TransformDTOSerializer.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.IO;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public class TransformDTOSerializer : SimpleSerializer<TransformDTO>
  6. {
  7. private const short version = 1;
  8. public override void Serialize(TransformDTO transform, BinaryWriter writer)
  9. {
  10. writer.WriteVersion(version);
  11. writer.Write(transform.Position);
  12. writer.Write(transform.Rotation);
  13. writer.Write(transform.LocalPosition);
  14. writer.Write(transform.LocalRotation);
  15. writer.Write(transform.LocalScale);
  16. }
  17. public override TransformDTO Deserialize(BinaryReader reader, SceneMetadata metadata)
  18. {
  19. _ = reader.ReadVersion();
  20. return new TransformDTO
  21. {
  22. Position = reader.ReadVector3(),
  23. Rotation = reader.ReadQuaternion(),
  24. LocalPosition = reader.ReadVector3(),
  25. LocalRotation = reader.ReadQuaternion(),
  26. LocalScale = reader.ReadVector3()
  27. };
  28. }
  29. }
  30. public class TransformDTO
  31. {
  32. public Vector3 Position { get; init; }
  33. public Vector3 LocalPosition { get; init; }
  34. public Quaternion Rotation { get; init; } = Quaternion.identity;
  35. public Quaternion LocalRotation { get; init; } = Quaternion.identity;
  36. public Vector3 LocalScale { get; init; } = Vector3.one;
  37. public TransformDTO() { }
  38. public TransformDTO(Transform transform)
  39. {
  40. Position = transform.position;
  41. LocalPosition = transform.localPosition;
  42. Rotation = transform.rotation;
  43. LocalRotation = transform.localRotation;
  44. LocalScale = transform.localScale;
  45. }
  46. }
  47. }