CreateScriptCommand.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using LLM.Editor.Helper;
  5. using JetBrains.Annotations;
  6. namespace LLM.Editor.Commands
  7. {
  8. [System.Serializable]
  9. public class CreateScriptParams
  10. {
  11. public string scriptName;
  12. public string scriptContent;
  13. }
  14. [UsedImplicitly]
  15. public class CreateScriptCommand : ICommand
  16. {
  17. private readonly CreateScriptParams _params;
  18. public CreateScriptCommand(string jsonParams)
  19. {
  20. _params = jsonParams?.FromJson<CreateScriptParams>();
  21. }
  22. public void Execute(Data.CommandContext context)
  23. {
  24. if (_params == null || string.IsNullOrEmpty(_params.scriptName) || string.IsNullOrEmpty(_params.scriptContent))
  25. {
  26. Debug.LogError("[CreateScriptCommand] Invalid parameters.");
  27. return;
  28. }
  29. // For simplicity, we'll save scripts to the root of the Assets folder.
  30. var path = Path.Combine(Application.dataPath, $"{_params.scriptName}.cs");
  31. File.WriteAllText(path, _params.scriptContent);
  32. context.CurrentSubject = path;
  33. Debug.Log($"[CreateScriptCommand] Created script at: {path}");
  34. AssetDatabase.Refresh();
  35. }
  36. }
  37. }