npc-clients-unity/Editor/GumYumSettingsWindow.cs

139 lines
No EOL
4.4 KiB
C#

using UnityEngine;
using UnityEditor;
namespace GumYum.NPC.Editor {
public class GumYumSettingsWindow : EditorWindow {
private string apiKey = "";
private string apiSecret = "";
private bool showSecret = false;
[MenuItem("Window/GumYum NPC/Settings")]
public static void ShowWindow() {
var window = GetWindow<GumYumSettingsWindow>("GumYum NPC Settings");
window.minSize = new Vector2(400, 200);
}
private void OnEnable() {
// Load saved credentials
apiKey = EditorPrefs.GetString("GumYum_APIKey", "");
apiSecret = EditorPrefs.GetString("GumYum_APISecret", "");
}
private void OnGUI() {
GUILayout.Label("GumYum NPC SDK Settings", EditorStyles.boldLabel);
GUILayout.Space(10);
GUILayout.Label("API Credentials", EditorStyles.label);
GUILayout.Label("Get your API keys from https://npc.gumyum.com",
EditorStyles.miniLabel);
GUILayout.Space(5);
// API Key
GUILayout.BeginHorizontal();
GUILayout.Label("API Key:", GUILayout.Width(80));
apiKey = EditorGUILayout.TextField(apiKey);
GUILayout.EndHorizontal();
// API Secret
GUILayout.BeginHorizontal();
GUILayout.Label("API Secret:", GUILayout.Width(80));
if (showSecret) {
apiSecret = EditorGUILayout.TextField(apiSecret);
} else {
apiSecret = EditorGUILayout.PasswordField(apiSecret);
}
showSecret = GUILayout.Toggle(showSecret, "Show", GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.Space(10);
// Save button
if (GUILayout.Button("Save Settings")) {
SaveSettings();
}
GUILayout.Space(10);
// Help section
EditorGUILayout.HelpBox("These credentials will be saved in Editor " +
"preferences and are not included in builds. " +
"For production, set credentials at runtime " +
"or use environment variables.",
MessageType.Info);
GUILayout.FlexibleSpace();
// Links
if (GUILayout.Button("Open Documentation")) {
Application.OpenURL("https://docs.gumyum.com/sdk/unity");
}
if (GUILayout.Button("Get API Keys")) {
Application.OpenURL("https://npc.gumyum.com");
}
}
private void SaveSettings() {
EditorPrefs.SetString("GumYum_APIKey", apiKey);
EditorPrefs.SetString("GumYum_APISecret", apiSecret);
Debug.Log("GumYum NPC settings saved!");
// If there's an active client in play mode, update it
if (Application.isPlaying && Unity.GumYumUnityClient.Instance != null) {
Unity.GumYumUnityClient.Instance.SetCredentials(apiKey, apiSecret);
}
}
}
// Custom property drawer for NPC display in inspector
[CustomEditor(typeof(MonoBehaviour), true)]
public class NPCInspector : UnityEditor.Editor {
private bool hasNPCField = false;
private void OnEnable() {
// Check if this MonoBehaviour has any NPC fields
var fields =
target.GetType().GetFields(System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
foreach (var field in fields) {
if (field.FieldType == typeof(NPC)) {
hasNPCField = true;
break;
}
}
}
public override void OnInspectorGUI() {
base.OnInspectorGUI();
if (hasNPCField && Application.isPlaying) {
GUILayout.Space(10);
GUILayout.Label("NPC Information", EditorStyles.boldLabel);
var fields =
target.GetType().GetFields(System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
foreach (var field in fields) {
if (field.FieldType == typeof(NPC)) {
var npc = field.GetValue(target) as NPC;
if (npc != null) {
EditorGUILayout.LabelField("Name:", npc.Name);
EditorGUILayout.LabelField("Profession:", npc.Profession);
EditorGUILayout.LabelField("Personality Type:",
npc.PersonalityType.ToString());
EditorGUILayout.LabelField("Current Mood:", npc.GetMood());
EditorGUILayout.LabelField("Stress Level:",
npc.GetStressLevel().ToString());
EditorGUILayout.LabelField("Location:", npc.GetLocation());
}
}
}
}
}
}
}