2016-05-13 27 views
0

我已經通過以下教程在Unity-Vuforia中創建了虛擬按鈕。它工作成功,沒有任何小故障。 問題是我試圖按下或釋放時啓用或禁用茶壺。我曾試圖改變材料下面的代碼:vuforia-unity虛擬按鈕啓用或禁用茶壺

public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) 
{ 
    Debug.Log("OnButtonPressed: " + vb.VirtualButtonName); 

    if (!IsValid()) 
    { 
     return; 
    } 

    // Add the material corresponding to this virtual button 
    // to the active material list: 
    switch (vb.VirtualButtonName) 
    { 
     case "red": 
      mActiveMaterials.Add(m_TeapotMaterials[0]); 
      break; 

     case "blue": 
      mActiveMaterials.Add(m_TeapotMaterials[1]); 
      break; 

     case "yellow": 
      mActiveMaterials.Add(m_TeapotMaterials[2]); 
      break; 

     case "green": 
      mActiveMaterials.Add(m_TeapotMaterials[3]); 
      break; 
    } 

    // Apply the new material: 
    if (mActiveMaterials.Count > 0) 
     mTeapot.GetComponent<Renderer>().material = mActiveMaterials[mActiveMaterials.Count - 1]; 
} 

/// <summary> 
/// Called when the virtual button has just been released: 
/// </summary> 
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb) 
{ 
    if (!IsValid()) 
    { 
     return; 
    } 

    // Remove the material corresponding to this virtual button 
    // from the active material list: 
    switch (vb.VirtualButtonName) 
    { 
     case "red": 
      mActiveMaterials.Remove(m_TeapotMaterials[0]); 
      break; 

     case "blue": 
      mActiveMaterials.Remove(m_TeapotMaterials[1]); 
      break; 

     case "yellow": 
      mActiveMaterials.Remove(m_TeapotMaterials[2]); 
      break; 

     case "green": 
      mActiveMaterials.Remove(m_TeapotMaterials[3]); 
      break; 
    } 

    // Apply the next active material, or apply the default material: 
    if (mActiveMaterials.Count > 0) 
     mTeapot.GetComponent<Renderer>().material = mActiveMaterials[mActiveMaterials.Count - 1]; 
    else 
     mTeapot.GetComponent<Renderer>().material = m_TeapotMaterials[4]; 
} 
#endregion //PUBLIC_METHODS 

有人可以點我,我會怎麼enable.teapot.gameobject當選擇「red'button按下並在禁用茶壺遊戲對象的‘紅色’按鈕釋放?

回答

0

首先你必須有一個對你的茶壺gameobject的參考。因此,在您聲明變量的頂部添加以下內容:

public GameObject teaPotGameObject; 

將茶壺分配給檢查器中的此插槽。然後在case "red":後OnButtonPressed()函數中添加這一行:

teaPotGameObject.SetActive(true); 

和好了,我想你已經知道在OnButtonReleased做()函數:))

+0

非常感謝你。正如你所解釋的,我做了一切。該應用程序正確執行第一次後崩潰。 – user6160538

+0

它的工作。我刪除了它是干擾的默認操作。現在它工作完美。謝謝 – user6160538