之前為了實作unity上傳的功能,基本上找相關資源都會找到要把image 轉成bytes[] 才行上傳
但是一般
byte[] bytes = showImage.sprite.texture.EncodeToPNG();
直接拿image來轉會出現
Unsupported texture format - Texture2D::EncodeTo functions do not support compressed texture formats.
解決方法為,不要直接針對畫面上的image來轉換,而是新建一個texture2d來進行轉換就行了
IEnumerator Upload()
{
yield return new WaitForEndOfFrame();
var sprite = showImage.sprite;
var targetTex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
var pixels = sprite.texture.GetPixels(
(int)sprite.textureRect.x,
(int)sprite.textureRect.y,
(int)sprite.textureRect.width,
(int)sprite.textureRect.height);
targetTex.SetPixels(pixels);
targetTex.Apply();
// Encode texture into PNG
byte[] bytes = targetTex.EncodeToPNG();
Object.Destroy(targetTex);
WWWForm form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("picture", bytes, "test.png", "image/png");
Debug.Log("upload url:"+url);
WWW w = new WWW(url, form);
yield return w;
if (w.isDone)
{
Debug.Log("上传完成");
}
}
留言列表