【问题标题】:Return place in list on button press按下按钮时返回列表中的位置
【发布时间】:2014-05-06 08:19:04
【问题描述】:

我正在根据程序中的列表内容创建一组按钮。我正在从我正在解析的 JSON 文件中填写我的列表并创建如下按钮:

foreach(string ss in prop1)
{
    if(GUI.Button(new Rect(0, 100+w, 100, 100), prop1[count].ToString()))
    {
        SelectType();
    }

}

列表prop1 包含来自我的 JSON 中第一组键的值。在我的程序中,它返回并显示两个带有文本FooBoo 的按钮

当我点击Foo 时,我需要能够返回该名称,以便将其传递给这样的支票:

public void SelectType()
{

    if (obj["building"].Value == "Foo") 
    { 
        GameObject sceneObject = GameObject.Find("#" + key);
        sceneObject.renderer.material.color = Color.blue;
    }
}

Foo 中的硬编码相反,我根据按钮单击传入另一个参数。但是,当我按下它时,我不知道如何从列表中获取名称。有人可以指出我正确的方向并帮助我弄清楚如何做到这一点吗?

【问题讨论】:

    标签: c# json list button unity3d


    【解决方案1】:

    你的意思是这样的吗?

    foreach(string ss in prop1)
    {
        if(GUI.Button(new Rect(0, 100+w, 100, 100), prop1[count].ToString()))
        {
            SelectType(ss);
        }
    
    }
    
    public void SelectType(string type)
    {
    
        if (obj["building"].Value == type) 
        { 
            GameObject sceneObject = GameObject.Find("#" + key);
            sceneObject.renderer.material.color = Color.blue;
        }
    }
    

    【讨论】:

    • 我花了好几个小时试图解决这个问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多