在active = false的情况下如何找到gameobject

如下代码,我们先获取顶级对象root,接着用Find()去找它的子节点“xxxx”的对象,无论"xxxx"对象是否active = true 都是可以直接找到对象的。

GameObject root = GameObject.Find("root") ;

GameObject xxxx = root.transform.Find("xxxx").gameObject;

xxxx.SetActive(true);

Find()方法只能去找子节点,如果要找孙节点,那么用"/"符号把层级关系隔开,找起来很方便。

GameObject cube = root.transform.Find("xxxx/Cube").gameObject;


但是Transform,Find()必须要保证你的顶级父对象的active = true,也就是说如果在用到某个对象需要在一开始的时候让它的active等于false的时候,那么为了以后能用Transform.Find()的方法找到它,必须给他加个父节点,并让它的父节点的active等于true



转自MOMO大神的微博

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值