0

我正在尝试Material在运行时更改墙。我从 Google Sketchup 导入房屋模型,它在一个对象中包含不同的材料(这在检查器中显示)。每当我单击下一个按钮 ( >>) 时,它都会更改对象的第一个材质。如何获取对其他元素的引用?这是我到目前为止所拥有的:

public class Material_GUI : MonoBehaviour {

public Material[] mats;
public GameObject go;
private int index = 0;

// Use this for initialization
void Start () {
    go.renderer.material= mats[index];
}

// Update is called once per frame
void Update () {
}

void OnGUI(){
    GUILayout.BeginArea(new Rect(Screen.width/2-100,Screen.height-60,200,50));
    GUI.Box (new Rect(10,10,190,40),"");

    GUI.Label(new Rect(62,20,100,20),"Wall Testing"+(index +1));

    if(GUI.Button (new Rect(15,15,30,30),"<<")){
        index--;
        if(index<0){
            index = mats.Length - 1;

        }
        go.renderer.material = mats[index];
    }

    if(GUI.Button (new Rect(165,15,30,30),">>")){
        index++;
        if(index > mats.Length -1){
            index = 0;

        }
        go.renderer.material = mats[index];
    }
    GUILayout.EndArea();
}
}
4

1 回答 1

0

如果要更改渲染器的其他材质,可以使用

go.renderer.materials

http://docs.unity3d.com/Documentation/ScriptReference/Renderer-materials.html?from=MeshRenderer

例如:

go.renderer.materials[0] = mats[0];
go.renderer.materials[1] = mats[1];
于 2012-12-13T11:40:15.023 回答