第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從字節(jié)數(shù)組創(chuàng)建圖像并顯示它?

如何從字節(jié)數(shù)組創(chuàng)建圖像并顯示它?

C#
白衣非少年 2022-07-23 16:21:21
我想從圖像文件中獲取數(shù)據(jù)并在 Unity 可以讀取的紋理中顯示該信息。我能夠?qū)⑾袼匦畔⒎湃胱止?jié)數(shù)組中,但屏幕上沒有任何顯示。我如何真正讓圖像顯示?     pcxFile = File.ReadAllBytes("Assets/5_ImageParser/bagit_icon.pcx");     int startPoint = 128;     int height = 152;      int width = 152;      target = new Texture2D(height, width);     for (var y = 0; y < height; y++)     {         for (var x = 0; x < width; x++)         {             timesDone ++;             pixels[x, y] = new Color(pcxFile[startPoint], pcxFile[startPoint+1], pcxFile[startPoint+2]);             startPoint += 4;             target.SetPixel(x, y, pixels[x, y]);         }                 }     target.Apply();     target.EncodeToJPG();
查看完整描述

1 回答

?
HUH函數(shù)

TA貢獻1836條經(jīng)驗 獲得超4個贊

好吧,(假設(shè)您正確獲取像素數(shù)據(jù))您必須將創(chuàng)建的紋理分配給某些東西......


我會使用例如RawImage,因為它不需要 a Sprite(就像UI.Image組件一樣 - 另請參閱RawImage Manual):


// Reference this in the Inspector

public RawImage image;


//...


pcxFile = File.ReadAllBytes("Assets/5_ImageParser/bagit_icon.pcx");

int startPoint = 128;

int height = 152; 

int width = 152; 

target = new Texture2D(height, width);

for (var y = 0; y < height; y++)

{

    for (var x = 0; x < width; x++)

    {

        timesDone ++;

        pixels[x, y] = new Color(pcxFile[startPoint], pcxFile[startPoint+1], pcxFile[startPoint+2]);

        startPoint += 4;

        target.SetPixel(x, y, pixels[x, y]);

    }            

}

target.Apply();

// You don't need this. Only if you are also going to save it locally

// as an actual *.jpg file or if you are going to

// e.g. upload it later via http POST

//

// In this case however you would have to asign the result 

// to a variable in order to use it later

//var rawJpgBytes = target.EncodeToJPG();


// Assign the texture to the RawImage component

image.texture = target;

或者,與普通Image組件一起使用Sprite從您的紋理創(chuàng)建一個Sprite.Create:


// Reference this in the Inspector

public Image image;


// ...


var sprite = Sprite.Create(target, new Rect(0.0f, 0.0f, target.width, target.height), new Vector2(0.5f, 0.5f), 100.0f);


image.sprite = sprite;

提示 1

為了獲得正確的縱橫比,我通常使用一個小技巧:


為這個 RawImage 創(chuàng)建一個父對象

RectTransform在父級中設(shè)置所需的“最大尺寸”

RawImage在/旁邊Image(在子對象上)添加一個AspectRatioFitter(另見AspectRatioFitter Manual)并設(shè)置AspectMode為FitInParent.

現(xiàn)在在代碼中調(diào)整縱橫比(從紋理中獲?。?/p>


public RawImage image;

public AspectRatioFitter fitter;


//...


image.texture = target;


var ratio = target.width / target.height;

fitter.aspectRatio = ratio;

提示 2對所有像素調(diào)用一次比重復

調(diào)用“便宜” :SetPixelsSetPixel


// ...


startPoint = 0;

pixels = new Color[width * height]();

for(int i = 0; i < pixels.Length; i++)

{

    pixels[i] = new Color(pcxFile[startPoint], pcxFile[startPoint+1], pcxFile[startPoint+2]);

    startPoint += 4;

}


target.SetPixels(pixels);

target.Apply();


// ...

(我不知道您的pcx格式究竟是如何工作的,但也許您甚至可以使用LoadRawTextureData


查看完整回答
反對 回復 2022-07-23
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號