CreateScriptCommand.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using JetBrains.Annotations;
  5. namespace LLM.Editor.Commands
  6. {
  7. [System.Serializable]
  8. public class CreateScriptParams
  9. {
  10. public string scriptName;
  11. public string scriptContent;
  12. }
  13. [UsedImplicitly]
  14. public class CreateScriptCommand : ICommand
  15. {
  16. private readonly CreateScriptParams _params;
  17. public CreateScriptCommand(string jsonParams)
  18. {
  19. Debug.Log($"[CreateScriptCommand] Received parameters: {jsonParams}");
  20. _params = JsonUtility.FromJson<CreateScriptParams>(jsonParams);
  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. Debug.Log($"[CreateScriptCommand] Created script at: {path}");
  33. AssetDatabase.Refresh(); // This will trigger a recompile.
  34. }
  35. }
  36. }