vuforia-unity虚拟按钮启用或禁用茶壶

时间:2016-05-13 16:26:32

标签: unity3d vuforia

我已经通过以下教程在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

有人可以指出我如何启用。按下红色&#39;按钮后启用.teapot.gameobject并禁用茶壶游戏对象&#39; red&#39;按钮发布了?

1 个答案:

答案 0 :(得分:0)

首先,您必须参考您的茶壶游戏对象。因此,在声明变量的顶部添加:

public GameObject teaPotGameObject;

在检查器中将茶壶分配给此插槽。然后在case "red":之后的OnButtonPressed()函数中添加以下行:

teaPotGameObject.SetActive(true);

好吧,我想你已经知道在OnButtonReleased()函数中该怎么做了:))