MCPSettingsProvider.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using LLM.Editor.Client;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace LLM.Editor.Settings
  5. {
  6. /// <summary>
  7. /// Creates the "MCP Assistant" entry in Unity's Project Settings window,
  8. /// allowing for project-wide configuration of the LLM client.
  9. /// </summary>
  10. internal static class MCPSettingsProvider
  11. {
  12. private static SerializedObject _settings;
  13. [SettingsProvider]
  14. public static SettingsProvider CreateMCPSettingsProvider()
  15. {
  16. var provider = new SettingsProvider("Project/MCP Assistant", SettingsScope.Project)
  17. {
  18. label = "MCP Assistant",
  19. guiHandler = OnGUI,
  20. };
  21. return provider;
  22. }
  23. private static void OnGUI(string searchContext)
  24. {
  25. if (_settings == null)
  26. {
  27. var settingsAsset = FindSettingsAsset();
  28. if (settingsAsset != null)
  29. {
  30. _settings = new SerializedObject(settingsAsset);
  31. }
  32. }
  33. if (_settings == null)
  34. {
  35. EditorGUILayout.HelpBox("MCPSettings asset not found. Please create one via 'Assets > Create > LLM > MCP Settings'.", MessageType.Error);
  36. return;
  37. }
  38. _settings.Update();
  39. EditorGUILayout.LabelField("Client Configuration", EditorStyles.boldLabel);
  40. var useDummyClientProp = _settings.FindProperty("useDummyClient");
  41. EditorGUI.BeginChangeCheck();
  42. EditorGUILayout.PropertyField(useDummyClientProp, new GUIContent("Use Dummy Client", "Bypass the real LLM API and use a local, mock client for testing."));
  43. if (EditorGUI.EndChangeCheck())
  44. {
  45. // When the value changes, invalidate the cached client in the factory.
  46. ApiClientFactory.InvalidateClient();
  47. }
  48. EditorGUILayout.HelpBox("Toggling this will switch between the live Gemini API and a local dummy client for testing command execution.", MessageType.Info);
  49. EditorGUILayout.Space();
  50. EditorGUILayout.LabelField("Gemini API Configuration", EditorStyles.boldLabel);
  51. EditorGUILayout.PropertyField(_settings.FindProperty("gcpProjectId"));
  52. EditorGUILayout.PropertyField(_settings.FindProperty("gcpRegion"));
  53. EditorGUILayout.PropertyField(_settings.FindProperty("modelName"));
  54. EditorGUILayout.PropertyField(_settings.FindProperty("gcloudPath"));
  55. _settings.ApplyModifiedProperties();
  56. }
  57. private static MCPSettings FindSettingsAsset()
  58. {
  59. var guids = AssetDatabase.FindAssets($"t:{nameof(MCPSettings)}");
  60. if (guids.Length <= 0) return null;
  61. var path = AssetDatabase.GUIDToAssetPath(guids[0]);
  62. return AssetDatabase.LoadAssetAtPath<MCPSettings>(path);
  63. }
  64. }
  65. }