我的代碼類似於此一大塊:禁用遊戲對象創建
GameObject prefab; // Gets set when GUI button is clicked
void Update(){
if(Input.GetMouseButtonUp(0) && prefab){
Instantiate(prefab, /* At mouse position on ground */);
prefab = null;
}
}
下面是代碼,當我按一下按鈕:
public void SetBuilding(GameObject building){
prefab = building;
Destroy(ghostBuildingObject);
ghostBuildingObject = Instantiate(building);
if (ghostBuildingObject) {
// Set the alpha to half
Color c = ghostBuildingObject.GetComponent<MeshRenderer>().material.color;
c.a = 0.5f;
ghostBuildingObject.GetComponent<MeshRenderer>().material.color = c;
// Disable Components
ghostBuildingObject.GetComponent<Collider>().isTrigger = true;
Destroy(ghostBuildingObject.GetComponent<NavMeshObstacle>());
Destroy(ghostBuildingObject.GetComponent<Building>());
ghostBuilding = ghostBuildingObject.AddComponent<GhostBuilding>();
ghostBuildingObject.layer = LayerMask.NameToLayer("Ghost");
ghostBuildingObject.name = "Ghost Building";
}
}
基本上它創建一個對象,我點擊地面物體。發生什麼是當我點擊一個按鈕時,它仍然會創建遊戲對象。當我點擊一個GUI按鈕時,如何阻止它創建一個gameObject?
?你使用'Image'組件作爲按鈕還是'Button'組件作爲按鈕?另外,發佈有助於修復代碼的按鈕代碼。 – Programmer
我正在使用按鈕('UnityEngine.GUI.Button')。 –
爲什麼不使用新的unity5 GUI元素?無論如何,解決方案是使用這樣的布爾。 1 - 做一個光線投射檢查,以測試你不打任何東西只是裏面,如果你的輸入檢查 2 - 如果光線投射==假 - >那麼你可以創建對象。 3-其他不是 讓我知道你是否需要進一步澄清。 – Cabrra