C# 如何在单击特定对象后显示gui.button或在单击后禁用gui.button?

C# 如何在单击特定对象后显示gui.button或在单击后禁用gui.button?,c#,button,user-interface,unity3d,onmousedown,C#,Button,User Interface,Unity3d,Onmousedown,到目前为止,我有以下脚本: Using UnityEngine; using System.Collections; public class text : MonoBehaviour { public GameObject mainCam; public bool showButton = false; void OnGUI () { // Make a background box GUI.Box(new Rect(10,10,230,150), "Menu"); i

到目前为止,我有以下脚本:

Using UnityEngine;
using System.Collections;

public class text : MonoBehaviour {

public GameObject mainCam; 

public bool showButton = false;



void OnGUI () {



// Make a background box
GUI.Box(new Rect(10,10,230,150), "Menu");


if (GameObject.Find("block1") && Input.GetMouseButtonDown(0)) { 

showButton = true; 

if(GUI.Button (new Rect (30,40,200,70), "Back to the blocks ")) {

print ("You clicked the button! The menu now appears");

mainCam.transform.position= new Vector3(-.13f, 0.87f, -8);

Camera.main.orthographicSize = 0.4f;



    }

} 
}
}

当视图位于多个块上时,我希望该按钮处于禁用或非活动状态,然后当我单击一个块并将其带到其中一个块的放大视图时,我希望显示gui.button。然后,如果我返回到包含所有块的主视图,我希望再次禁用按钮。我不知道该怎么做

您在以下任何情况下都没有使用showButton boolean编辑的脚本可能适合您

   public class text : MonoBehaviour {

public GameObject mainCam; 

public bool showButton = false;



void OnGUI () {



// Make a background box
GUI.Box(new Rect(10,10,230,150), "Menu");


if (GameObject.Find("block1") && Input.GetMouseButtonDown(0)) { 

showButton = true; 

if(GUI.Button (new Rect (30,40,200,70), "Back to the blocks ") && showButton) {//check showButton
showButton = false;// when you want go back to blocks then make it false
print ("You clicked the button! The menu now appears");

mainCam.transform.position= new Vector3(-.13f, 0.87f, -8);

Camera.main.orthographicSize = 0.4f;



    }

} 
}
}

出于某种原因,它只是禁用了所有功能。即使我选中了active或inactive,showButton的复选框也不起作用:/这很奇怪。实际上,需要注意的一件有趣的事情是:mainCam总是设置在特定的坐标上,所以我所做的是:void Update(){if(mainCam.transform.position==new Vector3(0,1.1f,-8)){GUI.enabled=false;}该视图仅适用于主摄像机位于某个位置时,逻辑上应该可以工作,但由于某些原因,GUI未被禁用。