unity学习——显示窗口 Menu.SetActive(true);

博客内容提及将要把显示的窗口给予menu,涉及窗口显示相关信息技术操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 将要显示的窗口给予menu;

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

public class GameManager : MonoBehaviour {

    public GameObject Menu;
	// Use this for initialization

    public void OnSettingButton()
    {
        Menu.SetActive(true);
    }
}

 

using System.Collections.Generic; using UnityEngine; using TMPro; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class DialogueManager : MonoBehaviour { public enum DialogueState { Before, Win, Lost } public static DialogueManager Instance; [Header("UI Elements")] public TextMeshProUGUI tmpText; public float typingSpeed = 0.05f; [Header("Dialogue Content")] public List<string> beforeBattleDialogues = new List<string>(); public List<string> winDialogues = new List<string>(); public List<string> lostDialogues = new List<string>(); private bool isTyping = false; private Coroutine typingCoroutine; private string currentSentence; private DialogueState currentState = DialogueState.Before; private int currentDialogueIndex = 0; private List<string> currentDialogueList; [Header("按钮设置")] public Button victoryButton; // 胜利按钮 public Button retryButton; // 重新挑战按钮 public Button exitButton; public Button StartButton; void Start() { // 检查战斗结果 int result = PlayerPrefs.GetInt("LastBattleResult", -1); if (result == 1) { Debug.Log("战斗胜利 - 触发胜利对话"); SetDialogueState(DialogueState.Win); } else if (result == 0) { Debug.Log("战斗失败 - 触发失败对话"); SetDialogueState(DialogueState.Lost); } else { Debug.Log("首次进入 - 默认对话"); SetDialogueState(DialogueState.Before); } // 清除状态 PlayerPrefs.DeleteKey("LastBattleResult"); // 初始化隐藏所有按钮 StartButton.gameObject.SetActive(false); victoryButton.gameObject.SetActive(false); retryButton.gameObject.SetActive(false); exitButton.gameObject.SetActive(false); // 设置按钮点击事件 StartButton.onClick.AddListener(OnStartClick); victoryButton.onClick.AddListener(OnVictoryClick); retryButton.onClick.AddListener(OnRetryClick); exitButton.onClick.AddListener(OnExitClick); } void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); } // 初始化当前对话列表 currentDialogueList = beforeBattleDialogues; } void Update() { // 添加跳过功能:按E键跳过当前打字效果 if (Input.GetKeyDown(KeyCode.E) && isTyping) { SkipTyping(); } // 按E键继续下一句对话 else if (Input.GetKeyDown(KeyCode.E) && !isTyping && tmpText.gameObject.activeSelf) { NextSentence(); } } // 设置当前对话状态 public void SetDialogueState(DialogueState newState) { currentState = newState; currentDialogueIndex = 0; switch (newState) { case DialogueState.Before: currentDialogueList = beforeBattleDialogues; break; case DialogueState.Win: currentDialogueList = winDialogues; break; case DialogueState.Lost: currentDialogueList = lostDialogues; break; } Debug.Log($"状态已切换至: {newState}"); } // 启动当前状态的对话 public void StartStateDialogue() { if (currentDialogueList == null || currentDialogueList.Count == 0) return; // 如果正在显示其他文本,先停止 if (isTyping) { StopCoroutine(typingCoroutine); } // 确保索引在有效范围内 currentDialogueIndex = Mathf.Clamp(currentDialogueIndex, 0, currentDialogueList.Count - 1); currentSentence = currentDialogueList[currentDialogueIndex]; // 暂停玩家移动 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = false; // 锁定玩家输入 Cursor.lockState = CursorLockMode.None; Cursor.visible = true; // 显示对话框UI tmpText.gameObject.SetActive(true); // 启动打字效果协程 typingCoroutine = StartCoroutine(TypeSentence(currentSentence)); } // 显示下一句对话 public void NextSentence() { currentDialogueIndex++; if (currentDialogueIndex < currentDialogueList.Count) { StartStateDialogue(); } else { // 对话结束 EndDialogue(); } } IEnumerator TypeSentence(string sentence) { isTyping = true; tmpText.text = ""; // 逐字显示 foreach (char letter in sentence.ToCharArray()) { tmpText.text += letter; yield return new WaitForSeconds(typingSpeed); } isTyping = false; } // 跳过当前打字效果 public void SkipTyping() { if (isTyping) { StopCoroutine(typingCoroutine); tmpText.text = currentSentence; isTyping = false; } } public void EndDialogue() { if (isTyping) StopCoroutine(typingCoroutine); playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; tmpText.gameObject.SetActive(false); // 添加按钮激活日志 Debug.Log($"准备激活按钮 - 当前状态: {currentState}"); // 确保所有按钮先隐藏再按需显示 StartButton.gameObject.SetActive(false); victoryButton.gameObject.SetActive(false); retryButton.gameObject.SetActive(false); exitButton.gameObject.SetActive(false); switch (currentState) { case DialogueState.Before: StartButton.gameObject.SetActive(true); exitButton.gameObject.SetActive(true); Debug.Log("已激活: 开始按钮 + 退出按钮"); break; case DialogueState.Win: victoryButton.gameObject.SetActive(true); exitButton.gameObject.SetActive(true); Debug.Log("已激活: 胜利按钮 + 退出按钮"); break; case DialogueState.Lost: retryButton.gameObject.SetActive(true); exitButton.gameObject.SetActive(true); Debug.Log("已激活: 重试按钮 + 退出按钮"); break; } } private void OnStartClick() { StartButton.gameObject.SetActive(true); } private void OnVictoryClick() {//解除玩家限制,让玩家重新自由活动 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; victoryButton.gameObject.SetActive(false); } private void OnRetryClick() { // 重新挑战:重新加载当前战斗场景 SceneManager.LoadScene("Fight"); retryButton.gameObject.SetActive(false); } private void OnExitClick() { // 退出到主菜单 SceneManager.LoadScene("MainMenu"); exitButton.gameObject.SetActive(false); } } 你这代码中button根本没用,既不显示也不能使用
08-01
using System.Collections.Generic; using UnityEngine; using TMPro; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class DialogueManager : MonoBehaviour { public enum DialogueState { Before, Win, Lost } public static DialogueManager Instance; [Header("UI Elements")] public TextMeshProUGUI tmpText; public float typingSpeed = 0.05f; [Header("Dialogue Content")] public List<string> beforeBattleDialogues = new List<string>(); public List<string> winDialogues = new List<string>(); public List<string> lostDialogues = new List<string>(); private bool isTyping = false; private Coroutine typingCoroutine; private string currentSentence; private DialogueState currentState = DialogueState.Before; private int currentDialogueIndex = 0; private List<string> currentDialogueList; [Header("按钮设置")] public Button victoryButton; // 胜利按钮 public Button retryButton; // 重新挑战按钮 public Button exitButton; public Button StartButton; void Start() { // 检查战斗结果 int result = PlayerPrefs.GetInt("LastBattleResult", -1); if (result == 1) { Debug.Log("战斗胜利 - 触发胜利对话"); SetDialogueState(DialogueState.Win); } else if (result == 0) { Debug.Log("战斗失败 - 触发失败对话"); SetDialogueState(DialogueState.Lost); } else { Debug.Log("首次进入 - 默认对话"); SetDialogueState(DialogueState.Before); } // 清除状态 PlayerPrefs.DeleteKey("LastBattleResult"); // 初始化隐藏所有按钮 StartButton.gameObject.SetActive(false); victoryButton.gameObject.SetActive(false); retryButton.gameObject.SetActive(false); exitButton.gameObject.SetActive(false); // 设置按钮点击事件 StartButton.onClick.AddListener(OnStartClick); victoryButton.onClick.AddListener(OnVictoryClick); retryButton.onClick.AddListener(OnRetryClick); exitButton.onClick.AddListener(OnExitClick); } void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); } // 初始化当前对话列表 currentDialogueList = beforeBattleDialogues; } void Update() { // 添加跳过功能:按E键跳过当前打字效果 if (Input.GetKeyDown(KeyCode.E) && isTyping) { SkipTyping(); } // 按E键继续下一句对话 else if (Input.GetKeyDown(KeyCode.E) && !isTyping && tmpText.gameObject.activeSelf) { NextSentence(); } } // 设置当前对话状态 public void SetDialogueState(DialogueState newState) { currentState = newState; currentDialogueIndex = 0; switch (newState) { case DialogueState.Before: currentDialogueList = beforeBattleDialogues; break; case DialogueState.Win: currentDialogueList = winDialogues; break; case DialogueState.Lost: currentDialogueList = lostDialogues; break; } Debug.Log("已完成最新的更新"); } // 启动当前状态的对话 public void StartStateDialogue() { if (currentDialogueList == null || currentDialogueList.Count == 0) return; // 如果正在显示其他文本,先停止 if (isTyping) { StopCoroutine(typingCoroutine); } // 确保索引在有效范围内 currentDialogueIndex = Mathf.Clamp(currentDialogueIndex, 0, currentDialogueList.Count - 1); currentSentence = currentDialogueList[currentDialogueIndex]; // 暂停玩家移动 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = false; // 锁定玩家输入 Cursor.lockState = CursorLockMode.None; Cursor.visible = true; // 显示对话框UI tmpText.gameObject.SetActive(true); // 启动打字效果协程 typingCoroutine = StartCoroutine(TypeSentence(currentSentence)); } // 显示下一句对话 public void NextSentence() { currentDialogueIndex++; if (currentDialogueIndex < currentDialogueList.Count) { StartStateDialogue(); } else { // 对话结束 EndDialogue(); } } IEnumerator TypeSentence(string sentence) { isTyping = true; tmpText.text = ""; // 逐字显示 foreach (char letter in sentence.ToCharArray()) { tmpText.text += letter; yield return new WaitForSeconds(typingSpeed); } isTyping = false; } // 跳过当前打字效果 public void SkipTyping() { if (isTyping) { StopCoroutine(typingCoroutine); tmpText.text = currentSentence; isTyping = false; } } public void EndDialogue() { // 如果还在打字,先停止打字 if (isTyping) { StopCoroutine(typingCoroutine); } // 恢复玩家移动 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; // 恢复输入锁定 Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; tmpText.gameObject.SetActive(false); switch (currentState) { case DialogueState.Before: StartButton.gameObject.SetActive(true); exitButton.gameObject.SetActive(true); break; case DialogueState.Win: victoryButton.gameObject.SetActive(true); exitButton.gameObject.SetActive(true); break; case DialogueState.Lost: retryButton.gameObject.SetActive(true); exitButton.gameObject.SetActive(true); break; default: // 默认状态不显示按钮 break; } } private void OnStartClick() { } private void OnVictoryClick() {//解除玩家限制,让玩家重新自由活动 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; victoryButton.gameObject.SetActive(false); } private void OnRetryClick() { // 重新挑战:重新加载当前战斗场景 SceneManager.LoadScene("Fight"); retryButton.gameObject.SetActive(false); } private void OnExitClick() { // 退出到主菜单 SceneManager.LoadScene("MainMenu"); exitButton.gameObject.SetActive(false); } } 我上面的四种button在各个阶段都不会显现出来
08-01
using System.Collections.Generic; using UnityEngine; using TMPro; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; using UnityEngine.EventSystems; public class DialogueManager : MonoBehaviour { public enum DialogueState { Before, Win, Lost } public static DialogueManager Instance; [Header("UI Elements")] public TextMeshProUGUI tmpText; public float typingSpeed = 0.05f; [Header("Dialogue Content")] public List<string> beforeBattleDialogues = new List<string>(); public List<string> winDialogues = new List<string>(); public List<string> lostDialogues = new List<string>(); [Header("场景布置")] public GameObject image; public GameObject image1; public GameObject image2; public TextMeshProUGUI text; [Header("按钮设置")] public Button mainMenuButton; public Button startButton; public Button loseButton; public Button winButton; private bool isTyping = false; private Coroutine typingCoroutine; private string currentSentence; private DialogueState currentState = DialogueState.Before; private int currentDialogueIndex = 0; private List<string> currentDialogueList; void Start() { // 隐藏所有按钮 // 检查战斗结果 int result = PlayerPrefs.GetInt("LastBattleResult", -1); if (result == 1) { Debug.Log("战斗胜利 - 触发胜利对话"); SetDialogueState(DialogueState.Win); } else if (result == 0) { Debug.Log("战斗失败 - 触发失败对话"); SetDialogueState(DialogueState.Lost); } else { Debug.Log("首次进入 - 默认对话"); SetDialogueState(DialogueState.Before); } // 清除状态 PlayerPrefs.DeleteKey("LastBattleResult"); } void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); } // 初始化当前对话列表 currentDialogueList = beforeBattleDialogues; // 设置按钮点击事件 mainMenuButton?.onClick.RemoveAllListeners(); startButton?.onClick.RemoveAllListeners(); loseButton?.onClick.RemoveAllListeners(); winButton?.onClick.RemoveAllListeners(); // 重新绑定正确的事件 if (mainMenuButton != null) mainMenuButton.onClick.AddListener(OnMainMenuClick); if (startButton != null) startButton.onClick.AddListener(OnStartClick); if (loseButton != null) loseButton.onClick.AddListener(OnLoseClick); if (winButton != null) winButton.onClick.AddListener(OnWinClick); } void Update() { // 添加跳过功能:按E键跳过当前打字效果 if (Input.GetKeyDown(KeyCode.E) && isTyping) { SkipTyping(); } // 按E键继续下一句对话 else if (Input.GetKeyDown(KeyCode.E) && !isTyping && tmpText.gameObject.activeSelf) { NextSentence(); } } // 隐藏所有按钮 // 设置当前对话状态 public void SetDialogueState(DialogueState newState) { currentState = newState; currentDialogueIndex = 0; switch (newState) { case DialogueState.Before: currentDialogueList = beforeBattleDialogues; break; case DialogueState.Win: currentDialogueList = winDialogues; break; case DialogueState.Lost: currentDialogueList = lostDialogues; break; } Debug.Log($"状态已切换至: {newState}"); } // 启动当前状态的对话 public void StartStateDialogue() { if (currentDialogueList == null || currentDialogueList.Count == 0) return; // 如果正在显示其他文本,先停止 if (isTyping) { StopCoroutine(typingCoroutine); } // 确保索引在有效范围内 currentDialogueIndex = Mathf.Clamp(currentDialogueIndex, 0, currentDialogueList.Count - 1); currentSentence = currentDialogueList[currentDialogueIndex]; // 暂停玩家移动 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = false; // 锁定玩家输入 Cursor.lockState = CursorLockMode.None; Cursor.visible = true; // 显示对话框UI tmpText.gameObject.SetActive(true); // 启动打字效果协程 typingCoroutine = StartCoroutine(TypeSentence(currentSentence)); } // 显示下一句对话 public void NextSentence() { currentDialogueIndex++; if (currentDialogueIndex < currentDialogueList.Count) { StartStateDialogue(); } else { // 对话结束 EndDialogue(); } } IEnumerator TypeSentence(string sentence) { isTyping = true; tmpText.text = ""; // 逐字显示 foreach (char letter in sentence.ToCharArray()) { tmpText.text += letter; yield return new WaitForSeconds(typingSpeed); } isTyping = false; } // 跳过当前打字效果 public void SkipTyping() { if (isTyping) { StopCoroutine(typingCoroutine); tmpText.text = currentSentence; isTyping = false; } } public void EndDialogue() { if (isTyping) { StopCoroutine(typingCoroutine); isTyping = false; } // 恢复玩家控制 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; // 隐藏对话框 tmpText.gameObject.SetActive(false); // 根据状态显示对应按钮 switch (currentState) { case DialogueState.Before: if (startButton != null) startButton.gameObject.SetActive(true); if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(true); break; case DialogueState.Win: if (winButton != null) winButton.gameObject.SetActive(true); if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(true); break; case DialogueState.Lost: if (loseButton != null) loseButton.gameObject.SetActive(true); if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(true); break; } // 确保事件系统可用 EnsureEventSystemExists(); // 设置按钮为选中状态 if (currentState == DialogueState.Before && startButton != null) { EventSystem.current.SetSelectedGameObject(startButton.gameObject); } else if (currentState == DialogueState.Win && winButton != null) { EventSystem.current.SetSelectedGameObject(winButton.gameObject); } else if (currentState == DialogueState.Lost && loseButton != null) { EventSystem.current.SetSelectedGameObject(loseButton.gameObject); } } private void EnsureEventSystemExists() { EventSystem eventSystem = FindObjectOfType<EventSystem>(); if (eventSystem == null) { GameObject eventSystemObj = new GameObject("EventSystem"); eventSystem = eventSystemObj.AddComponent<EventSystem>(); eventSystemObj.AddComponent<StandaloneInputModule>(); } else if (eventSystem.GetComponent<StandaloneInputModule>() == null) { eventSystem.gameObject.AddComponent<StandaloneInputModule>(); } } private void OnMainMenuClick() { Debug.Log("主菜单按钮点击"); SceneManager.LoadScene("MainMenu"); } private void OnStartClick() { Debug.Log("开始作战"); SceneManager.LoadScene("Fight"); } private void OnLoseClick() { Debug.Log("失败按钮点击"); SceneManager.LoadScene("Fight"); } private void OnWinClick() { if (currentState != DialogueState.Win) { Debug.LogWarning("尝试在非胜利状态执行胜利逻辑"); return; } playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; Debug.Log("执行胜利逻辑"); image.SetActive(false); image1.SetActive(false); image2.SetActive(false); text.gameObject.SetActive(false); } }using TMPro; using UnityEngine; using UnityEngine.UI; public class NPC2 : MonoBehaviour { public GameObject image; public GameObject image1; public GameObject image2; public TextMeshProUGUI text; [Header("按钮设置")] public Button mainMenuButton; public Button startButton; public Button loseButton; public Button winButton; private bool isDialogueActive = false; void Start() { // 初始化隐藏所有按钮 SetAllButtonsActive(false); } void OnTriggerStay2D(Collider2D other) { if (other.CompareTag("Player") && Input.GetKeyDown(KeyCode.F) && !isDialogueActive) { // 显示对话UI元素 image.SetActive(true); image1.SetActive(true); image2.SetActive(true); text.gameObject.SetActive(true); // 激活开始按钮 if (startButton != null) { startButton.gameObject.SetActive(true); // 添加按钮点击事件 startButton.onClick.AddListener(OnStartButtonClicked); } // 隐藏其他按钮 SetOtherButtonsActive(false); isDialogueActive = true; } } void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Player")) { // 隐藏所有UI元素和按钮 SetAllButtonsActive(false); image.SetActive(false); image1.SetActive(false); image2.SetActive(false); text.gameObject.SetActive(false); isDialogueActive = false; } } // 按钮点击事件处理 private void OnStartButtonClicked() { // 启动对话 DialogueManager.Instance.StartStateDialogue(); // 显示其他按钮 SetOtherButtonsActive(true); } // 辅助方法:设置所有按钮状态 private void SetAllButtonsActive(bool state) { if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(state); if (startButton != null) startButton.gameObject.SetActive(state); if (loseButton != null) loseButton.gameObject.SetActive(state); if (winButton != null) winButton.gameObject.SetActive(state); } // 辅助方法:设置除开始按钮外的其他按钮状态 private void SetOtherButtonsActive(bool state) { if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(state); if (loseButton != null) loseButton.gameObject.SetActive(state); if (winButton != null) winButton.gameObject.SetActive(state); } } 为什么我的tmpText在触发器中不显示
最新发布
08-02
using TMPro; using UnityEngine; using UnityEngine.UI; public class NPC2 : MonoBehaviour { public GameObject image; public GameObject image1; public GameObject image2; public TextMeshProUGUI text; [Header("按钮设置")] public Button mainMenuButton; public Button startButton; public Button loseButton; public Button winButton; void Start() { // 将按钮引用传递给DialogueManager if (DialogueManager.Instance != null) { DialogueManager.Instance.mainMenuButton = mainMenuButton; DialogueManager.Instance.startButton = startButton; DialogueManager.Instance.loseButton = loseButton; DialogueManager.Instance.winButton = winButton; } // 初始化隐藏所有按钮 if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(false); if (startButton != null) startButton.gameObject.SetActive(false); if (loseButton != null) loseButton.gameObject.SetActive(false); if (winButton != null) winButton.gameObject.SetActive(false); } void OnTriggerStay2D(Collider2D other) { if (other.CompareTag("Player") && Input.GetKeyDown(KeyCode.F)) { image.SetActive(true); image1.SetActive(true); image2.SetActive(true); text.gameObject.SetActive(true); // 启动对话 DialogueManager.Instance.StartStateDialogue(); } } }using System.Collections.Generic; using UnityEngine; using TMPro; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; using UnityEngine.EventSystems; public class DialogueManager : MonoBehaviour { public enum DialogueState { Before, Win, Lost } public static DialogueManager Instance; [Header("UI Elements")] public TextMeshProUGUI tmpText; public float typingSpeed = 0.05f; [Header("Dialogue Content")] public List<string> beforeBattleDialogues = new List<string>(); public List<string> winDialogues = new List<string>(); public List<string> lostDialogues = new List<string>(); [Header("场景布置")] public GameObject image; public GameObject image1; public GameObject image2; public TextMeshProUGUI text; [Header("按钮设置")] public Button mainMenuButton; public Button startButton; public Button loseButton; public Button winButton; private bool isTyping = false; private Coroutine typingCoroutine; private string currentSentence; private DialogueState currentState = DialogueState.Before; private int currentDialogueIndex = 0; private List<string> currentDialogueList; void Start() { // 隐藏所有按钮 // 检查战斗结果 int result = PlayerPrefs.GetInt("LastBattleResult", -1); if (result == 1) { Debug.Log("战斗胜利 - 触发胜利对话"); SetDialogueState(DialogueState.Win); } else if (result == 0) { Debug.Log("战斗失败 - 触发失败对话"); SetDialogueState(DialogueState.Lost); } else { Debug.Log("首次进入 - 默认对话"); SetDialogueState(DialogueState.Before); } // 清除状态 PlayerPrefs.DeleteKey("LastBattleResult"); } void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); } // 初始化当前对话列表 currentDialogueList = beforeBattleDialogues; // 设置按钮点击事件 mainMenuButton?.onClick.RemoveAllListeners(); startButton?.onClick.RemoveAllListeners(); loseButton?.onClick.RemoveAllListeners(); winButton?.onClick.RemoveAllListeners(); // 重新绑定正确的事件 if (mainMenuButton != null) mainMenuButton.onClick.AddListener(OnMainMenuClick); if (startButton != null) startButton.onClick.AddListener(OnStartClick); if (loseButton != null) loseButton.onClick.AddListener(OnLoseClick); if (winButton != null) winButton.onClick.AddListener(OnWinClick); } void Update() { // 添加跳过功能:按E键跳过当前打字效果 if (Input.GetKeyDown(KeyCode.E) && isTyping) { SkipTyping(); } // 按E键继续下一句对话 else if (Input.GetKeyDown(KeyCode.E) && !isTyping && tmpText.gameObject.activeSelf) { NextSentence(); } } // 隐藏所有按钮 // 设置当前对话状态 public void SetDialogueState(DialogueState newState) { currentState = newState; currentDialogueIndex = 0; switch (newState) { case DialogueState.Before: currentDialogueList = beforeBattleDialogues; break; case DialogueState.Win: currentDialogueList = winDialogues; break; case DialogueState.Lost: currentDialogueList = lostDialogues; break; } Debug.Log($"状态已切换至: {newState}"); } // 启动当前状态的对话 public void StartStateDialogue() { if (currentDialogueList == null || currentDialogueList.Count == 0) return; // 如果正在显示其他文本,先停止 if (isTyping) { StopCoroutine(typingCoroutine); } // 确保索引在有效范围内 currentDialogueIndex = Mathf.Clamp(currentDialogueIndex, 0, currentDialogueList.Count - 1); currentSentence = currentDialogueList[currentDialogueIndex]; // 暂停玩家移动 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = false; // 锁定玩家输入 Cursor.lockState = CursorLockMode.None; Cursor.visible = true; // 显示对话框UI tmpText.gameObject.SetActive(true); // 启动打字效果协程 typingCoroutine = StartCoroutine(TypeSentence(currentSentence)); } // 显示下一句对话 public void NextSentence() { currentDialogueIndex++; if (currentDialogueIndex < currentDialogueList.Count) { StartStateDialogue(); } else { // 对话结束 EndDialogue(); } } IEnumerator TypeSentence(string sentence) { isTyping = true; tmpText.text = ""; // 逐字显示 foreach (char letter in sentence.ToCharArray()) { tmpText.text += letter; yield return new WaitForSeconds(typingSpeed); } isTyping = false; } // 跳过当前打字效果 public void SkipTyping() { if (isTyping) { StopCoroutine(typingCoroutine); tmpText.text = currentSentence; isTyping = false; } } public void EndDialogue() { if (isTyping) { StopCoroutine(typingCoroutine); isTyping = false; } // 恢复玩家控制 playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; // 隐藏对话框 tmpText.gameObject.SetActive(false); // 根据状态显示对应按钮 switch (currentState) { case DialogueState.Before: if (startButton != null) startButton.gameObject.SetActive(true); if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(true); break; case DialogueState.Win: if (winButton != null) winButton.gameObject.SetActive(true); if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(true); break; case DialogueState.Lost: if (loseButton != null) loseButton.gameObject.SetActive(true); if (mainMenuButton != null) mainMenuButton.gameObject.SetActive(true); break; } // 确保事件系统可用 EnsureEventSystemExists(); // 设置按钮为选中状态 if (currentState == DialogueState.Before && startButton != null) { EventSystem.current.SetSelectedGameObject(startButton.gameObject); } else if (currentState == DialogueState.Win && winButton != null) { EventSystem.current.SetSelectedGameObject(winButton.gameObject); } else if (currentState == DialogueState.Lost && loseButton != null) { EventSystem.current.SetSelectedGameObject(loseButton.gameObject); } } private void EnsureEventSystemExists() { EventSystem eventSystem = FindObjectOfType<EventSystem>(); if (eventSystem == null) { GameObject eventSystemObj = new GameObject("EventSystem"); eventSystem = eventSystemObj.AddComponent<EventSystem>(); eventSystemObj.AddComponent<StandaloneInputModule>(); } else if (eventSystem.GetComponent<StandaloneInputModule>() == null) { eventSystem.gameObject.AddComponent<StandaloneInputModule>(); } } private void OnMainMenuClick() { Debug.Log("主菜单按钮点击"); SceneManager.LoadScene("MainMenu"); } private void OnStartClick() { Debug.Log("开始作战"); SceneManager.LoadScene("Fight"); } private void OnLoseClick() { Debug.Log("失败按钮点击"); SceneManager.LoadScene("Fight"); } private void OnWinClick() { if (currentState != DialogueState.Win) { Debug.LogWarning("尝试在非胜利状态执行胜利逻辑"); return; } playercontrol player = FindObjectOfType<playercontrol>(); if (player != null) player.canMove = true; Debug.Log("执行胜利逻辑"); image.SetActive(false); image1.SetActive(false); image2.SetActive(false); text.gameObject.SetActive(false); } }我现在是想要将控制button的出现放在NPC2脚本中的触发器来进行控制
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷丘CODER

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值