豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit a346558

Browse files
authored
fix: Unity 2021.3 compat — compile errors, Mono crash, 19 test failures (#1036)
1 parent 1f0ef2c commit a346558

File tree

12 files changed

+139
-45
lines changed

12 files changed

+139
-45
lines changed

MCPForUnity/Editor/Helpers/ComponentOps.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,18 @@ internal static bool SetObjectReference(SerializedProperty prop, JToken value, o
748748
return AssignObjectReference(prop, resolved, null, out error);
749749
}
750750

751+
// Try as asset GUID (32-char hex string)
752+
if (strVal.Length == 32 && IsHexString(strVal))
753+
{
754+
string assetPath = AssetDatabase.GUIDToAssetPath(strVal);
755+
if (!string.IsNullOrEmpty(assetPath))
756+
{
757+
var resolved = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
758+
if (resolved != null)
759+
return AssignObjectReference(prop, resolved, null, out error);
760+
}
761+
}
762+
751763
// Fall back to scene hierarchy lookup by name.
752764
return ResolveSceneObjectByName(prop, strVal, null, out error);
753765
}
@@ -969,6 +981,16 @@ private static long GetSpriteFileId(Sprite sprite)
969981
return 0;
970982
}
971983
}
984+
985+
private static bool IsHexString(string str)
986+
{
987+
foreach (char c in str)
988+
{
989+
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
990+
return false;
991+
}
992+
return true;
993+
}
972994
}
973995
}
974996

MCPForUnity/Editor/Tools/Profiler/Operations/CounterOps.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ void Tick()
111111

112112
private static readonly string[] ValidCategories = new[]
113113
{
114-
"Render", "Scripts", "Memory", "Physics", "Physics2D", "Animation",
114+
"Render", "Scripts", "Memory", "Physics",
115+
#if UNITY_2022_2_OR_NEWER
116+
"Physics2D",
117+
#endif
118+
"Animation",
115119
"Audio", "Lighting", "Network", "Gui", "UI", "Ai", "Video",
116120
"Loading", "Input", "Vr", "Internal", "Particles", "FileIO", "VirtualTexturing"
117121
};
@@ -125,7 +129,9 @@ void Tick()
125129
case "scripts": return ProfilerCategory.Scripts;
126130
case "memory": return ProfilerCategory.Memory;
127131
case "physics": return ProfilerCategory.Physics;
132+
#if UNITY_2022_2_OR_NEWER
128133
case "physics2d": return ProfilerCategory.Physics2D;
134+
#endif
129135
case "animation": return ProfilerCategory.Animation;
130136
case "audio": return ProfilerCategory.Audio;
131137
case "lighting": return ProfilerCategory.Lighting;

MCPForUnity/Editor/Tools/Profiler/Operations/FrameTimingOps.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ internal static class FrameTimingOps
88
{
99
internal static object GetFrameTiming(JObject @params)
1010
{
11+
#if UNITY_2022_2_OR_NEWER
1112
if (!FrameTimingManager.IsFeatureEnabled())
1213
{
1314
return new ErrorResponse(
1415
"Frame Timing Stats is not enabled. "
1516
+ "Enable it in Project Settings > Player > Other Settings > 'Frame Timing Stats', "
1617
+ "or use a Development Build (always enabled).");
1718
}
19+
#endif
1820

1921
FrameTimingManager.CaptureFrameTimings();
2022
var timings = new FrameTiming[1];
@@ -33,12 +35,16 @@ internal static object GetFrameTiming(JObject @params)
3335
{
3436
available = true,
3537
cpu_frame_time_ms = t.cpuFrameTime,
38+
#if UNITY_2022_2_OR_NEWER
3639
cpu_main_thread_frame_time_ms = t.cpuMainThreadFrameTime,
3740
cpu_main_thread_present_wait_time_ms = t.cpuMainThreadPresentWaitTime,
3841
cpu_render_thread_frame_time_ms = t.cpuRenderThreadFrameTime,
42+
#endif
3943
gpu_frame_time_ms = t.gpuFrameTime,
44+
#if UNITY_2022_2_OR_NEWER
4045
frame_start_timestamp = t.frameStartTimestamp,
4146
first_submit_timestamp = t.firstSubmitTimestamp,
47+
#endif
4248
cpu_time_present_called = t.cpuTimePresentCalled,
4349
cpu_time_frame_complete = t.cpuTimeFrameComplete,
4450
height_scale = t.heightScale,

MCPForUnity/Editor/Tools/UnityReflect.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ private static object GetTypeInfo(ToolParams p)
170170
});
171171
}
172172

173+
// Open generic type definitions (e.g. List<T>) segfault Mono on Unity 2021.3
174+
// in mono_metadata_generic_param_equal_internal when any member reflection is
175+
// performed. Return minimal info using only safe property accesses.
176+
if (type.IsGenericTypeDefinition)
177+
{
178+
return new SuccessResponse($"Type info for '{type.Name}'.", new
179+
{
180+
found = true,
181+
name = type.Name,
182+
full_name = type.FullName,
183+
@namespace = type.Namespace,
184+
assembly = type.Assembly.GetName().Name,
185+
is_generic_type_definition = true,
186+
hint = "Open generic type — consult docs for member details."
187+
});
188+
}
189+
173190
var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
174191

175192
var methods = type.GetMethods(flags)
@@ -272,6 +289,19 @@ private static object GetMemberInfo(ToolParams p)
272289
});
273290
}
274291

292+
if (type.IsGenericTypeDefinition)
293+
{
294+
return new SuccessResponse(
295+
$"Open generic type '{type.Name}' — consult docs for member details.", new
296+
{
297+
found = false,
298+
type_name = type.FullName,
299+
member_name = memberName,
300+
is_generic_type_definition = true,
301+
hint = "Open generic type — consult docs for member details."
302+
});
303+
}
304+
275305
// Use flags without DeclaredOnly to find inherited members
276306
var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
277307

Server/tests/test_cli_commands_characterization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ def test_prefab_workflow_open_modify_save(self, runner, mock_config):
807807
assert result.exit_code == 0
808808

809809
# Save
810-
result = runner.invoke(prefab, ["save", "--force"])
810+
result = runner.invoke(prefab, ["save"])
811811
assert result.exit_code == 0
812812

813813
# Close

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageGraphicsTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ManageGraphicsTests
1515
private const string TempRoot = "Assets/Temp/ManageGraphicsTests";
1616
private bool _hasVolumeSystem;
1717
private bool _hasURP;
18+
private bool _hasHDRP;
1819

1920
[SetUp]
2021
public void SetUp()
@@ -28,6 +29,7 @@ public void SetUp()
2829
var data = pingResult["data"];
2930
_hasVolumeSystem = data?.Value<bool>("hasVolumeSystem") ?? false;
3031
_hasURP = data?.Value<bool>("hasURP") ?? false;
32+
_hasHDRP = data?.Value<bool>("hasHDRP") ?? false;
3133
}
3234
}
3335

@@ -72,7 +74,7 @@ public void HandleCommand_MissingAction_ReturnsError()
7274
{
7375
var result = ToJObject(ManageGraphics.HandleCommand(new JObject()));
7476
Assert.IsFalse(result.Value<bool>("success"));
75-
Assert.That(result["message"].ToString(), Does.Contain("action"));
77+
Assert.That(result["error"].ToString(), Does.Contain("action"));
7678
}
7779

7880
[Test]
@@ -81,7 +83,7 @@ public void HandleCommand_UnknownAction_ReturnsError()
8183
var result = ToJObject(ManageGraphics.HandleCommand(
8284
new JObject { ["action"] = "bogus_action" }));
8385
Assert.IsFalse(result.Value<bool>("success"));
84-
Assert.That(result["message"].ToString(), Does.Contain("Unknown action"));
86+
Assert.That(result["error"].ToString(), Does.Contain("Unknown action"));
8587
}
8688

8789
[Test]
@@ -539,7 +541,7 @@ public void BakeSetProbePositions_WrongComponent_ReturnsError()
539541
["positions"] = new JArray { new JArray(0, 0, 0) }
540542
}));
541543
Assert.IsFalse(result.Value<bool>("success"));
542-
Assert.That(result["message"].ToString(), Does.Contain("LightProbeGroup"));
544+
Assert.That(result["error"].ToString(), Does.Contain("LightProbeGroup"));
543545
}
544546

545547
// =====================================================================
@@ -598,7 +600,7 @@ public void StatsSetSceneDebug_InvalidMode_ReturnsError()
598600
["mode"] = "InvalidMode"
599601
}));
600602
Assert.IsFalse(result.Value<bool>("success"));
601-
Assert.That(result["message"].ToString(), Does.Contain("Valid:"));
603+
Assert.That(result["error"].ToString(), Does.Contain("Valid:"));
602604
}
603605

604606
// =====================================================================
@@ -618,6 +620,7 @@ public void PipelineGetInfo_ReturnsPipelineName()
618620
[Test]
619621
public void PipelineGetSettings_ReturnsSettings()
620622
{
623+
Assume.That(_hasURP || _hasHDRP, "Built-in pipeline has no settings asset — skipping.");
621624
var result = ToJObject(ManageGraphics.HandleCommand(
622625
new JObject { ["action"] = "pipeline_get_settings" }));
623626
Assert.IsTrue(result.Value<bool>("success"), result.ToString());
@@ -635,7 +638,7 @@ public void PipelineSetQuality_InvalidLevel_ReturnsError()
635638
["level"] = "NonExistentLevel"
636639
}));
637640
Assert.IsFalse(result.Value<bool>("success"));
638-
Assert.That(result["message"].ToString(), Does.Contain("Available:"));
641+
Assert.That(result["error"].ToString(), Does.Contain("Available:"));
639642
}
640643

641644
// =====================================================================

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManagePhysicsTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,16 +768,17 @@ public void SetCollisionMatrix_MissingParams_ReturnsError()
768768
[Test]
769769
public void Raycast_HitsCollider()
770770
{
771+
// Use an offset origin/direction to avoid hitting unrelated scene objects
771772
var go = new GameObject("PhysTest_RayTarget");
772-
go.transform.position = new Vector3(0, 0, 5);
773+
go.transform.position = new Vector3(500, 500, 5);
773774
var col = go.AddComponent<BoxCollider>();
774775
col.size = new Vector3(10, 10, 1);
775776
UnityEngine.Physics.SyncTransforms();
776777

777778
var result = ToJObject(ManagePhysics.HandleCommand(new JObject
778779
{
779780
["action"] = "raycast",
780-
["origin"] = new JArray(0, 0, 0),
781+
["origin"] = new JArray(500, 500, 0),
781782
["direction"] = new JArray(0, 0, 1),
782783
["max_distance"] = 100
783784
}));

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageProBuilderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public void HandleCommand_UnknownAction_ReturnsError()
5353
var result = ToJObject(ManageProBuilder.HandleCommand(paramsObj));
5454

5555
Assert.IsFalse(result.Value<bool>("success"), result.ToString());
56-
Assert.That(result["error"]?.ToString() ?? result["message"]?.ToString(),
57-
Does.Contain("Unknown action"));
56+
var errorMsg = result["error"]?.ToString() ?? result["message"]?.ToString();
57+
Assert.That(errorMsg, Does.Contain("Unknown action").Or.Contain("not installed"));
5858
}
5959

6060
[Test]

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageSceneHierarchyPagingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void Screenshot_SceneViewRejectsSupersizeAboveOne()
115115
var response = raw as JObject ?? JObject.FromObject(raw);
116116

117117
Assert.IsFalse(response.Value<bool>("success"), response.ToString());
118-
StringAssert.Contains("does not support super_size above 1", response.Value<string>("message"));
118+
StringAssert.Contains("does not support super_size above 1", response.Value<string>("error"));
119119
}
120120

121121
[Test]
@@ -182,7 +182,7 @@ public void Screenshot_ViewTargetAcceptedForGameView()
182182

183183
// Should attempt positioned capture and fail to resolve the GO — not reject the param
184184
Assert.IsFalse(response.Value<bool>("success"), response.ToString());
185-
StringAssert.Contains("not found", response.Value<string>("message"));
185+
StringAssert.Contains("not found", response.Value<string>("error"));
186186
}
187187

188188
[Test]

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageSceneMultiSceneTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void ModifyBuildSettings_RedirectsToManageBuild()
7878
var result = ManageScene.HandleCommand(p);
7979
var r = result as JObject ?? JObject.FromObject(result);
8080
Assert.IsFalse(r.Value<bool>("success"));
81-
Assert.IsTrue(r.Value<string>("message").Contains("manage_build"));
81+
Assert.IsTrue(r.Value<string>("error").Contains("manage_build"));
8282
}
8383

8484
[Test]

0 commit comments

Comments
 (0)