[HOWTO] How To: Pause & Show Cursor During Conversations Without DialogueSystemTrigger

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 22148
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Pause & Show Cursor During Conversations Without DialogueSystemTrigger

Post by Tony Li »

The Dialogue System Trigger component's Start Conversation action provides some useful checkboxes such as Pause Game During Conversation and Show Cursor During Conversation.

If you're starting a conversation with the player without using a Dialogue System Trigger, the player will still receive "OnConversationStart" and "OnConversationEnd" events, so you can add a Dialogue System Events component or a script to the player, like this one:

ConversationGameControl.cs

Code: Select all

using UnityEngine;

public class ConversationGameControl : MonoBehaviour
{
    public bool pauseGameDuringConversation = true;
    public bool showCursorDuringConversation = true;

    private float previousTimeScale;
    private bool previousCursorVisible;

    void OnConversationStart(Transform actor)
    {
        if (pauseGameDuringConversation)
        {
            previousTimeScale = Time.timeScale;
            Time.timeScale = 0;
        }
        if (showCursorDuringConversation)
        {
            previousCursorVisible = Cursor.visible;
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }
    }

    void OnConversationEnd(Transform actor)
    {
        if (pauseGameDuringConversation)
        {
            Time.timeScale = previousTimeScale;
        }
        if (showCursorDuringConversation)
        {
            Cursor.visible = previousCursorVisible;
            Cursor.lockState = previousCursorVisible ? CursorLockMode.None : CursorLockMode.Locked;
        }
    }
}
Post Reply