Add prompt when dialogue

This commit is contained in:
Michal Pikulski
2025-10-17 15:35:25 +02:00
parent c3c9fd95a4
commit 0557ddd3dc
2 changed files with 127 additions and 13 deletions

View File

@@ -114,13 +114,14 @@ MonoBehaviour:
m_EditorClassIdentifier: AppleHillsScripts::Dialogue.SpeechBubble
textDisplay: {fileID: 4573570654593171780}
imageDisplay: {fileID: 4814676392695871198}
dialoguePromptImage: {fileID: 6481613319593204747}
dialogueBubble: {fileID: 1494212192306772670}
displayMode: 1
typewriterSpeed: 0.05
typingSoundSource: {fileID: 0}
typingSoundFrequency: 3
useRichText: 1
dialogueDisplayTime: 3
dialoguePromptText: . . .
--- !u!1 &3571537114331005905
GameObject:
m_ObjectHideFlags: 0
@@ -353,6 +354,81 @@ MonoBehaviour:
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!1 &5966238993889414591
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2913690643654480743}
- component: {fileID: 5237027710007059819}
- component: {fileID: 6481613319593204747}
m_Layer: 5
m_Name: Prompt
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &2913690643654480743
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5966238993889414591}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 3484825090253933040}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 143, y: 74}
m_SizeDelta: {x: 245, y: 148}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &5237027710007059819
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5966238993889414591}
m_CullTransparentMesh: 1
--- !u!114 &6481613319593204747
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5966238993889414591}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier: UnityEngine.UI::UnityEngine.UI.Image
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: -1369078500869033561, guid: d52355bd23697ac4eaad116fb0aec19d, type: 3}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &6499933157207406972
GameObject:
m_ObjectHideFlags: 0
@@ -385,6 +461,7 @@ RectTransform:
m_ConstrainProportionsScale: 1
m_Children:
- {fileID: 8307219291215824345}
- {fileID: 2913690643654480743}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}

View File

@@ -19,14 +19,15 @@ namespace Dialogue
public class SpeechBubble : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI textDisplay;
[SerializeField] private Image imageDisplay; // New field for displaying images
[SerializeField] private Image imageDisplay; // For displaying images in dialogue
[SerializeField] private Image dialoguePromptImage; // NEW: Reference to the dialogue prompt image
[SerializeField] private GameObject dialogueBubble; // NEW: Reference to the dialogue bubble container
[SerializeField] private TextDisplayMode displayMode = TextDisplayMode.Typewriter;
[SerializeField] private float typewriterSpeed = 0.05f; // Time between characters in seconds
[SerializeField] private AudioSource typingSoundSource;
[SerializeField] private float typingSoundFrequency = 3; // Play sound every X characters
[SerializeField] private bool useRichText = true; // Whether to respect rich text tags
[SerializeField] private float dialogueDisplayTime = 1.5f; // Time in seconds to display dialogue before showing prompt
[SerializeField] private string dialoguePromptText = ". . ."; // Text to show as a prompt for available dialogue
private Coroutine typewriterCoroutine;
private Coroutine promptUpdateCoroutine;
@@ -34,15 +35,22 @@ namespace Dialogue
private Sprite currentImage = null;
private bool isVisible = false;
private DialogueContentType currentContentType = DialogueContentType.Text;
private bool isPromptVisible = false; // Track if we're showing the prompt or dialogue
private void Awake()
{
// Ensure we have both components
// Ensure we have the required components
if (textDisplay == null)
Debug.LogError("SpeechBubble: TextMeshProUGUI component is not assigned!");
if (imageDisplay == null)
Debug.LogError("SpeechBubble: Image component is not assigned!");
Debug.LogError("SpeechBubble: Image component for dialogue is not assigned!");
if (dialoguePromptImage == null)
Debug.LogError("SpeechBubble: Dialogue prompt image is not assigned!");
if (dialogueBubble == null)
Debug.LogError("SpeechBubble: Dialogue bubble container is not assigned!");
}
/// <summary>
@@ -50,16 +58,28 @@ namespace Dialogue
/// </summary>
public void Show()
{
gameObject.SetActive(true);
// If we're showing the prompt, we only activate the prompt image
if (isPromptVisible)
{
dialogueBubble.SetActive(false);
dialoguePromptImage.gameObject.SetActive(true);
}
else // Otherwise, show the dialogue bubble
{
dialogueBubble.SetActive(true);
dialoguePromptImage.gameObject.SetActive(false);
}
isVisible = true;
}
/// <summary>
/// Hide the speech bubble
/// Hide the speech bubble and prompt
/// </summary>
public void Hide()
{
gameObject.SetActive(false);
dialogueBubble.SetActive(false);
dialoguePromptImage.gameObject.SetActive(false);
isVisible = false;
// Stop any ongoing typewriter effect
@@ -102,6 +122,7 @@ namespace Dialogue
currentFullText = text;
currentContentType = DialogueContentType.Text;
isPromptVisible = false; // We're showing dialogue, not a prompt
// Stop any existing typewriter effect
if (typewriterCoroutine != null)
@@ -110,7 +131,7 @@ namespace Dialogue
typewriterCoroutine = null;
}
// Activate text display, deactivate image display
// Activate text display, deactivate image display within the dialogue bubble
textDisplay.gameObject.SetActive(true);
if (imageDisplay != null)
{
@@ -150,6 +171,7 @@ namespace Dialogue
// Display the dialogue line
if (!string.IsNullOrEmpty(line))
{
isPromptVisible = false; // We're showing dialogue content
SetText(line);
// After a delay, update the prompt visibility
@@ -163,19 +185,24 @@ namespace Dialogue
}
/// <summary>
/// Update the speech bubble to either show a prompt or hide based on dialogue availability
/// Update to either show the dialogue prompt image or hide based on dialogue availability
/// </summary>
/// <param name="hasDialogueAvailable">Whether dialogue is available</param>
public void UpdatePromptVisibility(bool hasDialogueAvailable)
{
if (hasDialogueAvailable)
{
Show();
SetText(dialoguePromptText);
isPromptVisible = true; // We're showing the prompt, not dialogue
// Hide dialogue bubble, show prompt image
dialogueBubble.SetActive(false);
dialoguePromptImage.gameObject.SetActive(true);
isVisible = true;
}
else
{
Hide();
Hide(); // Hide both bubble and prompt
}
}
@@ -291,6 +318,7 @@ namespace Dialogue
currentImage = sprite;
currentContentType = DialogueContentType.Image;
isPromptVisible = false; // We're showing dialogue content, not a prompt
// Activate image display, set the sprite
imageDisplay.gameObject.SetActive(true);
@@ -364,6 +392,7 @@ namespace Dialogue
}
// Display the content based on its type
isPromptVisible = false; // We're showing dialogue content
currentContentType = content.ContentType;
if (content.ContentType == DialogueContentType.Text)
@@ -372,6 +401,10 @@ namespace Dialogue
textDisplay.gameObject.SetActive(true);
if (imageDisplay != null) imageDisplay.gameObject.SetActive(false);
// Show dialogue bubble, hide prompt
dialogueBubble.SetActive(true);
dialoguePromptImage.gameObject.SetActive(false);
// Display the text
DisplayDialogueLine(content.Text, hasMoreDialogue);
}
@@ -381,6 +414,10 @@ namespace Dialogue
textDisplay.gameObject.SetActive(false);
if (imageDisplay != null) imageDisplay.gameObject.SetActive(true);
// Show dialogue bubble, hide prompt
dialogueBubble.SetActive(true);
dialoguePromptImage.gameObject.SetActive(false);
// Set the image
SetImage(content.Image);