0
我想實例化四個新的gameobject。我想分配從Web服務器接收的四種材料。現在,我可以創建四種材質並將圖像存儲到它們中。但是,我無法改變這四個gameobject材質。例如,我想更改第一個gameobject材質是image0,然後第二個gameobject是image1,依此類推。任何人都可以幫助我?這裏是我的代碼:在運行時更改GameObject的素材
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;
}
}
還有另外一個問題。 gameobject newpaper有父母,它是飛機。我想改變平面材質。我該如何改變它? – HappyChan
您可以使用'newPaperInstance.transform.parent.gameObject'訪問gameObject的父級,然後訪問它的'Renderer'組件並更改其材質。 :) –
你能爲我編輯代碼嗎?我做不到。我不知道腳本粘貼正確。 – HappyChan