【问题标题】:Unity C# - Trying to pause coroutine until a specific GameObject is disabledUnity C# - 尝试暂停协程,直到禁用特定的游戏对象
【发布时间】: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(() =&gt; Is_Active());
  • @DigvijaysinhGohil 已经测试过了,同样的问题,当对象被禁用时协程不会继续,因为真正的问题是当 ShowArrowAndOutline() 执行时似乎 Is_Active() 停止工作(在至少我不再有我放入更新的调试消息了)。
  • 这个问题不是关于unityscript,而是关于c#

标签: c# unity3d


【解决方案1】:

您可以使用 OnDisable() 方法 (https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDisable.html) 为您等待禁用的对象 (ArrowTarget) 提供一个脚本。从那里您可以翻转一个公共布尔变量,替换 Is_Active 检查。

【讨论】:

  • 好主意。但我不能让它工作:布尔值一开始是真的,因为对象在开始时在场景中被禁用,然后,当我在我的 ShowText() 函数中使其处于活动状态时,布尔值似乎没有变成真(至少在我更新中的 Debug.Log 消息中)..
【解决方案2】:

回答您的解决方案@Alex Leest。
当箭头在开始时禁用时,布尔值 isDisable 返回 true,但是当我激活游戏对象 ArrowTarget 时,布尔值保持为 true。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多