我需要從螢屏截圖中加載 png 影像,然后將其轉換為 sprite 并將其放入影像中。我在下面有代碼,但它沒有看到任何作業?
為什么?
string pngpath = Application.dataPath $"/Screenshot{index}.png";
Sprite sp = Resources.Load<Sprite>(pngpath);
child.GetComponent<Image>().sprite = sp;
public IEnumerator CaptureScreen()
{
// Wait till the last possible moment before screen rendering to hide the UI
yield return null;
canvan.GetComponent<Canvas>().enabled = false;
foreach (GameObject i in Ball)
{
i.GetComponent<Renderer>().enabled = false;
}
// Wait for screen rendering to complete
yield return new WaitForEndOfFrame();
// Take screenshot
ScreenCapture.CaptureScreenshot(Application.dataPath $"/Screenshot{index}.png");
// Show UI after we're done
canvan.GetComponent<Canvas>().enabled = true;
foreach (GameObject i in Ball)
{
i.GetComponent<Renderer>().enabled = true;
}
index ;
PlayerPrefs.SetInt("index", index);
}
uj5u.com熱心網友回復:
您沒有將 png 轉換為 sprite,試試這個。
string pngpath = Application.dataPath $"/Screenshot{index}.png";
byte[] filedata;
filedata = System.IO.File.ReadAllBytes(pngpath);
Texture2D text = new Texture2D(2, 2);
text.LoadImage(filedata);
Rect rec = new Rect(0, 0, text.width, text.height);
Sprite sp = Sprite.Create(text, rec, new Vector2(0, 0), 0.1f);
child.GetComponent<Image>().sprite = sp;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/437901.html
標籤:unity3d
上一篇:如何獲取網路請求的上傳進度
下一篇:如何在代碼中使影片控制器轉換?
