Add support for images in dialogue windows (#19)

- Extend editor nodes with custom DialogueContent data type that holds either image or text
- Extend the dialogue importer to correctly process the new content into updated RuntimeDialogue content
- Update SpeechBubble to be able to display either text or image
- Add a custom property drawer for the DialogueContent to allow easy switching in graph authoring

Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com>
Reviewed-on: #19
This commit is contained in:
2025-10-08 09:34:58 +00:00
parent 7b949c5cb8
commit 3807ac652c
19 changed files with 1128 additions and 461 deletions

View File

@@ -74,19 +74,96 @@ namespace Dialogue
private void OnCharacterArrived()
{
if (speechBubble == null || ! HasAnyLines()) return;
if (speechBubble == null || !HasAnyLines()) return;
// Advance the dialogue state to move to the next content
AdvanceDialogueState();
// Get the current dialogue line
string line = GetCurrentDialogueLine();
// Check if we have DialogueContent available (prioritizing the new content system)
DialogueContent content = GetCurrentDialogueContent();
// Display the line with the new method that handles timed updates
speechBubble.DisplayDialogueLine(line, HasAnyLines());
// Advance dialogue state for next interaction
if (content != null)
{
// Display the content with the method that handles both text and images
// and pass whether there are more lines available for prompt display
speechBubble.DisplayDialogueContent(content, HasAnyLines());
// Log the content type for debugging
Debug.Log($"Displaying content type: {content.ContentType} - {(content.ContentType == DialogueContentType.Text ? content.Text : content.Image?.name)}");
}
else
{
// Fall back to legacy text-only method if no DialogueContent is available
string line = GetCurrentDialogueLine();
speechBubble.DisplayDialogueLine(line, HasAnyLines());
// Log for debugging
Debug.Log($"Displaying legacy text: {line}");
}
}
/// <summary>
/// Get the current dialogue content (text or image)
/// </summary>
/// <returns>DialogueContent or null if only legacy text content is available</returns>
private DialogueContent GetCurrentDialogueContent()
{
// Initialize if needed
if (!initialized)
{
StartDialogue();
}
if (!IsActive || IsCompleted || currentNode == null)
return null;
// Check if we have DialogueContent available
if (currentNode.dialogueContent != null && currentNode.dialogueContent.Count > 0)
{
// For WaitOnSlot nodes, use the appropriate content based on slot state
if (currentNode.nodeType == RuntimeDialogueNodeType.WaitOnSlot)
{
// Choose the appropriate content collection based on the current slot state
List<DialogueContent> contentForState = currentNode.dialogueContent; // Default content
switch (_currentSlotState)
{
case ItemSlotState.Incorrect:
// Use incorrect item content if available
if (currentNode.incorrectItemContent != null && currentNode.incorrectItemContent.Count > 0)
contentForState = currentNode.incorrectItemContent;
break;
case ItemSlotState.Forbidden:
// Use forbidden item content if available
if (currentNode.forbiddenItemContent != null && currentNode.forbiddenItemContent.Count > 0)
contentForState = currentNode.forbiddenItemContent;
break;
}
// If we have content for this state, return the current one
if (contentForState != null && contentForState.Count > 0)
{
// Make sure index is within bounds
int index = Mathf.Clamp(currentLineIndex, 0, contentForState.Count - 1);
return contentForState[index];
}
return null; // No content for this slot state
}
else
{
// For other node types, use the default dialogueContent
if (currentLineIndex >= 0 && currentLineIndex < currentNode.dialogueContent.Count)
{
return currentNode.dialogueContent[currentLineIndex];
}
}
}
// No DialogueContent available, will fall back to legacy text handling
return null;
}
private void OnDestroy()
{
// Unregister from events
@@ -205,7 +282,7 @@ namespace Dialogue
{
if (!IsActive || IsCompleted || currentNode == null)
return;
// If the condition was satisfied earlier, move to the next node immediately
if (_conditionSatisfiedPendingAdvance)
{
@@ -213,21 +290,44 @@ namespace Dialogue
MoveToNextNode();
return;
}
// First check if we have any dialogueContent to process
bool hasDialogueContent = currentNode.dialogueContent != null && currentNode.dialogueContent.Count > 0;
// If we have more lines in the current node, advance to the next line
if (currentLineIndex < currentNode.dialogueLines.Count - 1)
if (hasDialogueContent)
{
currentLineIndex++;
return;
// If we have dialogueContent and there are more entries, advance to the next one
if (currentLineIndex < currentNode.dialogueContent.Count - 1)
{
currentLineIndex++;
return;
}
// If we should loop through content, reset the index
if (currentNode.loopThroughLines && currentNode.dialogueContent.Count > 0)
{
currentLineIndex = 0;
return;
}
}
// If we should loop through lines, reset the index
if (currentNode.loopThroughLines && currentNode.dialogueLines.Count > 0)
else
{
currentLineIndex = 0;
return;
// Fall back to legacy dialogueLines
// If we have more lines in the current node, advance to the next line
if (currentLineIndex < currentNode.dialogueLines.Count - 1)
{
currentLineIndex++;
return;
}
// If we should loop through lines, reset the index
if (currentNode.loopThroughLines && currentNode.dialogueLines.Count > 0)
{
currentLineIndex = 0;
return;
}
}
// If we're at a node that doesn't have a next node, we're done
if (string.IsNullOrEmpty(currentNode.nextNodeID))
{
@@ -235,7 +335,7 @@ namespace Dialogue
IsCompleted = true;
return;
}
// Move to the next node only if no conditions to wait for
if (!IsWaitingForCondition())
{
@@ -560,60 +660,110 @@ namespace Dialogue
// Special case: if condition has been satisfied but not yet advanced, we should show lines
if (_conditionSatisfiedPendingAdvance && !string.IsNullOrEmpty(currentNode.nextNodeID))
{
// Check if the next node would have lines
// Check if the next node would have lines or content
RuntimeDialogueNode nextNode = dialogueGraph.GetNodeByID(currentNode.nextNodeID);
return nextNode != null && (nextNode.dialogueLines.Count > 0 || nextNode.nodeType != RuntimeDialogueNodeType.End);
return nextNode != null &&
((nextNode.dialogueLines != null && nextNode.dialogueLines.Count > 0) ||
(nextNode.dialogueContent != null && nextNode.dialogueContent.Count > 0) ||
nextNode.nodeType != RuntimeDialogueNodeType.End);
}
// For WaitOnSlot nodes, check for lines based on current slot state
// For WaitOnSlot nodes, check for lines or content based on current slot state
if (currentNode.nodeType == RuntimeDialogueNodeType.WaitOnSlot)
{
// Choose the appropriate line collection based on the current slot state
List<string> linesForState = currentNode.dialogueLines; // Default lines
// First check for DialogueContent
if (currentNode.dialogueContent != null && currentNode.dialogueContent.Count > 0)
{
// Choose the appropriate content collection based on the current slot state
List<DialogueContent> contentForState = currentNode.dialogueContent;
switch (_currentSlotState)
{
case ItemSlotState.Incorrect:
if (currentNode.incorrectItemContent != null && currentNode.incorrectItemContent.Count > 0)
contentForState = currentNode.incorrectItemContent;
break;
case ItemSlotState.Forbidden:
if (currentNode.forbiddenItemContent != null && currentNode.forbiddenItemContent.Count > 0)
contentForState = currentNode.forbiddenItemContent;
break;
}
if (contentForState.Count > 0)
{
if (currentLineIndex < contentForState.Count - 1 || currentNode.loopThroughLines)
{
return true;
}
}
}
// Fall back to legacy text lines
List<string> linesForState = currentNode.dialogueLines;
switch (_currentSlotState)
{
case ItemSlotState.Incorrect:
// Use incorrect item lines if available, otherwise fall back to default lines
if (currentNode.incorrectItemLines != null && currentNode.incorrectItemLines.Count > 0)
linesForState = currentNode.incorrectItemLines;
break;
case ItemSlotState.Forbidden:
// Use forbidden item lines if available, otherwise fall back to default lines
if (currentNode.forbiddenItemLines != null && currentNode.forbiddenItemLines.Count > 0)
linesForState = currentNode.forbiddenItemLines;
break;
}
// Check if we have any lines for the current state
if (linesForState != null && linesForState.Count > 0)
{
// If we're not at the end of the lines or we loop through them
if (currentLineIndex < linesForState.Count - 1 || currentNode.loopThroughLines)
{
return true;
}
}
}
// For other node types, use the standard check
else if (currentNode.dialogueLines.Count > 0)
// For other node types, check for DialogueContent first, then fall back to legacy text
else
{
// If we're not at the end of the lines or we loop through them
if (currentLineIndex < currentNode.dialogueLines.Count - 1 || currentNode.loopThroughLines)
// Check for DialogueContent
if (currentNode.dialogueContent != null && currentNode.dialogueContent.Count > 0)
{
return true;
if (currentLineIndex < currentNode.dialogueContent.Count - 1 || currentNode.loopThroughLines)
{
return true;
}
// If we're at the end of content but not waiting for a condition and have a next node
if (!IsWaitingForCondition() && !string.IsNullOrEmpty(currentNode.nextNodeID))
{
RuntimeDialogueNode nextNode = dialogueGraph.GetNodeByID(currentNode.nextNodeID);
return nextNode != null &&
((nextNode.dialogueContent != null && nextNode.dialogueContent.Count > 0) ||
(nextNode.dialogueLines != null && nextNode.dialogueLines.Count > 0) ||
nextNode.nodeType != RuntimeDialogueNodeType.End);
}
}
// If we're at the end of lines but not waiting for a condition and have a next node
if (!IsWaitingForCondition() && !string.IsNullOrEmpty(currentNode.nextNodeID))
// Fall back to legacy text lines
if (currentNode.dialogueLines != null && currentNode.dialogueLines.Count > 0)
{
// We need to check if the next node would have lines
RuntimeDialogueNode nextNode = dialogueGraph.GetNodeByID(currentNode.nextNodeID);
return nextNode != null && (nextNode.dialogueLines.Count > 0 || nextNode.nodeType != RuntimeDialogueNodeType.End);
if (currentLineIndex < currentNode.dialogueLines.Count - 1 || currentNode.loopThroughLines)
{
return true;
}
// If we're at the end of lines but not waiting for a condition and have a next node
if (!IsWaitingForCondition() && !string.IsNullOrEmpty(currentNode.nextNodeID))
{
RuntimeDialogueNode nextNode = dialogueGraph.GetNodeByID(currentNode.nextNodeID);
return nextNode != null &&
((nextNode.dialogueContent != null && nextNode.dialogueContent.Count > 0) ||
(nextNode.dialogueLines != null && nextNode.dialogueLines.Count > 0) ||
nextNode.nodeType != RuntimeDialogueNodeType.End);
}
}
}
return false;
}

View File

@@ -0,0 +1,76 @@
using System;
using UnityEngine;
namespace Dialogue
{
/// <summary>
/// Content type for dialogue entries
/// </summary>
public enum DialogueContentType
{
Text,
Image
}
/// <summary>
/// Wrapper class for dialogue content that can be either text or image
/// </summary>
[Serializable]
public class DialogueContent
{
[SerializeField] private DialogueContentType _contentType = DialogueContentType.Text;
[SerializeField] private string _text = string.Empty;
[SerializeField] private Sprite _image = null;
/// <summary>
/// The type of content this entry contains
/// </summary>
public DialogueContentType ContentType => _contentType;
/// <summary>
/// The text content (valid when ContentType is Text)
/// </summary>
public string Text => _text;
/// <summary>
/// The image content (valid when ContentType is Image)
/// </summary>
public Sprite Image => _image;
/// <summary>
/// Create text content
/// </summary>
/// <param name="text">The text to display</param>
public static DialogueContent CreateText(string text)
{
return new DialogueContent
{
_contentType = DialogueContentType.Text,
_text = text
};
}
/// <summary>
/// Create image content
/// </summary>
/// <param name="image">The image to display</param>
public static DialogueContent CreateImage(Sprite image)
{
return new DialogueContent
{
_contentType = DialogueContentType.Image,
_image = image
};
}
/// <summary>
/// Returns a string representation of this content
/// </summary>
public override string ToString()
{
return ContentType == DialogueContentType.Text
? $"Text: {_text}"
: $"Image: {_image?.name ?? "None"}";
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6b479dc736d44dea83d4d2cf4d940d8b
timeCreated: 1759912630

View File

@@ -36,10 +36,14 @@ namespace Dialogue
public RuntimeDialogueNodeType nodeType;
public string nextNodeID;
// Basic dialogue
// Basic dialogue - legacy text-only field
[HideInInspector]
public List<string> dialogueLines = new List<string>();
public bool loopThroughLines;
// New mixed content field that supports both text and images
public List<DialogueContent> dialogueContent = new List<DialogueContent>();
// Conditional nodes
public string puzzleStepID; // For WaitOnPuzzleStep
public string pickupItemID; // For WaitOnPickup
@@ -47,9 +51,14 @@ namespace Dialogue
public string combinationResultItemID; // For WaitOnCombination
// For WaitOnSlot - different responses
[HideInInspector]
public List<string> incorrectItemLines = new List<string>();
public bool loopThroughIncorrectLines;
public List<DialogueContent> incorrectItemContent = new List<DialogueContent>();
[HideInInspector]
public List<string> forbiddenItemLines = new List<string>();
public bool loopThroughForbiddenLines;
public List<DialogueContent> forbiddenItemContent = new List<DialogueContent>();
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Dialogue
{
@@ -18,6 +19,7 @@ namespace Dialogue
public class SpeechBubble : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI textDisplay;
[SerializeField] private Image imageDisplay; // New field for displaying images
[SerializeField] private TextDisplayMode displayMode = TextDisplayMode.Typewriter;
[SerializeField] private float typewriterSpeed = 0.05f; // Time between characters in seconds
[SerializeField] private AudioSource typingSoundSource;
@@ -29,11 +31,18 @@ namespace Dialogue
private Coroutine typewriterCoroutine;
private Coroutine promptUpdateCoroutine;
private string currentFullText = string.Empty;
private Sprite currentImage = null;
private bool isVisible = false;
private DialogueContentType currentContentType = DialogueContentType.Text;
private void Awake()
{
// Ensure we have both components
if (textDisplay == null)
Debug.LogError("SpeechBubble: TextMeshProUGUI component is not assigned!");
if (imageDisplay == null)
Debug.LogError("SpeechBubble: Image component is not assigned!");
}
/// <summary>
@@ -92,6 +101,7 @@ namespace Dialogue
}
currentFullText = text;
currentContentType = DialogueContentType.Text;
// Stop any existing typewriter effect
if (typewriterCoroutine != null)
@@ -100,6 +110,13 @@ namespace Dialogue
typewriterCoroutine = null;
}
// Activate text display, deactivate image display
textDisplay.gameObject.SetActive(true);
if (imageDisplay != null)
{
imageDisplay.gameObject.SetActive(false);
}
// Display text based on the selected mode
if (displayMode == TextDisplayMode.Instant)
{
@@ -259,5 +276,117 @@ namespace Dialogue
typewriterCoroutine = null;
}
/// <summary>
/// Set the image to display in the speech bubble
/// </summary>
/// <param name="sprite">Sprite to display</param>
public void SetImage(Sprite sprite)
{
if (imageDisplay == null)
{
Debug.LogError("SpeechBubble: Image component is not assigned!");
return;
}
currentImage = sprite;
currentContentType = DialogueContentType.Image;
// Activate image display, set the sprite
imageDisplay.gameObject.SetActive(true);
imageDisplay.sprite = sprite;
// Deactivate text display
if (textDisplay != null)
{
textDisplay.gameObject.SetActive(false);
}
// Make sure the bubble is visible when setting image
if (!isVisible)
Show();
}
/// <summary>
/// Clear the displayed image
/// </summary>
public void ClearImage()
{
SetImage(null);
}
/// <summary>
/// Set the content of the speech bubble (text or image)
/// </summary>
/// <param name="text">Text content</param>
/// <param name="image">Image content</param>
public void SetContent(string text, Sprite image)
{
if (!string.IsNullOrEmpty(text))
{
currentContentType = DialogueContentType.Text;
SetText(text);
}
else if (image != null)
{
currentContentType = DialogueContentType.Image;
SetImage(image);
}
}
/// <summary>
/// Get the current content type of the speech bubble
/// </summary>
/// <returns>Current content type</returns>
public DialogueContentType GetCurrentContentType()
{
return currentContentType;
}
/// <summary>
/// Display dialogue content (text or image)
/// </summary>
/// <param name="content">The dialogue content to display</param>
/// <param name="hasMoreDialogue">Whether there are more dialogue content items available</param>
public void DisplayDialogueContent(DialogueContent content, bool hasMoreDialogue)
{
// Cancel any existing prompt update
if (promptUpdateCoroutine != null)
{
StopCoroutine(promptUpdateCoroutine);
promptUpdateCoroutine = null;
}
if (content == null)
{
UpdatePromptVisibility(hasMoreDialogue);
return;
}
// Display the content based on its type
currentContentType = content.ContentType;
if (content.ContentType == DialogueContentType.Text)
{
// Show text display, hide image display
textDisplay.gameObject.SetActive(true);
if (imageDisplay != null) imageDisplay.gameObject.SetActive(false);
// Display the text
DisplayDialogueLine(content.Text, hasMoreDialogue);
}
else // Image content
{
// Hide text display, show image display
textDisplay.gameObject.SetActive(false);
if (imageDisplay != null) imageDisplay.gameObject.SetActive(true);
// Set the image
SetImage(content.Image);
// After a delay, update the prompt visibility
promptUpdateCoroutine = StartCoroutine(UpdatePromptAfterDelay(hasMoreDialogue));
}
}
}
}

View File

@@ -1 +0,0 @@


View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 877344c7a0014922bc3a2a469e03792d
timeCreated: 1759050622

View File

@@ -7,10 +7,34 @@ namespace UI
{
public class PauseMenu : MonoBehaviour
{
private static PauseMenu _instance;
private static bool _isQuitting;
public static PauseMenu Instance
{
get
{
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<PauseMenu>();
if (_instance == null)
{
var go = new GameObject("PauseMenu");
_instance = go.AddComponent<PauseMenu>();
// DontDestroyOnLoad(go);
}
}
return _instance;
}
}
[Header("UI References")]
[SerializeField] private GameObject pauseMenuPanel;
[SerializeField] private GameObject pauseButton;
public event Action OnGamePaused;
public event Action OnGameResumed;
private void Start()
{
// Subscribe to scene loaded events
@@ -32,6 +56,11 @@ namespace UI
}
}
void OnApplicationQuit()
{
_isQuitting = true;
}
/// <summary>
/// Sets the pause menu game object active or inactive based on the current level
/// </summary>