vuforia-unity 虚拟按钮启用或禁用茶壶
vuforia-unity virtual button enabling or disabling teapot
我已经按照教程在 Unity-Vuforia 中创建了虚拟按钮。它成功运行,没有任何故障。
问题是我试图在按下或释放时启用或禁用茶壶。我尝试了以下代码来更改材料:
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
有人可以告诉我如何在按下 'red' 按钮时 enable.teapot.gameobject 并在释放 'red' 按钮时禁用茶壶游戏对象?
首先你必须有一个对你的茶壶游戏对象的引用。所以在你声明变量的顶部添加这个:
public GameObject teaPotGameObject;
将茶壶分配给检查器中的这个插槽。然后在 case "red":
之后的 OnButtonPressed() 函数中添加这一行:
teaPotGameObject.SetActive(true);
好吧,我想您已经知道在 OnButtonReleased() 函数中要做什么:))
我已经按照教程在 Unity-Vuforia 中创建了虚拟按钮。它成功运行,没有任何故障。 问题是我试图在按下或释放时启用或禁用茶壶。我尝试了以下代码来更改材料:
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
有人可以告诉我如何在按下 'red' 按钮时 enable.teapot.gameobject 并在释放 'red' 按钮时禁用茶壶游戏对象?
首先你必须有一个对你的茶壶游戏对象的引用。所以在你声明变量的顶部添加这个:
public GameObject teaPotGameObject;
将茶壶分配给检查器中的这个插槽。然后在 case "red":
之后的 OnButtonPressed() 函数中添加这一行:
teaPotGameObject.SetActive(true);
好吧,我想您已经知道在 OnButtonReleased() 函数中要做什么:))