Unity 3d - 渲染的材质选择

时间:2012-12-10 09:23:45

标签: unity3d textures materialized-views

我正在尝试在运行时更改墙的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();
}
}

1 个答案:

答案 0 :(得分: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];