在运行时更改GameObject的素材

时间:2017-05-06 06:58:51

标签: c# unity3d runtime material

我想实例化四个新的游戏对象。我想分配从Web服务器接收的四种材料。现在,我可以创建四种材料并将图像存储到它们中。但是,我无法改变这四种游戏对象材料。例如,我想改变第一个gameobject材料是image0,然后第二个gameobject是image1,依此类推。有人可以帮帮我吗?这是我的代码:

public int k = 0;
public string filename;
public string url;
public string path;
public GameObject newpaper;
public Material material;
public GameObject newObject ;



[MenuItem("GameObject/Create Material")]

void Start () {
    StartCoroutine (LoadImage ());
}


void Update () {

    GetComponent<Renderer> ().material = material;


}

IEnumerator LoadImage(){
    for (int i = 0; i < 4; i++) {

        Instantiate (newpaper);

        filename = "image" + k.ToString () + ".png";
        Debug.Log (k);
        url = "https://wwwfoodparadisehk.000webhostapp.com/" + filename; 
        WWW www = new WWW (url);
        yield return www;

        path = "Assets/MyMaterial" + k.ToString () + ".mat";

        k = k + 1;

        material = new Material (Shader.Find ("Sprites/Default"));
        AssetDatabase.CreateAsset (material, path);


        Debug.Log (AssetDatabase.GetAssetPath (material));

        material.mainTexture = www.texture;

        //GetComponent<Renderer> ().material = material;
        yield return 0;

    }

}

0 个答案:

没有答案