1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using JetBrains.Annotations;
- namespace LLM.Editor.Commands
- {
- [System.Serializable]
- public class CreateScriptParams
- {
- public string scriptName;
- public string scriptContent;
- }
- [UsedImplicitly]
- public class CreateScriptCommand : ICommand
- {
- private readonly CreateScriptParams _params;
- public CreateScriptCommand(string jsonParams)
- {
- Debug.Log($"[CreateScriptCommand] Received parameters: {jsonParams}");
- _params = JsonUtility.FromJson<CreateScriptParams>(jsonParams);
- }
- public void Execute(Data.CommandContext context)
- {
- if (_params == null || string.IsNullOrEmpty(_params.scriptName) || string.IsNullOrEmpty(_params.scriptContent))
- {
- Debug.LogError("[CreateScriptCommand] Invalid parameters.");
- return;
- }
-
- // For simplicity, we'll save scripts to the root of the Assets folder.
- var path = Path.Combine(Application.dataPath, $"{_params.scriptName}.cs");
- File.WriteAllText(path, _params.scriptContent);
- Debug.Log($"[CreateScriptCommand] Created script at: {path}");
- AssetDatabase.Refresh(); // This will trigger a recompile.
- }
- }
- }
|