Skip to content

Commit 3063d54

Browse files
committed
fix: Improved component data retrieval with special handling for Transform and Camera components to prevent serialization crashes
1 parent 51eb59f commit 3063d54

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

UnityMcpBridge/Editor/Helpers/GameObjectSerializer.cs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,69 @@ public static object GetComponentData(Component c, bool includeNonPublicSerializ
157157
}
158158
// --- End Special handling for Transform ---
159159

160+
// --- Special handling for Camera to avoid matrix-related crashes ---
161+
if (componentType == typeof(Camera))
162+
{
163+
Camera cam = c as Camera;
164+
var cameraProperties = new Dictionary<string, object>();
165+
166+
// List of safe properties to serialize
167+
var safeProperties = new Dictionary<string, Func<object>>
168+
{
169+
{ "nearClipPlane", () => cam.nearClipPlane },
170+
{ "farClipPlane", () => cam.farClipPlane },
171+
{ "fieldOfView", () => cam.fieldOfView },
172+
{ "renderingPath", () => (int)cam.renderingPath },
173+
{ "actualRenderingPath", () => (int)cam.actualRenderingPath },
174+
{ "allowHDR", () => cam.allowHDR },
175+
{ "allowMSAA", () => cam.allowMSAA },
176+
{ "allowDynamicResolution", () => cam.allowDynamicResolution },
177+
{ "forceIntoRenderTexture", () => cam.forceIntoRenderTexture },
178+
{ "orthographicSize", () => cam.orthographicSize },
179+
{ "orthographic", () => cam.orthographic },
180+
{ "opaqueSortMode", () => (int)cam.opaqueSortMode },
181+
{ "transparencySortMode", () => (int)cam.transparencySortMode },
182+
{ "depth", () => cam.depth },
183+
{ "aspect", () => cam.aspect },
184+
{ "cullingMask", () => cam.cullingMask },
185+
{ "eventMask", () => cam.eventMask },
186+
{ "backgroundColor", () => cam.backgroundColor },
187+
{ "clearFlags", () => (int)cam.clearFlags },
188+
{ "stereoEnabled", () => cam.stereoEnabled },
189+
{ "stereoSeparation", () => cam.stereoSeparation },
190+
{ "stereoConvergence", () => cam.stereoConvergence },
191+
{ "enabled", () => cam.enabled },
192+
{ "name", () => cam.name },
193+
{ "tag", () => cam.tag },
194+
{ "gameObject", () => new { name = cam.gameObject.name, instanceID = cam.gameObject.GetInstanceID() } }
195+
};
196+
197+
foreach (var prop in safeProperties)
198+
{
199+
try
200+
{
201+
var value = prop.Value();
202+
if (value != null)
203+
{
204+
AddSerializableValue(cameraProperties, prop.Key, value.GetType(), value);
205+
}
206+
}
207+
catch (Exception)
208+
{
209+
// Silently skip any property that fails
210+
continue;
211+
}
212+
}
213+
214+
return new Dictionary<string, object>
215+
{
216+
{ "typeName", componentType.FullName },
217+
{ "instanceID", cam.GetInstanceID() },
218+
{ "properties", cameraProperties }
219+
};
220+
}
221+
// --- End Special handling for Camera ---
222+
160223
var data = new Dictionary<string, object>
161224
{
162225
{ "typeName", componentType.FullName },
@@ -257,15 +320,25 @@ public static object GetComponentData(Component c, bool includeNonPublicSerializ
257320
if (componentType == typeof(Camera) &&
258321
(propName == "pixelRect" ||
259322
propName == "rect" ||
260-
propName == "cullingMatrix"))
323+
propName == "cullingMatrix" ||
324+
propName == "useOcclusionCulling" ||
325+
propName == "worldToCameraMatrix" ||
326+
propName == "projectionMatrix" ||
327+
propName == "nonJitteredProjectionMatrix" ||
328+
propName == "previousViewProjectionMatrix" ||
329+
propName == "cameraToWorldMatrix"))
261330
{
262331
// Debug.Log($"[GetComponentData] Explicitly skipping Camera property: {propName}");
263332
skipProperty = true;
264333
}
265334
// --- End Skip Camera Properties ---
266335

267336
// --- Skip specific potentially problematic Transform properties ---
268-
if (componentType == typeof(Transform) && (propName == "lossyScale" || propName == "rotation"))
337+
if (componentType == typeof(Transform) &&
338+
(propName == "lossyScale" ||
339+
propName == "rotation" ||
340+
propName == "worldToLocalMatrix" ||
341+
propName == "localToWorldMatrix"))
269342
{
270343
// Debug.Log($"[GetComponentData] Explicitly skipping Transform property: {propName}");
271344
skipProperty = true;

0 commit comments

Comments
 (0)