diff --git a/Editor/Tools/UpdateComponentTool.cs b/Editor/Tools/UpdateComponentTool.cs index 36de206..5a6fd71 100644 --- a/Editor/Tools/UpdateComponentTool.cs +++ b/Editor/Tools/UpdateComponentTool.cs @@ -97,23 +97,44 @@ public override JObject Execute(JObject parameters) } component = Undo.AddComponent(gameObject, componentType); + + // Ensure changes are saved + EditorUtility.SetDirty(gameObject); + if (PrefabUtility.IsPartOfAnyPrefab(gameObject)) + { + PrefabUtility.RecordPrefabInstancePropertyModifications(component); + } wasAdded = true; McpLogger.LogInfo($"[MCP Unity] Added component '{componentName}' to GameObject '{gameObject.name}'"); } - // Update component fields if (componentData != null && componentData.Count > 0) { - UpdateComponentData(component, componentData); - } - - // Ensure changes are saved - EditorUtility.SetDirty(gameObject); - if (PrefabUtility.IsPartOfAnyPrefab(gameObject)) - { - PrefabUtility.RecordPrefabInstancePropertyModifications(component); + bool success = UpdateComponentData(component, componentData, out string errorMessage); + // If update failed, return error + if (!success) + { + if (wasAdded) + { + return McpUnitySocketHandler.CreateErrorResponse( + $"Successfully added component '{componentName}' to GameObject '{gameObject.name}' BUT\n" + + errorMessage, "component_error"); + } + else + { + return McpUnitySocketHandler.CreateErrorResponse(errorMessage, "update_error"); + } + } + + // Ensure field changes are saved + EditorUtility.SetDirty(gameObject); + if (PrefabUtility.IsPartOfAnyPrefab(gameObject)) + { + PrefabUtility.RecordPrefabInstancePropertyModifications(component); + } + } - + // Create the response return new JObject { @@ -236,19 +257,21 @@ private Type FindComponentType(string componentName) /// The component to update /// The data to apply to the component /// True if the component was updated successfully - private bool UpdateComponentData(Component component, JObject componentData) + private bool UpdateComponentData(Component component, JObject componentData, out string errorMessage) { + errorMessage = ""; if (component == null || componentData == null) { + errorMessage = "Component or component data is null"; return false; } - + Type componentType = component.GetType(); - bool anySuccess = false; - + bool fullSuccess = true; + // Record object for undo Undo.RecordObject(component, $"Update {componentType.Name} fields"); - + // Process each field in the component data foreach (var property in componentData.Properties()) { @@ -256,7 +279,7 @@ private bool UpdateComponentData(Component component, JObject componentData) JToken fieldValue = property.Value; // Skip null values - if (fieldValue.Type == JTokenType.Null) + if (string.IsNullOrEmpty(fieldName) || fieldValue.Type == JTokenType.Null) { continue; } @@ -269,18 +292,19 @@ private bool UpdateComponentData(Component component, JObject componentData) { object value = ConvertJTokenToValue(fieldValue, fieldInfo.FieldType); fieldInfo.SetValue(component, value); - anySuccess = true; continue; } else { - McpLogger.LogWarning($"Field '{fieldName}' not found on component '{componentType.Name}'"); + errorMessage = $"Field '{fieldName}' not found on component '{componentType.Name}'"; + McpLogger.LogError(errorMessage); + fullSuccess = false; } } - - return anySuccess; + + return fullSuccess; } - + /// /// Convert a JToken to a value of the specified type ///