Skip to content

Commit 7c049c6

Browse files
committed
# Conflicts: # Editor/Tools/UpdateComponentTool.cs
2 parents 00b3944 + af8a16d commit 7c049c6

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed

Editor/Tools/SelectGameObjectTool.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public override JObject Execute(JObject parameters)
2929
string objectPath = parameters["objectPath"]?.ToObject<string>();
3030
string objectName = parameters["objectName"]?.ToObject<string>();
3131
int? instanceId = parameters["instanceId"]?.ToObject<int?>();
32+
GameObject selectedGameObject = null;
3233

3334
// Validate parameters - require either objectPath or instanceId
3435
if (string.IsNullOrEmpty(objectPath) && string.IsNullOrEmpty(objectName) && !instanceId.HasValue)
@@ -42,32 +43,32 @@ public override JObject Execute(JObject parameters)
4243
// First try to find by instance ID if provided
4344
if (instanceId.HasValue)
4445
{
45-
Selection.activeGameObject = EditorUtility.InstanceIDToObject(instanceId.Value) as GameObject;
46+
selectedGameObject = EditorUtility.InstanceIDToObject(instanceId.Value) as GameObject;
4647
}
47-
// Otherwise, try to find by object path/name if provided
4848
else if (!string.IsNullOrEmpty(objectPath))
4949
{
5050
// Try to find the object by path in the hierarchy
51-
Selection.activeGameObject = GameObject.Find(objectPath);
51+
selectedGameObject = GameObject.Find(objectPath);
5252
}
53-
else if (!string.IsNullOrEmpty(objectName))
53+
else
5454
{
5555
// Try to find the object by name in the hierarchy
56-
Selection.activeGameObject = GameObject.Find(objectName);
56+
selectedGameObject = GameObject.Find(objectName);
5757
}
58+
59+
Selection.activeGameObject = selectedGameObject;
5860

5961
// Ping the selected object
60-
EditorGUIUtility.PingObject(Selection.activeGameObject);
62+
EditorGUIUtility.PingObject(selectedGameObject);
6163

62-
// Log the selection
63-
McpLogger.LogInfo($"[MCP Unity] Selected GameObject: {Selection.activeGameObject.name}"));
64+
McpLogger.LogInfo($"[MCP Unity] Selected GameObject: {selectedGameObject?.name}");
6465

6566
// Create the response
6667
return new JObject
6768
{
6869
["success"] = true,
6970
["type"] = "text",
70-
["message"] = $"Successfully selected GameObject {Selection.activeGameObject.name}"
71+
["message"] = $"Successfully selected GameObject {selectedGameObject?.name}"
7172
};
7273
}
7374
}

Editor/Tools/UpdateComponentTool.cs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public override JObject Execute(JObject parameters)
8282

8383
// Try to find the component by name
8484
Component component = gameObject.GetComponent(componentName);
85-
bool wasAdded = false;
8685

8786
// If component not found, try to add it
8887
if (component == null)
@@ -97,31 +96,41 @@ public override JObject Execute(JObject parameters)
9796
}
9897

9998
component = Undo.AddComponent(gameObject, componentType);
100-
wasAdded = true;
99+
100+
// Ensure changes are saved
101+
EditorUtility.SetDirty(gameObject);
102+
if (PrefabUtility.IsPartOfAnyPrefab(gameObject))
103+
{
104+
PrefabUtility.RecordPrefabInstancePropertyModifications(component);
105+
}
106+
101107
McpLogger.LogInfo($"[MCP Unity] Added component '{componentName}' to GameObject '{gameObject.name}'");
102108
}
103-
104109
// Update component fields
105110
if (componentData != null && componentData.Count > 0)
106111
{
107-
UpdateComponentData(component, componentData);
108-
}
109-
110-
// Ensure changes are saved
111-
EditorUtility.SetDirty(gameObject);
112-
if (PrefabUtility.IsPartOfAnyPrefab(gameObject))
113-
{
114-
PrefabUtility.RecordPrefabInstancePropertyModifications(component);
112+
bool success = UpdateComponentData(component, componentData, out string errorMessage);
113+
// If update failed, return error
114+
if (!success)
115+
{
116+
return McpUnitySocketHandler.CreateErrorResponse(errorMessage, "update_error");
117+
}
118+
119+
// Ensure field changes are saved
120+
EditorUtility.SetDirty(gameObject);
121+
if (PrefabUtility.IsPartOfAnyPrefab(gameObject))
122+
{
123+
PrefabUtility.RecordPrefabInstancePropertyModifications(component);
124+
}
125+
115126
}
116-
127+
117128
// Create the response
118129
return new JObject
119130
{
120131
["success"] = true,
121132
["type"] = "text",
122-
["message"] = wasAdded
123-
? $"Successfully added component '{componentName}' to GameObject '{gameObject.name}' and updated its data"
124-
: $"Successfully updated component '{componentName}' on GameObject '{gameObject.name}'"
133+
["message"] = $"Successfully updated component '{componentName}' on GameObject '{gameObject.name}'"
125134
};
126135
}
127136

@@ -236,16 +245,19 @@ private Type FindComponentType(string componentName)
236245
/// <param name="component">The component to update</param>
237246
/// <param name="componentData">The data to apply to the component</param>
238247
/// <returns>True if the component was updated successfully</returns>
239-
private bool UpdateComponentData(Component component, JObject componentData)
248+
private bool UpdateComponentData(Component component, JObject componentData, out string errorMessage)
240249
{
250+
errorMessage = "";
251+
241252
if (component == null || componentData == null)
242253
{
254+
errorMessage = "Component or component data is null";
243255
return false;
244256
}
245-
257+
246258
Type componentType = component.GetType();
247-
bool anySuccess = false;
248-
259+
bool fullSuccess = true;
260+
249261
// Record object for undo
250262
Undo.RecordObject(component, $"Update {componentType.Name} fields");
251263

@@ -256,7 +268,7 @@ private bool UpdateComponentData(Component component, JObject componentData)
256268
JToken fieldValue = property.Value;
257269

258270
// Skip null values
259-
if (fieldValue.Type == JTokenType.Null)
271+
if (string.IsNullOrEmpty(fieldName) || fieldValue.Type == JTokenType.Null)
260272
{
261273
continue;
262274
}
@@ -269,7 +281,6 @@ private bool UpdateComponentData(Component component, JObject componentData)
269281
{
270282
object value = ConvertJTokenToValue(fieldValue, fieldInfo.FieldType);
271283
fieldInfo.SetValue(component, value);
272-
anySuccess = true;
273284
continue;
274285
}
275286

@@ -281,17 +292,16 @@ private bool UpdateComponentData(Component component, JObject componentData)
281292
{
282293
object value = ConvertJTokenToValue(fieldValue, propertyInfo.PropertyType);
283294
propertyInfo.SetValue(component, value);
284-
anySuccess = true;
285295
continue;
286296
}
287297

288-
// Try to update field
289-
McpLogger.LogWarning($"Field or Property with name '{fieldName}' not found on component '{componentType.Name}'");
298+
fullSuccess = false;
299+
errorMessage = $"Field or Property with name '{fieldName}' not found on component '{componentType.Name}'";
290300
}
291-
292-
return anySuccess;
301+
302+
return fullSuccess;
293303
}
294-
304+
295305
/// <summary>
296306
/// Convert a JToken to a value of the specified type
297307
/// </summary>

0 commit comments

Comments
 (0)