-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Studio-23-xyz/Custom-Editor
Custom editor added. Multiple start node support added
- Loading branch information
Showing
31 changed files
with
1,001 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/Data/DialogueLineNodeEditor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Studio23.SS2.DialogueSystem.Utility; | ||
using UnityEditor; | ||
using UnityEditor.Localization; | ||
using UnityEngine; | ||
using UnityEngine.Localization; | ||
using UnityEngine.Localization.Settings; | ||
using UnityEngine.Localization.Tables; | ||
using XNodeEditor; | ||
|
||
namespace Studio23.SS2.DialogueSystem.Data | ||
{ | ||
[CustomNodeEditor(typeof(DialogueLineNodeBase))] | ||
public class DialogueLineNodeEditor : NodeEditor | ||
{ | ||
public static string DEFAULT_LOCALE = "en"; | ||
private string _text; | ||
|
||
public override void OnBodyGUI() | ||
{ | ||
// Update serialized object's representation | ||
serializedObject.Update(); | ||
var dialogueLineNode = target as DialogueLineNodeBase; | ||
|
||
var collection = LocalizationEditorSettings.GetStringTableCollection(dialogueLineNode.DialogueLocalizedString.TableReference); | ||
var defaultLocaleTable = collection.GetTable(DEFAULT_LOCALE) as StringTable; | ||
if (defaultLocaleTable == null) | ||
{ | ||
EditorGUILayout.HelpBox("No language Table", MessageType.Error); | ||
return; | ||
} | ||
DrawCustomDialogueTextbox(defaultLocaleTable, dialogueLineNode, collection); | ||
base.OnBodyGUI(); | ||
} | ||
|
||
private void DrawCustomDialogueTextbox(StringTable defaultTable, DialogueLineNodeBase dialogueLineNode, | ||
StringTableCollection collection) | ||
{ | ||
var entry = defaultTable.GetEntry(dialogueLineNode.DialogueLocalizedString.TableEntryReference.KeyId); | ||
if (entry == null) | ||
{ | ||
_text = EditorGUILayout.TextArea(_text); | ||
|
||
if(string.IsNullOrEmpty(_text)) | ||
{ | ||
EditorGUILayout.HelpBox("Write something", MessageType.Warning); | ||
} | ||
else | ||
{ | ||
if (GUILayout.Button("Create localized line")) | ||
{ | ||
CreateNewEntry(collection, defaultTable, dialogueLineNode); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
var localizedText = entry.Value; | ||
if (string.IsNullOrEmpty(_text)) | ||
{ | ||
_text = localizedText; | ||
} | ||
_text = EditorGUILayout.TextArea(_text); | ||
if (_text != localizedText) | ||
{ | ||
EditorGUILayout.HelpBox("Localized string doesn't match. PLEASE SAVE", MessageType.Warning); | ||
if (GUILayout.Button("SAVE")) | ||
{ | ||
Debug.Log($"{localizedText} -> {_text}"); | ||
defaultTable.SetEntry(dialogueLineNode.DialogueLocalizedString, _text); | ||
defaultTable.SaveChanges(); | ||
dialogueLineNode.DialogueLocalizedString.RefreshString(); | ||
EditorUtility.SetDirty(dialogueLineNode); | ||
}else if (GUILayout.Button("RESET")) | ||
{ | ||
_text = localizedText; | ||
} | ||
} | ||
if (GUILayout.Button("Replace with new Entry")) | ||
{ | ||
CreateNewEntry(collection, defaultTable, dialogueLineNode); | ||
} | ||
} | ||
} | ||
|
||
private void CreateNewEntry(StringTableCollection collection, StringTable englishTable, | ||
DialogueLineNodeBase dialogueLineNode) | ||
{ | ||
StringTableEntry entry; | ||
var key = _text.Substring(0, Mathf.Min(10, _text.Length)); | ||
collection.SharedData.AddKey(key); | ||
entry = englishTable.AddEntry(key, _text); | ||
|
||
Debug.Log($"new dialogue line entry {_text}"); | ||
|
||
englishTable.SaveChanges(); | ||
|
||
dialogueLineNode.DialogueLocalizedString = | ||
new LocalizedString(collection.SharedData.TableCollectionNameGuid, entry.KeyId); | ||
EditorUtility.SetDirty(dialogueLineNode); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/Data/DialogueLineNodeEditor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/Data/LocalizationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using UnityEditor; | ||
using UnityEditor.Localization; | ||
using UnityEngine.Localization; | ||
using UnityEngine.Localization.Tables; | ||
|
||
namespace Studio23.SS2.DialogueSystem.Utility | ||
{ | ||
public static class LocalizationExtensions | ||
{ | ||
public static void SetEntry(this StringTable table, LocalizedString localizedString, string newText) | ||
{ | ||
table.AddEntry(localizedString.TableEntryReference.KeyId, newText); | ||
} | ||
|
||
public static void SaveChanges(this StringTable table) | ||
{ | ||
EditorUtility.SetDirty(table.SharedData); | ||
EditorUtility.SetDirty(table); | ||
} | ||
/// <summary> | ||
/// Use this to get localized string value in editor | ||
/// for whatever reason notmal getString doesn't work | ||
/// </summary> | ||
/// <param name="localizedString"></param> | ||
/// <param name="locale"></param> | ||
/// <returns></returns> | ||
public static string GetLocalizedStringInEditor(this LocalizedString localizedString, string locale = "en") | ||
{ | ||
var collection = LocalizationEditorSettings.GetStringTableCollection(localizedString.TableReference); | ||
var englishTable = collection.GetTable(locale) as StringTable; | ||
var entry = englishTable.GetEntry(localizedString.TableEntryReference.KeyId); | ||
if (entry == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return entry.Value; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/Data/LocalizationExtensions.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.