2017-09-11 13 views
1

首先,我的英語很抱歉。 我想從一個遊戲對象備份一些紋理到一個列表中,每個框架都有不同的紋理。我試圖做到這一點,我推動紋理到列表中的每一幀,但所有推動的紋理似乎被覆蓋到最新的紋理...我也試過Instantiate();當每個紋理被推入列表中但紋理完全透明時。我沒有使用電影紋理。有誰知道如何做到這一點?如何獲得改變遊戲對象上每個幀的紋理?

我可能沒有足夠的解釋,所以請問我是否不清楚。 謝謝

//Those line of codes are looping every frame //This is the current code transitionTextures.Add(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture); //This is the another code that I tried transitionTextures.Add(Instantiate(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture));

回答

0
**transitionTextures.Add(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture);** 

這行代碼拷貝targetGameObject.GetComponent的參考()。材料[0] .mainTexture到列表中,以便根據參照規則複製當過targetGameObject的mainTexture已更改,它將將列表中的所有引用更改爲已更新的新值。所以你應該創建一個新的對象,所以它應該被像這樣的值複製

Texture2D temp = targetGameObject.GetComponent<Renderer>().materials[0].mainTexture as Texture2D; 

Texture2D tex = new Texture2D(temp.width, temp.height); 
tex.SetPixels(temp.GetPixels()); 
tex.Apply(); 

transitionTextures.Add(tex); 

試試這個,它可能會幫助你。

+0

謝謝你的回答!我試過這段代碼,它實際上有另一個問題... temp.GetPixels()返回錯誤的「紋理」'沒有數據'。我花了幾個小時,但沒有運氣..你知道爲什麼會發生這種情況嗎? –

相關問題