Linking the octo localization system to the dialogue system
This commit is contained in:
@@ -6,7 +6,6 @@ using Unheimlich.Data.Dialogues;
|
||||
public class DialogueConversationEditor : Editor
|
||||
{
|
||||
private SerializedProperty _speakerNameProp;
|
||||
private SerializedProperty _speakerNameLocalizationKeyProp;
|
||||
private SerializedProperty _displayModeProp;
|
||||
private SerializedProperty _dialogueNodesProp;
|
||||
|
||||
@@ -37,7 +36,6 @@ public class DialogueConversationEditor : Editor
|
||||
private void OnEnable()
|
||||
{
|
||||
_speakerNameProp = serializedObject.FindProperty("SpeakerName");
|
||||
_speakerNameLocalizationKeyProp = serializedObject.FindProperty("SpeakerNameLocalizationKey");
|
||||
_displayModeProp = serializedObject.FindProperty("DisplayMode");
|
||||
_dialogueNodesProp = serializedObject.FindProperty("DialogueNodes");
|
||||
}
|
||||
@@ -50,7 +48,6 @@ public class DialogueConversationEditor : Editor
|
||||
|
||||
EditorGUILayout.LabelField("Conversation Setup", EditorStyles.boldLabel);
|
||||
EditorGUILayout.PropertyField(_speakerNameProp);
|
||||
EditorGUILayout.PropertyField(_speakerNameLocalizationKeyProp, new GUIContent("Localization Key (Optional)"));
|
||||
|
||||
EditorGUILayout.Space(5);
|
||||
|
||||
|
||||
@@ -6,14 +6,12 @@ using Unheimlich.Data.Dialogues;
|
||||
public class DialogueNodeEditor : Editor
|
||||
{
|
||||
private SerializedProperty _dialogueTextProp;
|
||||
private SerializedProperty _dialogueTextLocalizationKeyProp;
|
||||
private SerializedProperty _choicesProp;
|
||||
private SerializedProperty _autoCloseDelayProp;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_dialogueTextProp = serializedObject.FindProperty("DialogueText");
|
||||
_dialogueTextLocalizationKeyProp = serializedObject.FindProperty("DialogueTextLocalizationKey");
|
||||
_choicesProp = serializedObject.FindProperty("Choices");
|
||||
_autoCloseDelayProp = serializedObject.FindProperty("AutoCloseDelay");
|
||||
}
|
||||
@@ -26,7 +24,6 @@ public class DialogueNodeEditor : Editor
|
||||
|
||||
EditorGUILayout.LabelField("Node Content", EditorStyles.boldLabel);
|
||||
EditorGUILayout.PropertyField(_dialogueTextProp);
|
||||
EditorGUILayout.PropertyField(_dialogueTextLocalizationKeyProp, new GUIContent("Localization Key (Optional)"));
|
||||
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
@@ -89,7 +86,7 @@ public class DialogueNodeEditor : Editor
|
||||
|
||||
DialogueChoiceData newChoice = new DialogueChoiceData
|
||||
{
|
||||
ChoiceText = "New choice",
|
||||
ChoiceText = null, // Will need to assign OctoLocalizedTextData manually
|
||||
NextNode = null
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using OctoCore.Localization;
|
||||
using System.IO;
|
||||
|
||||
namespace Unheimlich.Editor
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(OctoLocalizedTextData))]
|
||||
public class LocalizedTextFieldDrawer : PropertyDrawer
|
||||
{
|
||||
private const float LINE_HEIGHT = 20f;
|
||||
private const float SPACING = 2f;
|
||||
private const float LABEL_WIDTH = 80f;
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
// Height for label + EN field + FR field
|
||||
return LINE_HEIGHT * 3 + SPACING * 2;
|
||||
}
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
EditorGUI.BeginProperty(position, label, property);
|
||||
|
||||
OctoLocalizedTextData textData = property.objectReferenceValue as OctoLocalizedTextData;
|
||||
|
||||
// If null, create new OctoLocalizedTextData
|
||||
if (textData == null)
|
||||
{
|
||||
textData = CreateLocalizedTextData(property);
|
||||
property.objectReferenceValue = textData;
|
||||
property.serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
// Draw label
|
||||
Rect labelRect = new Rect(position.x, position.y, position.width, LINE_HEIGHT);
|
||||
EditorGUI.LabelField(labelRect, label, EditorStyles.boldLabel);
|
||||
|
||||
// Draw EN field
|
||||
Rect enRect = new Rect(position.x, position.y + LINE_HEIGHT + SPACING, position.width, LINE_HEIGHT);
|
||||
DrawLanguageField(enRect, textData, OctoLanguages.English, "EN");
|
||||
|
||||
// Draw FR field
|
||||
Rect frRect = new Rect(position.x, position.y + (LINE_HEIGHT + SPACING) * 2, position.width, LINE_HEIGHT);
|
||||
DrawLanguageField(frRect, textData, OctoLanguages.French, "FR");
|
||||
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
|
||||
private void DrawLanguageField(Rect position, OctoLocalizedTextData textData, OctoLanguages language, string labelText)
|
||||
{
|
||||
if (textData == null) return;
|
||||
|
||||
// Ensure dictionary is initialized
|
||||
if (textData.Languages.Dictionary == null || textData.Languages.Dictionary.Count == 0)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
textData.Init();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Get language key
|
||||
string langKey = language.ToString();
|
||||
|
||||
// Get current value
|
||||
string currentValue = "";
|
||||
if (textData.Languages.Dictionary.TryGetValue(langKey, out string value))
|
||||
{
|
||||
currentValue = value;
|
||||
}
|
||||
|
||||
// Draw label
|
||||
Rect labelRect = new Rect(position.x, position.y, LABEL_WIDTH, position.height);
|
||||
EditorGUI.LabelField(labelRect, labelText + ":");
|
||||
|
||||
// Draw text field
|
||||
Rect fieldRect = new Rect(position.x + LABEL_WIDTH, position.y, position.width - LABEL_WIDTH, position.height);
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
string newValue = EditorGUI.TextField(fieldRect, currentValue);
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
// Update via indexer (Dictionary is read-only property but supports indexer)
|
||||
textData.Languages.Dictionary[langKey] = newValue;
|
||||
EditorUtility.SetDirty(textData);
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
}
|
||||
|
||||
private OctoLocalizedTextData CreateLocalizedTextData(SerializedProperty property)
|
||||
{
|
||||
// Get the object being edited
|
||||
Object targetObject = property.serializedObject.targetObject;
|
||||
string assetPath = AssetDatabase.GetAssetPath(targetObject);
|
||||
|
||||
if (string.IsNullOrEmpty(assetPath))
|
||||
return null;
|
||||
|
||||
string directory = Path.GetDirectoryName(assetPath);
|
||||
string fileName = Path.GetFileNameWithoutExtension(assetPath);
|
||||
string propertyPath = property.propertyPath.Replace(".", "_");
|
||||
|
||||
// Create unique name
|
||||
string textDataName = $"text_{fileName}_{propertyPath}";
|
||||
string textDataPath = $"{directory}/{textDataName}.asset";
|
||||
|
||||
// Check if already exists
|
||||
OctoLocalizedTextData existingData = AssetDatabase.LoadAssetAtPath<OctoLocalizedTextData>(textDataPath);
|
||||
if (existingData != null)
|
||||
return existingData;
|
||||
|
||||
// Create new OctoLocalizedTextData
|
||||
OctoLocalizedTextData newTextData = ScriptableObject.CreateInstance<OctoLocalizedTextData>();
|
||||
|
||||
// Initialize via Init() method
|
||||
newTextData.Init();
|
||||
|
||||
// Save asset
|
||||
AssetDatabase.CreateAsset(newTextData, textDataPath);
|
||||
AssetDatabase.SaveAssets();
|
||||
AssetDatabase.Refresh();
|
||||
|
||||
return newTextData;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6566d44f45287f943b77aa92aaefeeb0
|
||||
Reference in New Issue
Block a user