You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中如何通过OnTriggerEnter控制对话暂停与激活指定对话行

实现对话暂停与触发区激活后续对话及选项功能

一、核心实现方案

我们通过新增状态控制变量、扩展触发事件逻辑、添加选项UI交互来满足需求:

  • 对话播放到第2行后自动暂停,等待玩家进入触发区
  • 玩家进入触发区时激活对话继续权限,并弹出对话选项
  • 玩家选择选项后直接跳转至第3行继续对话

二、修改后的完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Dialogue : MonoBehaviour
{
    public TextMeshProUGUI textComponent;
    public string[] lines;
    public float textSpeed;
    // 对话选项面板(需在Inspector中赋值)
    public GameObject dialogueOptionsPanel;
    // 选项文本(可按需扩展数量)
    public TextMeshProUGUI option1Text;

    private int index;
    // 控制对话是否可继续的开关
    private bool canContinueDialogue = false;

    void Start()
    {
        textComponent.text = string.Empty;
        dialogueOptionsPanel.SetActive(false); // 初始隐藏选项面板
        startDia();
    }

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            if (textComponent.text == lines[index])
            {
                NextLine();
            }
            else
            {
                StopAllCoroutines();
                textComponent.text = lines[index];
            }
        }
    }

    void startDia()
    {
        index = 0;
        StartCoroutine(TypeLine());
    }

    IEnumerator TypeLine()
    {
        foreach (char c in lines[index].ToCharArray())
        {
            textComponent.text += c;
            yield return new WaitForSeconds(textSpeed);
        }

        // 第2行播放完成后暂停对话(index从0开始,第2行对应index=1)
        if (index == 1)
        {
            canContinueDialogue = false;
        }
    }

    void NextLine()
    {
        if (index < lines.Length - 1)
        {
            // 第2行未获得继续权限时,不推进对话
            if (index == 1 && !canContinueDialogue)
            {
                return;
            }
            
            index++;
            textComponent.text = string.Empty;
            StartCoroutine(TypeLine());
        }
        else
        {
            gameObject.SetActive(false);
        }
    }

    // 触发区域检测逻辑
    private void OnTriggerEnter(Collider other)
    {
        // 检测进入对象是否为玩家(需确保玩家标签设为"Player")
        if (other.CompareTag("Player"))
        {
            canContinueDialogue = true;
            ShowDialogueOptions();
        }
    }

    // 显示对话选项
    void ShowDialogueOptions()
    {
        dialogueOptionsPanel.SetActive(true);
        // 设置选项内容,示例为继续对话的选项
        option1Text.text = "查看后续内容";
    }

    // 选项按钮点击事件(需绑定到按钮的OnClick事件)
    public void OnOption1Clicked()
    {
        dialogueOptionsPanel.SetActive(false);
        // 直接跳转到第3行开始播放
        index = 2;
        textComponent.text = string.Empty;
        StartCoroutine(TypeLine());
    }
}

三、配置步骤

  1. 设置触发区域

    • 创建空对象作为触发区,添加BoxCollider组件并勾选Is Trigger
    • 确保玩家对象带有Rigidbody组件(否则触发事件不生效),且玩家标签设为Player
  2. 配置对话选项UI

    • 创建Panel作为选项面板,添加Button和TextMeshProUGUI组件
    • 将选项面板和选项文本拖入Dialogue脚本的对应字段
    • 给选项按钮绑定OnOption1Clicked方法
  3. 逻辑流程说明

    • 对话启动后自动播放第1、2行,第2行结束后暂停
    • 玩家进入触发区后,触发事件激活继续权限并显示选项
    • 玩家点击选项后,直接跳转至第3行继续后续对话

内容的提问来源于stack exchange,提问作者D4RKTORNADO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 12:32:25