2016-06-16 119 views
2

我的代碼類似於此一大塊:禁用遊戲對象創建

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?

+0

?你使用'Image'組件作爲按鈕還是'Button'組件作爲按鈕?另外,發佈有助於修復代碼的按鈕代碼。 – Programmer

+0

我正在使用按鈕('UnityEngine.GUI.Button')。 –

+1

爲什麼不使用新的unity5 GUI元素?無論如何,解決方案是使用這樣的布爾。 1 - 做一個光線投射檢查,以測試你不打任何東西只是裏面,如果你的輸入檢查 2 - 如果光線投射==假 - >那麼你可以創建對象。 3-其他不是 讓我知道你是否需要進一步澄清。 – Cabrra

回答

1

在打字時,你把你自己的答案之前的過程。我仍然決定把我的答案,因爲你在你的答案做了什麼是錯誤的和不必要的。您分配內存多個每幀次,在你的答案代碼只檢測到點擊...

我使用一個按鈕(UnityEngine.GUI.Button)

我我正在使用unity5 GUI元素

你不應該用這個開始。有些人建議你應該使用光線投射,但通過UI的光線問題仍然存在。

你需要的是ButtonUnityEngine.UI.Button不是UnityEngine.GUI.Button。您必須更改您的整個程序才能使用UnityEngine.UI中的UI

要創建按鈕,你去遊戲對象 - >UI - >按鈕。這應該創建Canvas和其他組件以使Button運行。這裏是Unity UI tutorials

基本上它所做的是創建一個對象,我點擊 地面物體。

您可以通過執行IPointerClickHandler並覆蓋OnPointerClick函數來檢測按鈕單擊。

將下面的腳本附加到您想檢測點擊的地面上。

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
using UnityEngine.EventSystems; 

public class Click : MonoBehaviour, IPointerClickHandler 
{ 

    void Start() 
    { 
     Camera.main.gameObject.AddComponent<PhysicsRaycaster>(); 
    } 

    public void OnPointerClick(PointerEventData eventData) 
    { 
     Debug.Log("Clicked!"); 
    } 
} 

對於您的按鈕,您只需使用Button.onClick.AddListener(() => functionName());訂閱按鈕即可。以下是檢測3個按鈕點擊的完整代碼。關於這一點的好處是,只有一個點擊將被檢測到。點擊不會經過其他GameObjects,並且您是而不是爲每個幀分配內存只是爲了檢測按鈕點擊。

您正在使用什麼類型的按鈕
using UnityEngine; 
using UnityEngine.UI; 

public class Test : MonoBehaviour 
{ 

    public Button button1; 
    public Button button2; 
    public Button button3; 

    void OnEnable() 
    { 
     //Register Button Events 
     button1.onClick.AddListener(() => buttonCallBack(button1)); 
     button2.onClick.AddListener(() => buttonCallBack(button2)); 
     button3.onClick.AddListener(() => buttonCallBack(button3)); 

    } 

    private void buttonCallBack(Button buttonPressed) 
    { 
     if (buttonPressed == button1) 
     { 
      //Your code for button 1 
      //Call your SetBuilding(GameObject building) function for any button 
      SetBuilding(someGameOBject); 
     } 

     if (buttonPressed == button2) 
     { 
      //Your code for button 2 
     } 

     if (buttonPressed == button3) 
     { 
      //Your code for button 3 
     } 
    } 

    void OnDisable() 
    { 
     //Un-Register Button Events 
     button1.onClick.RemoveAllListeners(); 
     button2.onClick.RemoveAllListeners(); 
     button3.onClick.RemoveAllListeners(); 
    } 
} 
+0

我的意思是'UnityEngine.UI.Button',對不起,我有一個typeo –

+0

我想你錯過了這個問題。想象一下城市建築遊戲,您可以在其中點擊要從UI構建的物品,然後點擊地圖放置該建築物。我正在做同樣的事情,但是發生的事情是,當你點擊UI按鈕時,它會選擇建築物,然後立即將其放置在地圖上。 –

+0

@GetOffMyLawn不知道這是一個錯字,因爲'UnityEngine.GUI.Button'也是有效的。你在答案中有什麼奇怪的,不好的。爲每個幀創建一個新列表是可怕的。如果您想修復它,我的答案對您仍然有效。你說你和我一樣做着同樣的事情。這不是真的。 – Programmer

0

我不但要知道,如果我清楚地瞭解您所實例化的按鈕,在點擊:

ghostBuildingObject = Instantiate(building); 

Instantiate(prefab, /* At mouse position on ground */); 

所以這是正常的,要創建它的2倍

+0

它只被創建一次。我不希望它被創建。 「Ghost Build」是一個項目,顯示預製件在地面上被點擊後放置的位置。 –

0

你有沒有試着用:

if(Input.GetMouseButtonUp(0) && prefab != null){ 
    Instantiate(prefab, /* At mouse position on ground */); 
    prefab = null; 
} 
+0

'Input.GetMouseButtonUp(0)&&預製!= null'是-一樣團結'Input.GetMouseButtonUp(0)&& prefab'因爲沒有爲每個對象,使得如果不爲null他們返回true的運算符重載。 – Programmer

-1

使用指針事件似乎解決該問題:

void Update(){ 
    // Get pointer events 
    var pointer = new PointerEventData(EventSystem.current); 
    pointer.position = Input.mousePosition; 

    // Test the raycast 
    List<RaycastResult> raycastResults = new List<RaycastResult>(); 
    EventSystem.current.RaycastAll(pointer, raycastResults); 

    // Check if anything was cast 
    if(Input.GetMouseButtonUp(0) && raycastResults.Count == 0 && prefab){ 
     Instantiate(prefab, /* At mouse position on ground */); 
     prefab = null; 
    } 
}