【发布时间】:2022-06-29 04:17:46
【问题描述】:
我目前正在制作一个演示,对于我的场景,我需要暂停协程,直到我在演示中做出一个特定的动作来禁用特定的游戏对象。
为此,我尝试创建一个布尔函数,该函数将说明对象是否处于活动状态(使用 activeSelf),并且在我的协程主函数中,我创建了一个 while(is_active) 循环,内部有一个 yield return null(我在下面显示代码)。我的问题是 Is_Active 布尔函数似乎被阻止了,当我第一次激活对象时(通过函数 ShowArrowAndOutline()),我不再收到任何消息。
请问您有什么想法或解决方案可以帮助我吗?
public class Scenario : MonoBehaviour
{
public float delay = 0.1f;
private string fullText = "Welcome to *** demo !";
private string currentText = "";
// Start is called before the first frame update
void Start()
{
StartCoroutine(ShowText());
}
IEnumerator ShowText()
{
yield return StartCoroutine(LetterbyLetter());
yield return new WaitForSeconds(3);
fullText = "We will guide you through this experience.";
yield return StartCoroutine(LetterbyLetter());
yield return new WaitForSeconds(3);
fullText = "Let's start with the Steering Wheel !";
yield return StartCoroutine(LetterbyLetter());
yield return new WaitForSeconds(3);
GameObject TextBox = GameObject.Find("TextBox");
TextBox.SetActive(false);
this.GetComponent<Text>().text = "";
ShowArrowAndOutline();
while(Is_Active()){
yield return null;
}
GameObject m_Canvas = GameObject.Find("Canvas");
TextBox = FindChildObject(m_Canvas,"TextBox");
TextBox.SetActive(true);
GameObject guideText = FindChildObject(TextBox,"GuideText");
guideText.SetActive(true);
Debug.Log("debug message");
}
IEnumerator LetterbyLetter()
{
for(int i = 0; i < fullText.Length+1; i++){
currentText = fullText.Substring(0,i);
this.GetComponent<Text>().text = currentText;
yield return new WaitForSeconds(delay);
}
}
void ShowArrowAndOutline()
{
GameObject camera = GameObject.Find("Camera");
GameObject arrow = FindChildObject(camera,"ArrowTarget");
arrow.SetActive(true);
GameObject steeringwheel = GameObject.Find("SteeringWheel");
Outline outline = steeringwheel.GetComponent<Outline>();
outline.enabled = true;
}
GameObject FindChildObject(GameObject parent, string child_name){
Transform trs = parent.GetComponentInChildren<Transform>(true);
GameObject child = new GameObject();
foreach(Transform t_child in trs)
{
if(t_child.name == child_name){
child = t_child.gameObject;
}
}
return child;
}
bool Is_Active()
{
GameObject camera = GameObject.Find("Camera");
GameObject arrow = FindChildObject(camera,"ArrowTarget");
if(arrow.activeSelf == false){
return false;
}
return true;
}
void Update()
{
Debug.Log(Is_Active());
}
}
【问题讨论】:
-
yield return new WaitWhile(() => Is_Active()); -
@DigvijaysinhGohil 已经测试过了,同样的问题,当对象被禁用时协程不会继续,因为真正的问题是当 ShowArrowAndOutline() 执行时似乎 Is_Active() 停止工作(在至少我不再有我放入更新的调试消息了)。
-
这个问题不是关于unityscript,而是关于c#