diff --git a/Assets/Dialog.meta b/Assets/Dialog.meta new file mode 100644 index 0000000..e569a14 --- /dev/null +++ b/Assets/Dialog.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 52aa0b70b4d687141bc04242ebf8202f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Dialog/DialogData.csv b/Assets/Dialog/DialogData.csv new file mode 100644 index 0000000..09deaa3 --- /dev/null +++ b/Assets/Dialog/DialogData.csv @@ -0,0 +1,2 @@ +index,content,type,animation,character,event +0,我会一直视奸你,永远..永远…..,screen,typeing,, diff --git a/Assets/Dialog/DialogData.csv.meta b/Assets/Dialog/DialogData.csv.meta new file mode 100644 index 0000000..db9f44e --- /dev/null +++ b/Assets/Dialog/DialogData.csv.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: f7d9a486e022f8d42b7c0af9a617cde7 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index e42f1fc..95e9aba 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1127,7 +1127,7 @@ GameObject: - component: {fileID: 1157830469} - component: {fileID: 1157830470} m_Layer: 5 - m_Name: Dialog + m_Name: ScreenDialog m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -1164,9 +1164,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 166525e51f1d41a2addfb1df626096fc, type: 3} m_Name: m_EditorClassIdentifier: - dialogsPath: - dialogJson: - typingSpeed: 0.5 + typingSpeed: 0.25 IntroduceText: textMeshPro: {fileID: 1644802857} printTime: 0 @@ -1412,52 +1410,6 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1644802855} m_CullTransparentMesh: 1 ---- !u!1 &1779357123 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1779357125} - - component: {fileID: 1779357124} - m_Layer: 0 - m_Name: KeySettingManger - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!114 &1779357124 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1779357123} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: de55af222f74476380e7cd6bae3af057, type: 3} - m_Name: - m_EditorClassIdentifier: - keyMappings: [] - filePath: ---- !u!4 &1779357125 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1779357123} - serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 5.6259704, y: 2.8222196, z: 5.9930687} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &2138876381 GameObject: m_ObjectHideFlags: 0 @@ -1536,5 +1488,4 @@ SceneRoots: - {fileID: 1107264578} - {fileID: 2138876384} - {fileID: 127902485} - - {fileID: 1779357125} - {fileID: 1306517523} diff --git a/Assets/Scripts/Data/DialogData.cs b/Assets/Scripts/Data/DialogData.cs deleted file mode 100644 index 11efccb..0000000 --- a/Assets/Scripts/Data/DialogData.cs +++ /dev/null @@ -1,5 +0,0 @@ -public struct DialogData -{ - public int index; - public string content; -} \ No newline at end of file diff --git a/Assets/Scripts/Data/DialogManager.cs b/Assets/Scripts/Data/DialogManager.cs new file mode 100644 index 0000000..9ab0ef0 --- /dev/null +++ b/Assets/Scripts/Data/DialogManager.cs @@ -0,0 +1,90 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using UnityEngine; + +namespace Data +{ + public class Dialog + { + public readonly int Index; + public readonly string Content; + public readonly string Type; + public readonly string Animation; + public readonly string Character; + public readonly string DialogEvent; + + public Dialog(int index, string content, string type, string animation, string character, string dialogEvent) + { + Index = index; + Content = content; + Type = type; + Animation = animation; + Character = character; + DialogEvent = dialogEvent; + } + } + + public class DialogManager : MonoBehaviour + { + public string filePath = "Assets/Dialog/DialogData.csv"; + private readonly List _dialog = new(); + + private static DialogManager _instance; + public static DialogManager Instance + { + get + { + if (_instance) + return _instance; + _instance = FindObjectOfType() ?? + new GameObject("DialogData").AddComponent(); + return _instance; + } + } + private void Awake() + { + if (_instance != null && _instance != this) + { + Destroy(gameObject); + } + else + { + LoadCsv(filePath); + _instance = this; + } + } + + private void LoadCsv(string path) + { + var lines = File.ReadAllLines(path); + print(lines); + for (var i = 1; i < lines.Length; i++) + { + var values = lines[i].Split(','); + + var dialog = new Dialog( + int.Parse(values[0]), + values[1], + values[2], + values[3], + values[4], + values[5] + ); + + _dialog.Add(dialog); + } + } + + public Dialog GetDialogByIndex(int index) + { + foreach (var dialog in _dialog.Where(dialogue => dialogue.Index == index)) + { + return dialog; + } + + Debug.LogWarning($"Dialog with index {index} not found."); + return null; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Data/DialogData.cs.meta b/Assets/Scripts/Data/DialogManager.cs.meta similarity index 100% rename from Assets/Scripts/Data/DialogData.cs.meta rename to Assets/Scripts/Data/DialogManager.cs.meta diff --git a/Assets/Scripts/Event/EventArgs/DialogPopArgs.cs b/Assets/Scripts/Event/EventArgs/DialogPopArgs.cs index 44bd664..723b9f1 100644 --- a/Assets/Scripts/Event/EventArgs/DialogPopArgs.cs +++ b/Assets/Scripts/Event/EventArgs/DialogPopArgs.cs @@ -3,6 +3,7 @@ public class DialogPopArgs : System.EventArgs { public int Index { get; private set; } + public bool IsScreenDialog { get;private set; } public DialogPopArgs(int index) { diff --git a/Assets/Scripts/UI/Dialog.cs b/Assets/Scripts/UI/ScreenDialog.cs similarity index 58% rename from Assets/Scripts/UI/Dialog.cs rename to Assets/Scripts/UI/ScreenDialog.cs index 2c3acda..de6d328 100644 --- a/Assets/Scripts/UI/Dialog.cs +++ b/Assets/Scripts/UI/ScreenDialog.cs @@ -1,23 +1,16 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; +using System.Collections; +using Data; using Event; using Event.EventArgs; -using Newtonsoft.Json; using TMPro; using UnityEngine; namespace UI { - public class Dialog : MonoBehaviour + public class ScreenDialog : MonoBehaviour { - [SerializeField] private string dialogsPath; - [SerializeField] private string dialogJson; - private Dictionary _dialogsMap = new(); - public float typingSpeed = 0.05f; - public string IntroduceText; + public string introduceText; private string _currentText = ""; private float _timer; private int _currentIndex; @@ -27,22 +20,7 @@ namespace UI public float printTime; public float maxPrintTime=10; public bool isTime2Break; - private void Awake() - { - dialogsPath = Application.persistentDataPath + "/" + "Dialogs.json"; - - if (File.Exists(dialogsPath)) - { - dialogJson = File.ReadAllText(dialogsPath); - _dialogsMap = JsonConvert.DeserializeObject>(dialogJson); - } - else - { - _dialogsMap.Add(0, "我会视奸你,永远...永远......"); - dialogJson = JsonConvert.SerializeObject(_dialogsMap, Formatting.Indented); - File.WriteAllText(dialogsPath, dialogJson); - } - } + private void Update() { @@ -52,11 +30,11 @@ namespace UI { isTime2Break = true; } - if (_currentText == IntroduceText) return; + if (_currentText == introduceText) return; _timer += Time.deltaTime; if (!(_timer >= typingSpeed)) return; _timer = 0f; - _currentText += IntroduceText[_currentIndex]; + _currentText += introduceText[_currentIndex]; textMeshPro.text = _currentText; _currentIndex++; } @@ -66,12 +44,6 @@ namespace UI EventManager.Instance.DialogPop += StartPrinting; } - /*public void OpenDialog(DialogPopArgs e) - { - Debug.Log(_dialogsMap[e.Index]); - }*/ - - //闪烁光标 private IEnumerator BlinkCursor() { while (true) @@ -95,19 +67,19 @@ namespace UI textMeshPro.text = textMeshPro.text[..^1]; _isBlinking = false; } - yield return new WaitForSeconds(0.3f); // 闪烁速度 + yield return new WaitForSeconds(0.3f); } } - // 调用此方法开始打印文字 private void StartPrinting(DialogPopArgs e) { + if(!e.IsScreenDialog) return; if(printTime != 0) return; _start = true; _currentText = ""; - - - IntroduceText = _dialogsMap[e.Index]; + + + introduceText = DialogManager.Instance.GetDialogByIndex(e.Index).Content; _currentIndex = 0; _timer = 0f; textMeshPro.fontSize = 80; diff --git a/Assets/Scripts/UI/Dialog.cs.meta b/Assets/Scripts/UI/ScreenDialog.cs.meta similarity index 100% rename from Assets/Scripts/UI/Dialog.cs.meta rename to Assets/Scripts/UI/ScreenDialog.cs.meta