Skip to content

Commit c12bd38

Browse files
committed
Fix
1 parent 97a418e commit c12bd38

8 files changed

Lines changed: 43 additions & 19 deletions

File tree

MCPForUnity/Editor/Tools/ManageComponents.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ private static object RemoveComponent(JObject @params, JToken targetToken, strin
152152
var components = targetGo.GetComponents(type);
153153
if (componentIndex.Value < 0 || componentIndex.Value >= components.Length)
154154
return new ErrorResponse($"component_index {componentIndex.Value} out of range. Found {components.Length} '{componentTypeName}' component(s).");
155+
if (type == typeof(Transform) || type == typeof(RectTransform))
156+
return new ErrorResponse("Cannot remove Transform or RectTransform components.");
155157
Undo.DestroyObjectImmediate(components[componentIndex.Value]);
156158
EditorUtility.SetDirty(targetGo);
157159
MarkOwningSceneDirty(targetGo);

MCPForUnity/Editor/Tools/Physics/JointOps.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public static object ConfigureJoint(JObject @params)
162162

163163
string jointTypeStr = p.Get("joint_type");
164164
int? componentIndex = ParamCoercion.CoerceIntNullable(@params["componentIndex"] ?? @params["component_index"]);
165+
166+
if (componentIndex.HasValue && string.IsNullOrEmpty(jointTypeStr))
167+
return new ErrorResponse("component_index requires joint_type to be specified.");
168+
165169
Component joint = ResolveJoint(go, jointTypeStr, componentIndex, out int foundCount);
166170
if (joint == null)
167171
{
@@ -330,6 +334,9 @@ public static object RemoveJoint(JObject @params)
330334
string jointTypeStr = p.Get("joint_type");
331335
int? componentIndex = ParamCoercion.CoerceIntNullable(@params["componentIndex"] ?? @params["component_index"]);
332336

337+
if (componentIndex.HasValue && string.IsNullOrEmpty(jointTypeStr))
338+
return new ErrorResponse("component_index requires joint_type to be specified.");
339+
333340
var jointsToRemove = new List<Component>();
334341

335342
if (!string.IsNullOrEmpty(jointTypeStr))

MCPForUnity/Editor/Tools/Physics/PhysicsMaterialOps.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ public static object Assign(JObject @params)
120120
if (componentIndex.HasValue)
121121
{
122122
var type3D = !string.IsNullOrEmpty(colliderType) ? UnityTypeResolver.ResolveComponent(colliderType) : typeof(Collider);
123-
int count3D = type3D != null ? go.GetComponents(type3D).Length : 0;
124-
return new ErrorResponse($"component_index {componentIndex.Value} out of range. Found {count3D} '{(type3D ?? typeof(Collider)).Name}' collider(s) on '{go.name}'.");
123+
if (type3D != null)
124+
{
125+
int count3D = go.GetComponents(type3D).Length;
126+
return new ErrorResponse($"component_index {componentIndex.Value} out of range. Found {count3D} '{type3D.Name}' collider(s) on '{go.name}'.");
127+
}
125128
}
126129
}
127130

@@ -149,8 +152,11 @@ public static object Assign(JObject @params)
149152
if (componentIndex.HasValue)
150153
{
151154
var type2D = !string.IsNullOrEmpty(colliderType) ? UnityTypeResolver.ResolveComponent(colliderType) : typeof(Collider2D);
152-
int count2D = type2D != null ? go.GetComponents(type2D).Length : 0;
153-
return new ErrorResponse($"component_index {componentIndex.Value} out of range. Found {count2D} '{(type2D ?? typeof(Collider2D)).Name}' collider(s) on '{go.name}'.");
155+
if (type2D != null)
156+
{
157+
int count2D = go.GetComponents(type2D).Length;
158+
return new ErrorResponse($"component_index {componentIndex.Value} out of range. Found {count2D} '{type2D.Name}' collider(s) on '{go.name}'.");
159+
}
154160
}
155161
}
156162

MCPForUnity/Editor/Tools/Vfx/ParticleCommon.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public static ParticleSystem FindParticleSystem(JObject @params)
1111
public static string FindParticleSystemError(JObject @params)
1212
=> ManageVfxCommon.FindComponentError<ParticleSystem>(@params);
1313

14-
public static ParticleSystemRenderer FindParticleSystemRenderer(JObject @params)
15-
=> ManageVfxCommon.FindComponent<ParticleSystemRenderer>(@params);
14+
public static ParticleSystemRenderer FindParticleSystemRenderer(ParticleSystem ps)
15+
=> ps != null ? ps.GetComponent<ParticleSystemRenderer>() : null;
1616

1717
public static ParticleSystem.MinMaxCurve ParseMinMaxCurve(JToken token, float defaultValue = 1f)
1818
{

MCPForUnity/Editor/Tools/Vfx/ParticleControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static object Control(JObject @params, string action)
138138
// Ensure material is assigned before playing
139139
if (action == "play" || action == "restart")
140140
{
141-
var renderer = ParticleCommon.FindParticleSystemRenderer(@params);
141+
var renderer = ParticleCommon.FindParticleSystemRenderer(ps);
142142
if (renderer != null)
143143
{
144144
ensureResult = RendererHelpers.EnsureMaterial(renderer);
@@ -173,7 +173,7 @@ public static object AddBurst(JObject @params)
173173
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
174174

175175
// Ensure material is assigned
176-
var renderer = ParticleCommon.FindParticleSystemRenderer(@params);
176+
var renderer = ParticleCommon.FindParticleSystemRenderer(ps);
177177
RendererHelpers.EnsureMaterialResult ensureResult = default;
178178
bool materialChecked = false;
179179
if (renderer != null)

MCPForUnity/Editor/Tools/Vfx/ParticleRead.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static object GetInfo(JObject @params)
9595
var main = ps.main;
9696
var emission = ps.emission;
9797
var shape = ps.shape;
98-
var renderer = ParticleCommon.FindParticleSystemRenderer(@params);
98+
var renderer = ParticleCommon.FindParticleSystemRenderer(ps);
9999

100100
return new
101101
{

MCPForUnity/Editor/Tools/Vfx/ParticleWrite.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static object SetMain(JObject @params)
2121
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
2222

2323
// Ensure material is assigned before any configuration.
24-
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(@params));
24+
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(ps));
2525

2626
// Stop particle system if it's playing and duration needs to be changed
2727
bool wasPlaying = ps.isPlaying;
@@ -68,7 +68,7 @@ public static object SetEmission(JObject @params)
6868
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
6969

7070
// Ensure material is assigned.
71-
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(@params));
71+
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(ps));
7272

7373
Undo.RecordObject(ps, "Set ParticleSystem Emission");
7474
var emission = ps.emission;
@@ -88,7 +88,7 @@ public static object SetShape(JObject @params)
8888
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
8989

9090
// Ensure material is assigned.
91-
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(@params));
91+
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(ps));
9292

9393
Undo.RecordObject(ps, "Set ParticleSystem Shape");
9494
var shape = ps.shape;
@@ -114,7 +114,7 @@ public static object SetColorOverLifetime(JObject @params)
114114
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
115115

116116
// Ensure material is assigned.
117-
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(@params));
117+
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(ps));
118118

119119
Undo.RecordObject(ps, "Set ParticleSystem Color Over Lifetime");
120120
var col = ps.colorOverLifetime;
@@ -133,7 +133,7 @@ public static object SetSizeOverLifetime(JObject @params)
133133
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
134134

135135
// Ensure material is assigned.
136-
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(@params));
136+
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(ps));
137137

138138
Undo.RecordObject(ps, "Set ParticleSystem Size Over Lifetime");
139139
var sol = ps.sizeOverLifetime;
@@ -168,7 +168,7 @@ public static object SetVelocityOverLifetime(JObject @params)
168168
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
169169

170170
// Ensure material is assigned.
171-
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(@params));
171+
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(ps));
172172

173173
Undo.RecordObject(ps, "Set ParticleSystem Velocity Over Lifetime");
174174
var vol = ps.velocityOverLifetime;
@@ -191,7 +191,7 @@ public static object SetNoise(JObject @params)
191191
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
192192

193193
// Ensure material is assigned.
194-
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(@params));
194+
EnsureParticleRendererMaterial(ParticleCommon.FindParticleSystemRenderer(ps));
195195

196196
Undo.RecordObject(ps, "Set ParticleSystem Noise");
197197
var noise = ps.noise;
@@ -214,8 +214,8 @@ public static object SetRenderer(JObject @params)
214214
ParticleSystem ps = ParticleCommon.FindParticleSystem(@params);
215215
if (ps == null) return new { success = false, message = ParticleCommon.FindParticleSystemError(@params) };
216216

217-
var renderer = ParticleCommon.FindParticleSystemRenderer(@params);
218-
if (renderer == null) return new { success = false, message = "ParticleSystemRenderer not found" };
217+
var renderer = ParticleCommon.FindParticleSystemRenderer(ps);
218+
if (renderer == null) return new { success = false, message = $"ParticleSystemRenderer not found on '{ps.gameObject.name}'" };
219219

220220
// Ensure material is set before any other operations
221221
RendererHelpers.EnsureMaterialResult ensureResult = RendererHelpers.EnsureMaterial(renderer);

Server/src/cli/commands/component.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,21 @@ def set_property(target: str, component_type: str, property_name: str, value: st
179179
default=None,
180180
help="How to find the target GameObject."
181181
)
182+
@click.option(
183+
"--component-index", "-i",
184+
type=int,
185+
default=None,
186+
help="Zero-based index when multiple components of the same type exist."
187+
)
182188
@handle_unity_errors
183-
def modify(target: str, component_type: str, properties: str, search_method: Optional[str]):
189+
def modify(target: str, component_type: str, properties: str, search_method: Optional[str], component_index: Optional[int]):
184190
"""Set multiple properties on a component at once.
185191
186192
\b
187193
Examples:
188194
unity-mcp component modify "Player" Rigidbody --properties '{"mass": 5.0, "useGravity": false}'
189195
unity-mcp component modify "Light" Light --properties '{"intensity": 2.0, "color": [1, 0, 0, 1]}'
196+
unity-mcp component modify "Player" BoxCollider --properties '{"size": [2,2,2]}' --component-index 1
190197
"""
191198
config = get_config()
192199

@@ -201,6 +208,8 @@ def modify(target: str, component_type: str, properties: str, search_method: Opt
201208

202209
if search_method:
203210
params["searchMethod"] = search_method
211+
if component_index is not None:
212+
params["componentIndex"] = component_index
204213

205214
result = run_command("manage_components", params, config)
206215
click.echo(format_output(result, config.format))

0 commit comments

Comments
 (0)