I'm creating a collection of buttons based on the contents of a list I have in my program. I'm filling my list from a JSON file I'm parsing in and creating the buttons like so:

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

}

The list prop1 contains the values from the first set of keys in my JSON. Where in my program it returns and displays two buttons with the text Foo and Boo

When I click on Foo I need to be able to return that name so I can pass it into a check like so:

public void SelectType()
{

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

Where as opposed to hardcoding in Foo I pass in another paramenter based on the button click. However, I've no idea how to grab the name out of the list when I press it. Could someone please point me in the right direction and help me figure out how to do this?

有帮助吗?

解决方案

Do you mean something like this?

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;
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top