|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEditor; |
| 5 | +using Newtonsoft.Json.Linq; |
| 6 | +using McpUnity.Unity; |
| 7 | +using McpUnity.Utils; |
| 8 | + |
| 9 | +namespace McpUnity.Tools |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Tool for deleting a Unity scene and removing it from Build Settings |
| 13 | + /// </summary> |
| 14 | + public class DeleteSceneTool : McpToolBase |
| 15 | + { |
| 16 | + public DeleteSceneTool() |
| 17 | + { |
| 18 | + Name = "delete_scene"; |
| 19 | + Description = "Deletes a scene by path or name and removes it from Build Settings"; |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Execute the DeleteScene tool with the provided parameters |
| 24 | + /// </summary> |
| 25 | + /// <param name="parameters">Tool parameters as a JObject</param> |
| 26 | + public override JObject Execute(JObject parameters) |
| 27 | + { |
| 28 | + string scenePath = parameters["scenePath"]?.ToObject<string>(); |
| 29 | + string sceneName = parameters["sceneName"]?.ToObject<string>(); |
| 30 | + string folderPath = parameters["folderPath"]?.ToObject<string>(); |
| 31 | + |
| 32 | + if (string.IsNullOrEmpty(scenePath)) |
| 33 | + { |
| 34 | + if (string.IsNullOrEmpty(sceneName)) |
| 35 | + { |
| 36 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 37 | + "Provide either 'scenePath' or 'sceneName'", |
| 38 | + "validation_error" |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Resolve scene path by name (optionally within folderPath) |
| 43 | + string filter = $"{sceneName} t:Scene"; |
| 44 | + string[] searchInFolders = null; |
| 45 | + if (!string.IsNullOrEmpty(folderPath)) |
| 46 | + { |
| 47 | + // Ensure folder exists |
| 48 | + if (!AssetDatabase.IsValidFolder(folderPath)) |
| 49 | + { |
| 50 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 51 | + $"Folder '{folderPath}' does not exist", |
| 52 | + "not_found_error" |
| 53 | + ); |
| 54 | + } |
| 55 | + searchInFolders = new[] { folderPath }; |
| 56 | + } |
| 57 | + |
| 58 | + var guids = AssetDatabase.FindAssets(filter, searchInFolders); |
| 59 | + foreach (var guid in guids) |
| 60 | + { |
| 61 | + var path = AssetDatabase.GUIDToAssetPath(guid); |
| 62 | + if (System.IO.Path.GetFileNameWithoutExtension(path) == sceneName) |
| 63 | + { |
| 64 | + scenePath = path; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (string.IsNullOrEmpty(scenePath)) |
| 70 | + { |
| 71 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 72 | + $"Scene named '{sceneName}' not found", |
| 73 | + "not_found_error" |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + // If the scene is open, close it without saving changes |
| 81 | + var scene = UnityEditor.SceneManagement.EditorSceneManager.GetSceneByPath(scenePath); |
| 82 | + if (scene.IsValid() && scene.isLoaded) |
| 83 | + { |
| 84 | + UnityEditor.SceneManagement.EditorSceneManager.CloseScene(scene, true); |
| 85 | + } |
| 86 | + |
| 87 | + // Remove from Build Settings |
| 88 | + RemoveSceneFromBuildSettings(scenePath); |
| 89 | + |
| 90 | + // Delete asset |
| 91 | + bool deleted = AssetDatabase.DeleteAsset(scenePath); |
| 92 | + AssetDatabase.Refresh(); |
| 93 | + |
| 94 | + if (!deleted) |
| 95 | + { |
| 96 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 97 | + $"Failed to delete scene at '{scenePath}'", |
| 98 | + "delete_error" |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + McpLogger.LogInfo($"Deleted scene at path '{scenePath}' and removed from Build Settings"); |
| 103 | + |
| 104 | + return new JObject |
| 105 | + { |
| 106 | + ["success"] = true, |
| 107 | + ["type"] = "text", |
| 108 | + ["message"] = $"Successfully deleted scene at path '{scenePath}' and removed from Build Settings", |
| 109 | + ["scenePath"] = scenePath |
| 110 | + }; |
| 111 | + } |
| 112 | + catch (Exception ex) |
| 113 | + { |
| 114 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 115 | + $"Error deleting scene: {ex.Message}", |
| 116 | + "scene_delete_error" |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void RemoveSceneFromBuildSettings(string scenePath) |
| 122 | + { |
| 123 | + var scenes = UnityEditor.EditorBuildSettings.scenes; |
| 124 | + var filtered = scenes.Where(s => s.path != scenePath).ToArray(); |
| 125 | + if (filtered.Length != scenes.Length) |
| 126 | + { |
| 127 | + UnityEditor.EditorBuildSettings.scenes = filtered; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | + |
0 commit comments