Repository navigation
fix: serialize Unity.Mathematics structs by their fields, not their swizzles - #1418
Conversation
…wizzles GetComponentData handed float3/quaternion values to Newtonsoft's default object contract, which walks every public swizzle property (117 on float3, 336 on float4) and never finishes, so reading a SplineContainer's components hung the Editor. A namespace-matched converter writes only the public fields. Fixes CoplayDev#1415
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (4)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughThe output serializer now uses ChangesUnity Mathematics serialization
Priority: ⬆️ High Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to This change makes component data for Unity Mathematics types serialize through their fields instead of their swizzle properties, which addresses the reported hangs. No merge-blocking issue was found. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change is confined to how existing component reads encode math values and avoids recursively expanding swizzle properties. No new privilege or component-write path was demonstrated. Coverage of additional math types and concurrent use remains limited. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 3 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…wizzles (upstream CoplayDev#1418) (cherry picked from upstream merge 5eefb7a)
Description
Reading
mcpforunity://scene/gameobject/{id}/componentson a component that holds aUnity.Mathematicsstruct (aSplineContainerwith knots is the reported case, but anyfloat3/quaternionfield does it) never returns and the Editor main thread is stuck until Unity is killed (#1415).GameObjectSerializer.GetComponentDatahands every field and property to a Newtonsoft serializer that has converters forVector3,Quaternion,Matrix4x4and so on, but nothing for the math types.float3exposes 117 public swizzle properties (xxy,zyx, …),float4336, each returning a new struct with swizzles of its own, andReferenceLoopHandling.Ignoredoes not prune value types, so the default object contract walks a combinatorial tree that in practice never finishes.This adds a
UnityMathematicsConverterthat writes any value type from theUnity.Mathematicsnamespace as its public instance fields only:float3→{x, y, z},quaternion→{value: {x, y, z, w}},float4x4→ its fourfloat4columns. Matching by namespace keeps the package free of acom.unity.mathematicsdependency. Same class of bug as #585 and #1128, closed for the whole type family this time instead of per component.Type of Change
Changes Made
MCPForUnity/Runtime/Serialization/UnityTypeConverters.cs: newUnityMathematicsConverter(CanConvert= value type in theUnity.Mathematicsnamespace, not primitive/enum;WriteJsonemits the cached public instance fields through the serializer so nested math types go through the same path;CanRead = false).MCPForUnity/Editor/Helpers/GameObjectSerializer.cs: registers it in_outputSerializerSettingsnext toMatrix4x4Converter.TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/UnityMathematicsSerializationTests.cs: declares stand-inUnity.Mathematics.float2/float3/float4/quaternionstructs with the same field + swizzle shape (the test project does not reference the real package; same approach asFusionUnsafeTypeSerializationTests) and checks that a component'sfloat3,quaternionandList<float3>fields serialize as field-only objects, with unrelated fields untouched.Compatibility / Package Source
System.Reflectionand Newtonsoft APIs that exist on netstandard2.0 / 2021.3 are used.file:../../../MCPForUnity(the TestProjects manifest, unchanged)Testing/Screenshots/Recordings
unity test TestProjects/UnityMCPTests --mode EditModeon 6000.3.9f1:The one failure is
ToolParamsTests.GetFloat_ValidFloat_ReturnsValue("Expected: 2.5f But was: 25.0f"), the tr-TR culture bug that #1416 fixes; it fails onbetaon this machine with or without this change.Documentation Updates
Related Issues
Fixes #1415
Additional Notes
.cs.metawas generated by Unity 6000.3 (compact, guid-only form).Summary by CodeRabbit