繁体   English   中英

Unity 游戏对象可见性 - 无法多次显示游戏对象

[英]Unity Game Object Visibility - Unable to display Game Object more than once

我有一个带有 Image 和 TextMeshPro (TMP) 的 Canvas 作为子项,并且对话框的 canvas 组件在 Start() 方法中设置为 false 以便将其隐藏在主 Canvas 中。 TMP 出现在图像上(就像对话框中的文本)。 我在 2D 环境中有一个播放器和一个硬币精灵。 当玩家拿起硬币时,我尝试显示如下所示的对话框和 TMP。

public class PlayerMovement : MonoBehaviour {

    public GameObject suggestion;
    public GameObject dialogBox;

    private bool wasSuggestionShown; //to check if dialog was shown

    private void Start()
    {
        wasSuggestionShown = false;
        suggestionTimer = 0;
        dialogBox.GetComponent<Canvas>().enabled = false; //To hide the dialog box
    }

    void Update () {

        //horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
        horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input

        if (wasSuggestionShown)
        {
            suggestionTimer += Time.deltaTime;
            if (suggestionTimer > 5)
            {
                wasSuggestionShown = false;
                dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
            }
        }
    }

    void FixedUpdate ()
    {
        // Move the character
        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
        jump = false;
    }


    //For destroying coin object on collision with player
    private void OnTriggerEnter2D(Collider2D col)
    {
        
        if (col.gameObject.CompareTag("coin"))
        {

            //For destroying coin and to recude player movement speed.

            Destroy(col.gameObject); //Coin Disappears

            isRunSpeedReduced = true;
            runSpeed = 10f;

            //For Showing dialog box

            dialogBox.GetComponent<Canvas>().enabled = true;
            wasSuggestionShown = true;
        }
    }
}

OnTriggerEnter2D()方法中,我检查玩家角色是否接触硬币,如果是,我销毁对象并显示对话框和 TMP 并在 5 秒后隐藏它们。 问题是

“当我包含另一个硬币时,相同的对话框和 TMP,在玩家选择第二个硬币时不会出现。两个硬币都有相同的标签“硬币””

有人可能会争辩说,如果脚本在同一个被销毁或不活动的对象中,那么这是不可能的。 但是我是在附加到玩家对象的玩家移动脚本中完成所有这些操作的。

此外,我切换对话框的方式没有任何区别。 dialogBox.GetComponent<Canvas>().enabled = true; dialogBox.SetActive(true)任何一个只显示一次,并且是第一次出现。

即使我想实例化它,我也不知道在画布中正确定位它的确切变换。 (我想要它在底部中间部分,就像它可以锚定一样)

场景层次:

在此处输入图片说明

问题出在您的Update() ,您在suggestionTimer计时器结束后关闭画布:

void Update () {

    //horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
    horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input

    if (wasSuggestionShown)
    {
        suggestionTimer += Time.deltaTime;
        if (suggestionTimer > 5)
        {
            wasSuggestionShown = false;
            dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
        }
    }
}

原因是您在击中新硬币时从未重置过suggestionTimer计时器。 做这个:

private void OnTriggerEnter2D(Collider2D col)
{
    
    if (col.gameObject.CompareTag("coin"))
    {

        //For destroying coin and to recude player movement speed.

        Destroy(col.gameObject); //Coin Disappears

        isRunSpeedReduced = true;
        runSpeed = 10f;

        //For Showing dialog box

        dialogBox.GetComponent<Canvas>().enabled = true;
        wasSuggestionShown = true;

        //  !!! ADD THIS
        suggestionTimer = 0;
    }
}

问题是在OnTriggerEnter2D ,您设置wasSuggestionShown = true ,然后在下一个 Update 方法中,您正在检查wasSuggestionShown ,看到它是真的,然后立即再次关闭画布。

这可能会有所帮助。 这只是十几种方法中的一种,可以提供帮助:

private bool _showSuggestion = true;
public bool showSuggestion
{
  get { return _showSuggestion; }
  set   
  {
    if ( !value && _showSuggestion )
    {
      dialogBox.SetActive ( false );
      _showSuggestion = value;
    }
  }
}


void Update () {

    //horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
    horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input

    if ( showSuggestion )
    {
        suggestionTimer += Time.deltaTime;
        if ( suggestionTimer > 5f ) showSuggestion = false;
    }
}

上面的代码将等到您第一次将showSuggestion设置为 false,然后忽略任何进一步打开建议的尝试。 我怀疑这就是你想要的? 如果我读错了情况,可以稍微调整代码以获得正确的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM