也是向csv投降了

This commit is contained in:
2024-09-18 09:44:03 +08:00
parent 180c8f1038
commit 7257c39577
10 changed files with 122 additions and 96 deletions

8
Assets/Dialog.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 52aa0b70b4d687141bc04242ebf8202f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,2 @@
index,content,type,animation,character,event
0,我会一直视奸你,永远..永远…..,screen,typeing,,
1 index content type animation character event
2 0 我会一直视奸你,永远..永远….. screen typeing

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f7d9a486e022f8d42b7c0af9a617cde7
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

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

View File

@@ -1,5 +0,0 @@
public struct DialogData
{
public int index;
public string content;
}

View File

@@ -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> _dialog = new();
private static DialogManager _instance;
public static DialogManager Instance
{
get
{
if (_instance)
return _instance;
_instance = FindObjectOfType<DialogManager>() ??
new GameObject("DialogData").AddComponent<DialogManager>();
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;
}
}
}

View File

@@ -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)
{

View File

@@ -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<int, string> _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<Dictionary<int, string>>(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;