首页 > 解决方案 > 如何通过 C# (Unity) 中的代码隐藏画布

问题描述

(C#/Unity 的新手,所以这可能是一个愚蠢的问题,但有一种简单的方法可以解决它)

当我用枪进行 ADS 时,我试图隐藏一块画布,它充当十字准线。我已经搜索了如何做到这一点,但在 ADSing 时它仍然没有隐藏画布。

我该如何解决这个问题,或者有更简单的方法吗?

    if (ISADS == true) //if ADS is true
    {
        Debug.Log(ISADS);
        GameObject.Find("AK-47").GetComponent<Canvas>().enabled = false; //hide the canvas (The crosshair)
        GameObject.Find("Pistol").GetComponent<Canvas>().enabled = false; // same with Pistol
        GameObject.Find("870_Shotgun").GetComponent<Canvas>().enabled = false; // same with Shotgun
    }

    else
    {
        Debug.Log(ISADS);
        GameObject.Find("AK-47").GetComponent<Canvas>().enabled = true; //else (if you're not ADSing) show the canvas
        GameObject.Find("Pistol").GetComponent<Canvas>().enabled = true; // same with Pistol
        GameObject.Find("870_Shotgun").GetComponent<Canvas>().enabled = true; // same with Shotgun
    }

标签: c#unity3d

解决方案


我建议这样做有两种方法:

  1. 设置gameObject.SetActive(false)

  2. 或者将 CanvasGroup 组件添加到您的 Canvas。然后您可以编辑 3 个不同的值,称为 alpha、intractable 和 blocksraycasts。IE,GameObject.Find("AK-47").GetComponent<CanvasGroup>().alpha = 0


推荐阅读