+事件连琐
+对话框分化完毕 +打包Resources文件夹
This commit is contained in:
@@ -1,7 +1,97 @@
|
||||
namespace Dialog
|
||||
using System.Collections;
|
||||
using Event;
|
||||
using Event.EventArgs;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Dialog
|
||||
{
|
||||
public class BoxDialog
|
||||
public class BoxDialog : MonoBehaviour
|
||||
{
|
||||
|
||||
public float typingSpeed = 0.05f;
|
||||
public string introduceText;
|
||||
private string _currentText = "";
|
||||
private float _timer;
|
||||
private int _currentIndex;
|
||||
private bool _start;
|
||||
public TMP_Text textMeshPro;
|
||||
public float printTime;
|
||||
public float maxPrintTime = 10;
|
||||
public bool isTime2Break;
|
||||
private string _eventToBeExc;
|
||||
private string _eventArg;
|
||||
[SerializeField] private GameObject dialogBox;
|
||||
[SerializeField] private RawImage head;
|
||||
[SerializeField] private RawImage face;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_start) return;
|
||||
printTime += Time.deltaTime;
|
||||
if (printTime > maxPrintTime)
|
||||
{
|
||||
isTime2Break = true;
|
||||
StartCoroutine(Delay());
|
||||
}
|
||||
|
||||
Time2Break();
|
||||
if (_currentText == introduceText)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_timer += Time.deltaTime;
|
||||
if (!(_timer >= typingSpeed)) return;
|
||||
_timer = 0f;
|
||||
_currentText += introduceText[_currentIndex];
|
||||
textMeshPro.text = _currentText;
|
||||
_currentIndex++;
|
||||
}
|
||||
|
||||
// 沟槽的生命周期
|
||||
private IEnumerator Delay()
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
EventManager.Instance.DialogEventSwitch(_eventToBeExc, _eventArg);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
EventManager.Instance.DialogPop += StartPrinting;
|
||||
}
|
||||
|
||||
private void Time2Break()
|
||||
{
|
||||
if (!isTime2Break) return;
|
||||
_start = false;
|
||||
printTime = 0;
|
||||
isTime2Break = false;
|
||||
textMeshPro.text = "";
|
||||
dialogBox.SetActive(false);
|
||||
}
|
||||
|
||||
private void StartPrinting(DialogPopArgs e)
|
||||
{
|
||||
if (!DialogManager.Instance.GetDialogByIndex(e.Index).Type.Equals("box")) return;
|
||||
if (printTime != 0) return;
|
||||
dialogBox.SetActive(true);
|
||||
_start = true;
|
||||
_currentText = "";
|
||||
introduceText = DialogManager.Instance.GetDialogByIndex(e.Index).Content;
|
||||
_eventToBeExc = DialogManager.Instance.GetDialogByIndex(e.Index).DialogEvent;
|
||||
_eventArg = DialogManager.Instance.GetDialogByIndex(e.Index).DialogEventArg;
|
||||
|
||||
head.texture = Resources.Load<Texture2D>("Character/Head" + "/" +
|
||||
DialogManager.Instance.GetDialogByIndex(e.Index).Character +
|
||||
"_Head");
|
||||
face.texture = Resources.Load<Texture2D>("Character/Face" + "/" +
|
||||
DialogManager.Instance.GetDialogByIndex(e.Index).Character + "_" +
|
||||
DialogManager.Instance.GetDialogByIndex(e.Index).Animation +
|
||||
"_Face");
|
||||
_currentIndex = 0;
|
||||
_timer = 0f;
|
||||
textMeshPro.fontSize = 80;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
namespace Dialog
|
||||
{
|
||||
public class Dialog
|
||||
{
|
||||
@@ -15,7 +14,8 @@ namespace Data
|
||||
public readonly string DialogEvent;
|
||||
public readonly string DialogEventArg;
|
||||
|
||||
public Dialog(int index, string content, string type, string animation, string character, string dialogEvent, string dialogEventArg)
|
||||
public Dialog(int index, string content, string type, string animation, string character, string dialogEvent,
|
||||
string dialogEventArg)
|
||||
{
|
||||
Index = index;
|
||||
Content = content;
|
||||
@@ -29,10 +29,9 @@ namespace Data
|
||||
|
||||
public class DialogManager : MonoBehaviour
|
||||
{
|
||||
public string filePath = "Assets/Dialog/DialogData.csv";
|
||||
private readonly List<Dialog> _dialog = new();
|
||||
|
||||
private readonly List<Dialog> _dialog = new();
|
||||
private static DialogManager _instance;
|
||||
|
||||
public static DialogManager Instance
|
||||
{
|
||||
get
|
||||
@@ -44,6 +43,7 @@ namespace Data
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_instance != null && _instance != this)
|
||||
@@ -52,18 +52,25 @@ namespace Data
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadCsv(filePath);
|
||||
LoadCsv("Dialog/DialogData");
|
||||
_instance = this;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadCsv(string path)
|
||||
|
||||
private void LoadCsv(string resourcePath)
|
||||
{
|
||||
var lines = File.ReadAllLines(path);
|
||||
var textAsset = Resources.Load<TextAsset>(resourcePath);
|
||||
if (textAsset == null)
|
||||
{
|
||||
Debug.LogError($"Unable to find CSV file at path: {resourcePath}");
|
||||
return;
|
||||
}
|
||||
|
||||
var lines = textAsset.text.Split('\n');
|
||||
for (var i = 1; i < lines.Length; i++)
|
||||
{
|
||||
var values = lines[i].Split(',');
|
||||
|
||||
if (values.Length < 7) continue;
|
||||
var dialog = new Dialog(
|
||||
int.Parse(values[0]),
|
||||
values[1],
|
||||
@@ -73,11 +80,11 @@ namespace Data
|
||||
values[5],
|
||||
values[6]
|
||||
);
|
||||
|
||||
|
||||
_dialog.Add(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Dialog GetDialogByIndex(int index)
|
||||
{
|
||||
foreach (var dialog in _dialog.Where(dialogue => dialogue.Index == index))
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
using System.Collections;
|
||||
using Dialog;
|
||||
using Event;
|
||||
using Event.EventArgs;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
namespace Dialog
|
||||
{
|
||||
public class ScreenDialog : MonoBehaviour
|
||||
{
|
||||
@@ -18,7 +17,7 @@ namespace UI
|
||||
private bool _isBlinking;
|
||||
public TMP_Text textMeshPro;
|
||||
public float printTime;
|
||||
public float maxPrintTime=10;
|
||||
public float maxPrintTime = 10;
|
||||
public bool isTime2Break;
|
||||
private string _eventToBeExc;
|
||||
private string _eventArg;
|
||||
@@ -32,10 +31,12 @@ namespace UI
|
||||
isTime2Break = true;
|
||||
StartCoroutine(Delay());
|
||||
}
|
||||
|
||||
if (_currentText == introduceText)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_timer += Time.deltaTime;
|
||||
if (!(_timer >= typingSpeed)) return;
|
||||
_timer = 0f;
|
||||
@@ -48,8 +49,9 @@ namespace UI
|
||||
private IEnumerator Delay()
|
||||
{
|
||||
yield return new WaitForSeconds(1f);
|
||||
EventManager.Instance.DialogEventSwitch(_eventToBeExc,_eventArg);
|
||||
EventManager.Instance.DialogEventSwitch(_eventToBeExc, _eventArg);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
EventManager.Instance.DialogPop += StartPrinting;
|
||||
@@ -68,6 +70,7 @@ namespace UI
|
||||
textMeshPro.text = "+";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!_isBlinking)
|
||||
{
|
||||
textMeshPro.text += "|";
|
||||
@@ -78,14 +81,15 @@ namespace UI
|
||||
textMeshPro.text = textMeshPro.text[..^1];
|
||||
_isBlinking = false;
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.3f);
|
||||
}
|
||||
}
|
||||
|
||||
private void StartPrinting(DialogPopArgs e)
|
||||
{
|
||||
if(!DialogManager.Instance.GetDialogByIndex(e.Index).Type.Equals("screen")) return;
|
||||
if(printTime != 0) return;
|
||||
if (!DialogManager.Instance.GetDialogByIndex(e.Index).Type.Equals("screen")) return;
|
||||
if (printTime != 0) return;
|
||||
_start = true;
|
||||
_currentText = "";
|
||||
introduceText = DialogManager.Instance.GetDialogByIndex(e.Index).Content;
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
public class DialogPopArgs : System.EventArgs
|
||||
{
|
||||
public int Index { get; private set; }
|
||||
public bool IsScreenDialog { get;private set; }
|
||||
|
||||
public DialogPopArgs(int index)
|
||||
{
|
||||
|
||||
@@ -43,6 +43,18 @@ namespace Event
|
||||
public event PlayerWalkStopHandler PlayerWalkStop;
|
||||
public event PlayerWalkingHandler PlayerWalking;
|
||||
|
||||
public void DialogEventSwitch(string eventName, string args)
|
||||
{
|
||||
switch (eventName)
|
||||
{
|
||||
case "OnDialogPop":
|
||||
OnDialogPop(int.Parse(args));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCameraInterAct(GameObject item)
|
||||
{
|
||||
CameraInterAct?.Invoke(new CameraInterActArgs(item));
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace Items.Abstract
|
||||
{
|
||||
public abstract class ItemBase : MonoBehaviour
|
||||
{
|
||||
public int index;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
EventManager.Instance.CameraInterAct += ReceiveEvent;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Items
|
||||
protected override void ActivateItem()
|
||||
{
|
||||
base.ActivateItem();
|
||||
EventManager.Instance.OnDialogPop(0);
|
||||
EventManager.Instance.OnDialogPop(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Assets/Scripts/UI/PauseButton.cs
Normal file
26
Assets/Scripts/UI/PauseButton.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class PauseButton : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject pauseMenu;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
Pause();
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
}
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
pauseMenu.SetActive(true);
|
||||
Time.timeScale = 0;
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/UI/PauseButton.cs.meta
Normal file
3
Assets/Scripts/UI/PauseButton.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c0f94546ebb4d1291e84e1867586885
|
||||
timeCreated: 1726632669
|
||||
41
Assets/Scripts/UI/PausePanel.cs
Normal file
41
Assets/Scripts/UI/PausePanel.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class PausePanel : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject pauseButton;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Time.timeScale = 1;
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
Continue();
|
||||
}
|
||||
}
|
||||
|
||||
public void Continue()
|
||||
{
|
||||
pauseButton.SetActive(true);
|
||||
Time.timeScale = 1;
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void Exit()
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
|
||||
public void MainMenu()
|
||||
{
|
||||
Time.timeScale = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/UI/PausePanel.cs.meta
Normal file
3
Assets/Scripts/UI/PausePanel.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a7e77e32f8249e59678cd60afb13a77
|
||||
timeCreated: 1726632456
|
||||
Reference in New Issue
Block a user