|
| 1 | +using System; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEditor; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using McpUnity.Unity; |
| 6 | +using McpUnity.Utils; |
| 7 | + |
| 8 | +namespace McpUnity.Tools |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Tool for creating and saving a new Unity scene |
| 12 | + /// </summary> |
| 13 | + public class CreateSceneTool : McpToolBase |
| 14 | + { |
| 15 | + public CreateSceneTool() |
| 16 | + { |
| 17 | + Name = "create_scene"; |
| 18 | + Description = "Creates a new scene and saves it to the specified path"; |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Execute the CreateScene tool with the provided parameters |
| 23 | + /// </summary> |
| 24 | + /// <param name="parameters">Tool parameters as a JObject</param> |
| 25 | + public override JObject Execute(JObject parameters) |
| 26 | + { |
| 27 | + // Parameters |
| 28 | + string sceneName = parameters["sceneName"]?.ToObject<string>(); |
| 29 | + string folderPath = parameters["folderPath"]?.ToObject<string>(); |
| 30 | + bool addToBuildSettings = parameters["addToBuildSettings"]?.ToObject<bool?>() ?? false; |
| 31 | + bool makeActive = parameters["makeActive"]?.ToObject<bool?>() ?? true; |
| 32 | + |
| 33 | + if (string.IsNullOrEmpty(sceneName)) |
| 34 | + { |
| 35 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 36 | + "Required parameter 'sceneName' not provided", |
| 37 | + "validation_error" |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + // Default folder path |
| 42 | + if (string.IsNullOrEmpty(folderPath)) |
| 43 | + { |
| 44 | + folderPath = "Assets"; |
| 45 | + } |
| 46 | + |
| 47 | + // Ensure folder exists |
| 48 | + if (!AssetDatabase.IsValidFolder(folderPath)) |
| 49 | + { |
| 50 | + // Attempt to create nested folders as needed |
| 51 | + string[] parts = folderPath.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries); |
| 52 | + string current = parts.Length > 0 && parts[0] == "Assets" ? "Assets" : "Assets"; |
| 53 | + for (int i = 0; i < parts.Length; i++) |
| 54 | + { |
| 55 | + if (i == 0 && parts[i] == "Assets") continue; |
| 56 | + string next = current + "/" + parts[i]; |
| 57 | + if (!AssetDatabase.IsValidFolder(next)) |
| 58 | + { |
| 59 | + AssetDatabase.CreateFolder(current, parts[i]); |
| 60 | + } |
| 61 | + current = next; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Create unique path for the scene |
| 66 | + string basePath = folderPath.TrimEnd('/'); |
| 67 | + string scenePath = AssetDatabase.GenerateUniqueAssetPath($"{basePath}/{sceneName}.unity"); |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + var newScene = UnityEditor.SceneManagement.EditorSceneManager.NewScene(UnityEditor.SceneManagement.NewSceneSetup.DefaultGameObjects, UnityEditor.SceneManagement.NewSceneMode.Single); |
| 72 | + |
| 73 | + bool saved = UnityEditor.SceneManagement.EditorSceneManager.SaveScene(newScene, scenePath); |
| 74 | + if (!saved) |
| 75 | + { |
| 76 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 77 | + $"Failed to save scene at '{scenePath}'", |
| 78 | + "save_error" |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + AssetDatabase.Refresh(); |
| 83 | + |
| 84 | + // Make the scene active if requested |
| 85 | + if (makeActive) |
| 86 | + { |
| 87 | + UnityEditor.SceneManagement.EditorSceneManager.OpenScene(scenePath, UnityEditor.SceneManagement.OpenSceneMode.Single); |
| 88 | + } |
| 89 | + |
| 90 | + // Optionally add to build settings |
| 91 | + if (addToBuildSettings) |
| 92 | + { |
| 93 | + AddSceneToBuildSettings(scenePath); |
| 94 | + } |
| 95 | + |
| 96 | + McpLogger.LogInfo($"Created scene '{sceneName}' at path '{scenePath}'"); |
| 97 | + |
| 98 | + return new JObject |
| 99 | + { |
| 100 | + ["success"] = true, |
| 101 | + ["type"] = "text", |
| 102 | + ["message"] = $"Successfully created scene '{sceneName}' at path '{scenePath}'", |
| 103 | + ["scenePath"] = scenePath |
| 104 | + }; |
| 105 | + } |
| 106 | + catch (Exception ex) |
| 107 | + { |
| 108 | + return McpUnitySocketHandler.CreateErrorResponse( |
| 109 | + $"Error creating scene: {ex.Message}", |
| 110 | + "scene_creation_error" |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void AddSceneToBuildSettings(string scenePath) |
| 116 | + { |
| 117 | + var scenes = UnityEditor.EditorBuildSettings.scenes; |
| 118 | + |
| 119 | + // Check if already present |
| 120 | + foreach (var s in scenes) |
| 121 | + { |
| 122 | + if (s.path == scenePath) |
| 123 | + { |
| 124 | + return; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + var newList = new UnityEditor.EditorBuildSettingsScene[scenes.Length + 1]; |
| 129 | + for (int i = 0; i < scenes.Length; i++) |
| 130 | + { |
| 131 | + newList[i] = scenes[i]; |
| 132 | + } |
| 133 | + newList[newList.Length - 1] = new UnityEditor.EditorBuildSettingsScene(scenePath, true); |
| 134 | + UnityEditor.EditorBuildSettings.scenes = newList; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
0 commit comments