[英]How to switch display/non-display by right-clicking in C# of Unity
我尝试使用以下代码切换 object 的可见性。
但是,第一次右击时,object被隐藏了,但我第二次右击时,却没有显示object。
object 应出现在第二次右键单击中。
这段代码有什么问题?
using UnityEngine;
public class EnvironmentSettings : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Debug.Log(this.gameObject.activeSelf);
if(this.gameObject.activeSelf)
{
this.gameObject.SetActive(false);
}
else
{
this.gameObject.SetActive(true);
}
}
}
}
如果你禁用了保存脚本的游戏 object,它就不能再工作了,因为它没有激活,这就是你可以隐藏 object 的原因。 但不要唤醒它。
使用您将启用/禁用的 TargetGameObject 字段。 像这样:
public class EnvironmentSettings : MonoBehaviour
{
public GameObject TargetGameObject;
private void Update()
{
if (Input.GetMouseButtonDown(1))
TargetGameObject.SetActive(!TargetGameObject.activeSelf);
}
}
将此脚本放在 GameManger 上,或者至少放在游戏期间不会被禁用的东西上,例如主摄像头。 然后滑动您的 TargetGameObject 以从场景视图切换到该字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.