c# - 如何通过 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
}
解决方案
我建议这样做有两种方法:
设置
gameObject.SetActive(false)
或者将 CanvasGroup 组件添加到您的 Canvas。然后您可以编辑 3 个不同的值,称为 alpha、intractable 和 blocksraycasts。IE,
GameObject.Find("AK-47").GetComponent<CanvasGroup>().alpha = 0
推荐阅读
- php - LARAVEL 删除方法无法正常工作
- llvm - LLVM opt 工具不会对由 clang -O0 生成的 IR 进行矢量化
- javascript - 需要一个用于压缩文件夹的 JavaScript 简单代码
- monaco-editor - 有没有办法从摩纳哥编辑器的标记小部件中隐藏“偷看问题”链接?
- c - 在 C 中拆分字符串并从标准输入进行转换
- python - 我可以使用 celery 任务的 send_event 而不是 update_state 进行状态更新吗?
- rust - 如何有条件地启用基于 cfg 的 Rust 功能?
- python - odoo12 中的搜索菜单位置
- android - 如何在没有互联网的情况下以编程方式连接到 Wifi Android (Xamarin)
- azure-devops - 如何在 Azure devops 的发布管道中显示和引发警告