I have created virtual button in Unity-Vuforia by following tutorial. It works successfully without any glitch. The problem is I tried to enable or disable teapot upon press or release. I have tried the following code for changing materials:
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
Somebody could point me how will I enable.teapot.gameobject upon 'red'button pressed and disable teapot gameobject upon 'red' button released?
First of all you have to have a reference to your teapot gameobject. So at the top where you declare your variables add this:
public GameObject teaPotGameObject;
Assign teapot to this slot in the inspector. Then in the OnButtonPressed() function after case "red":
add this line:
teaPotGameObject.SetActive(true);
And well, I guess you already know what to do in OnButtonReleased() function :))