//第一种方法
public int aa;
void Update()
{
if(Input.GetKeyDown(KeyCode.S))//按下s键
{
aa++;
}
if(aa % 2 != 0)
{
GetComponent<Light>().enable = false;//关灯
}
else
{
GetComponent<Light>().enable = true;//开灯
}
}
//第二种方法
private bool bool_s;
void Update()
{
if(Input.GetKeyDown(KeyCode.S))//按下s键
{
bool_s = !bool_s;
if(bool_s)
{
GetComponent<Light>().enable = false;//关灯
}
if(!bool_s)
{
GetComponent<Light>().enable = true;//开灯
}
}
}
//第三种方法
void Update()
{
if(Input.GetKeyDown(KeyCode.S))//按下s键
{
GetComponent<Light>().enable = false;//关灯
}
if(Input.GetKeyDUp(KeyCode.S))//抬起s键
{
GetComponent<Light>().enable = true;//开灯
}
}