1. 程式人生 > >代碼動態更換MeshRenderer的材質Materials

代碼動態更換MeshRenderer的材質Materials

image ima frame bsp etc iou nbsp call mes

public class metrailstest : MonoBehaviour
{
    Material[] m;
    Material i;
    public Material[] n;
    int index = 0;
    // Use this for initialization
    void Start()
    {
        i = this.GetComponent<MeshRenderer>().material;
        m = this.GetComponent<MeshRenderer>().materials;
    }

    
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.A)) { index++; if (index % 2 == 0) this.GetComponent<MeshRenderer>().materials = n; else this.GetComponent<MeshRenderer>().materials = m; } } }

效果如下:

技術分享

代碼動態更換MeshRenderer的材質Materials