簡體   English   中英

將動畫gif轉換為Unity Texture2D數組時的反向字節數組

[英]Reverse Byte Array when converting an animated gif to a Unity Texture2D Array

我目前正在嘗試將動畫gif加載到Unity應用程序中(在運行時),但是遇到了一些麻煩:

我正在使用System.Drawing從每個gif幀加載字節數組,然后使用Unity的LoadRawTextureData函數創建Texture。 我現在遇到的問題是數組中字節的順序不是Unity期望的(即使我將兩者的Format都指定為ARGB32)。 顯然,Unity期望ABGR和System.Drawing給我RGBA,反之亦然。 同樣,圖像會垂直翻轉(但是可以很容易地修復)。

這是我當前的代碼,可以運行,但是我必須反轉數組,這將花費10-20倍的時間。 它仍然至少是分別復制每個像素的兩倍快,但是我希望如果我能更接近不反轉的情況下獲得的性能。

GraphicsUnit graphicsUnit = GraphicsUnit.Pixel;
RectangleF rect1 = gifImage.GetBounds(ref graphicsUnit);
Rectangle rect2 = new Rectangle((int)rect1.X, (int)rect1.Y, (int)rect1.Width, (int)rect1.Height);
byte[] rgbValues = new byte[rect2.Width * rect2.Height * 4];

for (int i = 0; i < frameCount; i++) {
    gifImage.SelectActiveFrame(dimension, i);
    Bitmap frame = new Bitmap(gifImage.Width, gifImage.Height);
    System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);
    frame.RotateFlip(RotateFlipType.RotateNoneFlipX);

    Texture2D frameTexture = new Texture2D(frame.Width, frame.Height, TextureFormat.ARGB32, false);

    BitmapData data = frame.LockBits(rect2, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    Marshal.Copy(data.Scan0, rgbValues, 0, rgbValues.Length);
    frame.UnlockBits(data);

    frameTexture.LoadRawTextureData(rgbValues.Reverse().ToArray());
    frameTexture.Apply();
    gifFrames[i] = frameTexture;
}

還有什么我可以嘗試以“正確”的順序獲取字節的嗎?

如果使用TextureFormat.BGRA32構造Texture2D,則可以跳過.Reverse() 但是,這並不能真正解決其必要性的原因

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM