Skip to content

Commit

Permalink
Merge pull request #16 from Studio-23-xyz/Custom-Editor
Browse files Browse the repository at this point in the history
Custom editor added. Multiple start node support added
  • Loading branch information
BDeshiDev authored Mar 25, 2024
2 parents b37956a + d257c85 commit c54f47c
Show file tree
Hide file tree
Showing 31 changed files with 1,001 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ MonoBehaviour:
m_ReadOnly: 1
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: c2bd177689020fd468d2d17b65acc54e
m_Address: Assets/Packages/com.studio23.ss2.dialoguesystem/Samples/DialogueChoiceDemo/New
Table 2 Shared Data.asset
m_ReadOnly: 1
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
m_ReadOnly: 1
m_Settings: {fileID: 11400000, guid: 45bcaa0f9119bdc44b426e7814210719, type: 2}
m_SchemaSet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ MonoBehaviour:
m_SerializedLabels:
- Locale-en
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 5f49daacee4b45b4a94f58f3c7c49cb7
m_Address: New Table 2_en
m_ReadOnly: 1
m_SerializedLabels:
- Locale-en
FlaggedDuringContentUpdateRestriction: 0
m_ReadOnly: 1
m_Settings: {fileID: 11400000, guid: 45bcaa0f9119bdc44b426e7814210719, type: 2}
m_SchemaSet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ MonoBehaviour:
m_SerializedLabels:
- Locale-ja
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: b3f1e7a888bb1cd4480171a27de7c79d
m_Address: New Table 2_ja
m_ReadOnly: 1
m_SerializedLabels:
- Locale-ja
FlaggedDuringContentUpdateRestriction: 0
m_ReadOnly: 1
m_Settings: {fileID: 11400000, guid: 45bcaa0f9119bdc44b426e7814210719, type: 2}
m_SchemaSet:
Expand Down
6 changes: 6 additions & 0 deletions Assets/Packages/com.studio23.ss2.dialoguesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Changelog
## [v0.3.10] - 2023-3-22
1. Can write dialogue without needing to setup/open localized string
1. Dialogue is synced with localized string.
1. Custom dialogue node Editor class added.
1. Multiple start nodes for dialogue
1. Dialogue Start Helper class with custom editor that shows all start nodes and allows selecting one of them.
## [v0.3.1] - 2023-4-1
1. Dialogue nodes now used localized strings
2. Sample scene updated with UI that supports localized strings
Expand Down
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"references": [
"GUID:3e1deda3cfa83394e827391555d95b6f",
"GUID:b8e24fd1eb19b4226afebb2810e3c19b",
"GUID:002c1bbed08fa44d282ef34fd5edb138"
"GUID:002c1bbed08fa44d282ef34fd5edb138",
"GUID:eec0964c48f6f4e40bc3ec2257ccf8c5",
"GUID:2e77701fe0ceda44fb763f03ee704c37"
],
"includePlatforms": [
"Editor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,33 @@ public void StartDialogue(DialogueGraph graph)
{
PlayDialogue(graph);
}

public void StartDialogue(DialogueGraph graph, DialogueNodeBase startNode)
{
PlayDialogue(graph, startNode);
}

public void StartDialogue()
{
PlayDialogue(_currentGraph);
}

public async UniTask PlayDialogue(DialogueGraph graph)
{
PlayDialogue(graph, graph.StartNode);
}

public async UniTask PlayDialogue(DialogueGraph graph, DialogueNodeBase startNode)
{
_currentGraph = graph;
_currentGraph.Initialize();

_currentGraph.HandleDialogueStarted();
OnDialogueStarted?.Invoke(_currentGraph);

_curNode = _currentGraph.StartNode;
_curNode = startNode;
OnDialogueStarted?.Invoke(_currentGraph);
while (_curNode != null)
{
Debug.Log("node = " + _curNode, _curNode);
Debug.Log("Play = " + _curNode, _curNode);
await _curNode.Play();

_curNode = _curNode.GetNextNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections.Generic;
using Studio23.SS2.DialogueSystem.Runtime.Data;
using UnityEngine;
using UnityEngine.Localization.Tables;
using UnityEngine.Serialization;
using XNode;

# if UNITY_EDITOR
using UnityEditor;
#endif
namespace Studio23.SS2.DialogueSystem.Data
{
[CreateAssetMenu(menuName = "Studio-23/Dialogue System/New Dialogue Graph", fileName ="Dialogue Graph")]
Expand All @@ -28,6 +31,39 @@ public class DialogueGraph : NodeGraph
public event Action<DialogueGraph> OnDialogueStarted;
public event Action<DialogueGraph> OnDialogueEnded;

public override Node AddNode(Type type)
{
var node = base.AddNode(type);

if (TryGetDefaultTable(out var defaultTable))
{
if (node is DialogueLineNodeBase dialogueNodeBase)
{
dialogueNodeBase.SetLocalizationTable(defaultTable);
}
}

return node;
}

public bool TryGetDefaultTable(out TableReference defaultTableReference)
{
defaultTableReference = default;
var startNode = FindStartNode();
if (startNode == null)
{
return false;
}
var firstNode = startNode as DialogueLineNodeBase;
if (firstNode != null)
{
defaultTableReference = firstNode.GetLocalizationTable();
return true;
}

return false;
}

public void Initialize()
{
if (_initialized)
Expand Down Expand Up @@ -95,23 +131,65 @@ public void HandleDialogueEnded()
OnDialogueEnded?.Invoke(this);
}

private void FindStartNode()
[ContextMenu("SetAllLanguageTablesToDefault")]
public void SetAllTablesToDefault()
{
if (_startNode != null)
if (!TryGetDefaultTable(out var defaultTable))
{
return;
}
foreach (var node in nodes)
{
Debug.Log("node " , node);
if (node is DialogueLineNodeBase dialogueLineNodeBase)
{
dialogueLineNodeBase.SetLocalizationTable(defaultTable);
# if UNITY_EDITOR
EditorUtility.SetDirty(dialogueLineNodeBase);
#endif
}
}
}

[ContextMenu("SET ALL Empty DIALOGUE TABLES to DEFAULT")]
public void SetEmptyDialogueTablesToDefault()
{
if (!TryGetDefaultTable(out var table))
{
return;
}
foreach (var node in nodes)
{
if (node is DialogueLineNodeBase dialogueLineNodeBase)
{
if (string.IsNullOrEmpty(dialogueLineNodeBase.GetLocalizationTable().TableCollectionName))
{
Debug.LogWarning($"{dialogueLineNodeBase} replace table to {table}");
dialogueLineNodeBase.SetLocalizationTable(table);
# if UNITY_EDITOR
EditorUtility.SetDirty(dialogueLineNodeBase);
#endif
}
}
}
}

private DialogueNodeBase FindStartNode()
{
if (_startNode != null)
{
return _startNode;
}
foreach (var node in nodes)
{
if (node is DialogueStartNode startNode)
{
_startNode = startNode;
return;
return _startNode;
}
}

Debug.LogError($"NO START NODE FOR DIALOGUE GRAPH {this}");
return null;
}

}
Expand Down
Loading

0 comments on commit c54f47c

Please sign in to comment.