【问题标题】:vuforia-unity virtual button enabling or disabling teapotvuforia-unity 虚拟按钮启用或禁用茶壶
【发布时间】:2018-07-13 19:24:43
【问题描述】:

我按照教程在 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

有人可以指出我将如何在按下“红色”按钮时启用.teapot.gameobject 并在释放“红色”按钮时禁用茶壶游戏对象?

【问题讨论】:

    标签: unity3d vuforia


    【解决方案1】:

    首先,您必须有一个对您的茶壶游戏对象的引用。因此,在您声明变量的顶部添加以下内容:

    public GameObject teaPotGameObject;
    

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

    teaPotGameObject.SetActive(true);
    

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

    【讨论】:

    • 非常感谢。我按照你的解释做了一切。应用程序在第一次正确执行后崩溃。
    • 它工作正常。我删除了干扰的默认操作。现在它工作得很好。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多