Skip to content

Commit d28fab7

Browse files
authored
Merge pull request #983 from Scriptwonder/beta
Manage_physics
2 parents 9081fd5 + f92c08f commit d28fab7

35 files changed

Lines changed: 5809 additions & 14 deletions

MCPForUnity/Editor/Services/Server/TerminalLauncher.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ public System.Diagnostics.ProcessStartInfo CreateTerminalProcessStartInfo(string
7070
CreateNoWindow = true
7171
};
7272
#else
73-
// Linux: Try common terminal emulators
74-
// We use bash -c to execute the command, so we must properly quote/escape for bash
75-
// Escape single quotes for the inner bash string
76-
string escapedCommandLinux = command.Replace("'", "'\\''");
77-
// Wrap the command in single quotes for bash -c
78-
string script = $"'{escapedCommandLinux}; exec bash'";
79-
// Escape double quotes for the outer Process argument string
80-
string escapedScriptForArg = script.Replace("\"", "\\\"");
73+
// Linux: Try common terminal emulators.
74+
// ProcessStartInfo passes the argument string directly to the terminal, so we only
75+
// need to escape for the double-quoted bash -c payload — no inner single quotes.
76+
string script = $"{command}; exec bash";
77+
string escapedScriptForArg = script
78+
.Replace("\\", "\\\\")
79+
.Replace("\"", "\\\"");
8180
string bashCmdArgs = $"bash -c \"{escapedScriptForArg}\"";
8281

8382
string[] terminals = { "gnome-terminal", "xterm", "konsole", "xfce4-terminal" };

MCPForUnity/Editor/Tools/Build/BuildRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static void RunBuildCore(BuildJob job, Func<BuildReport> buildFunc)
111111
else
112112
{
113113
job.State = BuildJobState.Failed;
114-
#if UNITY_2022_3_OR_NEWER
114+
#if UNITY_2023_1_OR_NEWER
115115
job.ErrorMessage = report.SummarizeErrors();
116116
#else
117117
job.ErrorMessage = $"Build failed with result: {summary.result}";

MCPForUnity/Editor/Tools/Physics.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json.Linq;
3+
using UnityEditor;
4+
using UnityEngine;
5+
using MCPForUnity.Editor.Helpers;
6+
7+
namespace MCPForUnity.Editor.Tools.Physics
8+
{
9+
internal static class CollisionMatrixOps
10+
{
11+
public static object GetCollisionMatrix(JObject @params)
12+
{
13+
var p = new ToolParams(@params);
14+
string dimension = (p.Get("dimension") ?? "3d").ToLowerInvariant();
15+
16+
if (dimension != "3d" && dimension != "2d")
17+
return new ErrorResponse($"Invalid dimension: '{dimension}'. Use '3d' or '2d'.");
18+
19+
var layers = new List<object>();
20+
var populatedIndices = new List<int>();
21+
22+
for (int i = 0; i < 32; i++)
23+
{
24+
string name = LayerMask.LayerToName(i);
25+
if (string.IsNullOrEmpty(name)) continue;
26+
layers.Add(new { index = i, name });
27+
populatedIndices.Add(i);
28+
}
29+
30+
var matrix = new Dictionary<string, Dictionary<string, bool>>();
31+
32+
foreach (int i in populatedIndices)
33+
{
34+
string nameA = LayerMask.LayerToName(i);
35+
var row = new Dictionary<string, bool>();
36+
37+
foreach (int j in populatedIndices)
38+
{
39+
if (j > i) continue;
40+
string nameB = LayerMask.LayerToName(j);
41+
bool collides = dimension == "2d"
42+
? !Physics2D.GetIgnoreLayerCollision(i, j)
43+
: !UnityEngine.Physics.GetIgnoreLayerCollision(i, j);
44+
row[nameB] = collides;
45+
}
46+
47+
matrix[nameA] = row;
48+
}
49+
50+
return new
51+
{
52+
success = true,
53+
message = $"Collision matrix retrieved ({dimension}).",
54+
data = new { layers, matrix }
55+
};
56+
}
57+
58+
public static object SetCollisionMatrix(JObject @params)
59+
{
60+
var p = new ToolParams(@params);
61+
string dimension = (p.Get("dimension") ?? "3d").ToLowerInvariant();
62+
63+
if (dimension != "3d" && dimension != "2d")
64+
return new ErrorResponse($"Invalid dimension: '{dimension}'. Use '3d' or '2d'.");
65+
66+
var layerAToken = p.GetRaw("layer_a");
67+
var layerBToken = p.GetRaw("layer_b");
68+
69+
if (layerAToken == null)
70+
return new ErrorResponse("'layer_a' parameter is required.");
71+
if (layerBToken == null)
72+
return new ErrorResponse("'layer_b' parameter is required.");
73+
74+
int layerA = ResolveLayer(layerAToken);
75+
int layerB = ResolveLayer(layerBToken);
76+
77+
if (layerA < 0 || layerA >= 32)
78+
return new ErrorResponse($"Invalid layer_a: '{layerAToken}'. Layer not found or out of range.");
79+
if (layerB < 0 || layerB >= 32)
80+
return new ErrorResponse($"Invalid layer_b: '{layerBToken}'. Layer not found or out of range.");
81+
82+
bool collide = p.GetBool("collide", true);
83+
84+
if (dimension == "2d")
85+
{
86+
Physics2D.IgnoreLayerCollision(layerA, layerB, !collide);
87+
MarkSettingsDirty("ProjectSettings/Physics2DSettings.asset");
88+
}
89+
else
90+
{
91+
UnityEngine.Physics.IgnoreLayerCollision(layerA, layerB, !collide);
92+
MarkSettingsDirty("ProjectSettings/DynamicsManager.asset");
93+
}
94+
95+
string nameA = LayerMask.LayerToName(layerA);
96+
string nameB = LayerMask.LayerToName(layerB);
97+
if (string.IsNullOrEmpty(nameA)) nameA = layerA.ToString();
98+
if (string.IsNullOrEmpty(nameB)) nameB = layerB.ToString();
99+
100+
return new
101+
{
102+
success = true,
103+
message = $"Collision between '{nameA}' and '{nameB}' set to {(collide ? "enabled" : "disabled")} ({dimension}).",
104+
data = new { layer_a = nameA, layer_b = nameB, collide, dimension }
105+
};
106+
}
107+
108+
private static void MarkSettingsDirty(string assetPath)
109+
{
110+
var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
111+
if (assets != null && assets.Length > 0)
112+
EditorUtility.SetDirty(assets[0]);
113+
}
114+
115+
private static int ResolveLayer(JToken token)
116+
{
117+
if (token.Type == JTokenType.Integer)
118+
{
119+
int idx = token.Value<int>();
120+
return idx >= 0 && idx < 32 ? idx : -1;
121+
}
122+
string name = token.ToString();
123+
if (int.TryParse(name, out int parsed))
124+
return parsed >= 0 && parsed < 32 ? parsed : -1;
125+
return LayerMask.NameToLayer(name);
126+
}
127+
}
128+
}

MCPForUnity/Editor/Tools/Physics/CollisionMatrixOps.cs.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)