From d163629ba98d677263bb9f34709159becdf62a77 Mon Sep 17 00:00:00 2001 From: Alexandre Coirier Date: Mon, 1 Jun 2026 17:13:42 +0200 Subject: [PATCH] fix: LocalizedSubtitle --- .../Editor/LocalizedTextFieldDrawer.cs | 127 ------------------ .../Editor/LocalizedTextFieldDrawer.cs.meta | 2 - .../Dialogue/Runtime/LocalizedSubtitle.cs | 71 +++++----- 3 files changed, 38 insertions(+), 162 deletions(-) delete mode 100644 Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs delete mode 100644 Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs.meta diff --git a/Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs b/Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs deleted file mode 100644 index 192d3d36..00000000 --- a/Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs +++ /dev/null @@ -1,127 +0,0 @@ -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(textDataPath); - if (existingData != null) - return existingData; - - // Create new OctoLocalizedTextData - OctoLocalizedTextData newTextData = ScriptableObject.CreateInstance(); - - // Initialize via Init() method - newTextData.Init(); - - // Save asset - AssetDatabase.CreateAsset(newTextData, textDataPath); - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); - - return newTextData; - } - } -} diff --git a/Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs.meta b/Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs.meta deleted file mode 100644 index a3e66bab..00000000 --- a/Assets/_Content/Scripts/Editor/LocalizedTextFieldDrawer.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 6566d44f45287f943b77aa92aaefeeb0 \ No newline at end of file diff --git a/Assets/_Content/Scripts/Features/Dialogue/Runtime/LocalizedSubtitle.cs b/Assets/_Content/Scripts/Features/Dialogue/Runtime/LocalizedSubtitle.cs index a8b87f20..3d1e4890 100644 --- a/Assets/_Content/Scripts/Features/Dialogue/Runtime/LocalizedSubtitle.cs +++ b/Assets/_Content/Scripts/Features/Dialogue/Runtime/LocalizedSubtitle.cs @@ -1,57 +1,62 @@ -using UnityEngine; -using OctoCore.Localization; -using OctoFramework = OctoCore.OctoCore; +using System; using Unheimlich.UI; +using UnityEngine; +using OctoFramework = OctoCore.OctoCore; +using OctoCore.Localization; namespace Unheimlich.Features.Dialogue { - /// - /// Bridges OctoLocalizedText and PerLetterFontShifter. - /// OctoLocalizedText writes to the source TMP, which is disabled by the shifter — - /// this component intercepts Localize() and forwards the result to SetText() instead. - /// - [RequireComponent(typeof(OctoLocalizedText))] [RequireComponent(typeof(PerLetterFontShifter))] public class LocalizedSubtitle : MonoBehaviour { - private OctoLocalizedText _localizedText; + [SerializeField] private OctoLocalizedTextData _data; + + private OctoLocalizedText _localizedText; // A retirer private PerLetterFontShifter _shifter; + private Action localize; - private void Awake() + void Awake() { - _localizedText = GetComponent(); + _localizedText = GetComponent(); // A retirer _shifter = GetComponent(); + + if (!OctoFramework.Instance) + { + Debug.LogWarning($"OctoCore.Instance not found"); + return; + } + + Localize(); + OctoFramework.Instance.Localization.SubscribeOnLanguageWrittenChange(localize = Localize); } - private void Start() + void OnDestroy() { - // OctoLocalizedText.Awake() has already run and called Localize() into the dead TMP. - // Pull the resolved text back out and push it into the shifter. - Refresh(); + if (!OctoFramework.Instance) return; - // Re-hook on language change so the shifter stays in sync. - if (OctoFramework.Instance) - OctoFramework.Instance.Localization.SubscribeOnLanguageWrittenChange(Refresh); + if (localize != null) + { + OctoFramework.Instance.Localization.UnsubscribeOnLanguageWrittenChange(localize); + localize = null; + } } - private void OnDestroy() + private void Localize() { - if (OctoFramework.Instance) - OctoFramework.Instance.Localization.UnsubscribeOnLanguageWrittenChange(Refresh); - } + string localized = "Not localized"; - /// - /// Pulls the current localized string and pushes it to the shifter. - /// - private void Refresh() - { - // Force OctoLocalizedText to re-resolve the string into the source TMP. - _localizedText.Localize(); + if (_data) + { + localized = OctoLocalization.GetLocalizedText(_data); + } + else // A retirer + { // A retirer + _localizedText.Localize(); // A retirer + localized = GetComponent().text; // A retirer + } // A retirer // Read it back from the source TMP (it's disabled but the .text field is still set). - string resolved = GetComponent().text; - - _shifter.SetText(resolved); + _shifter.SetText(localized); } } } \ No newline at end of file