当前位置:网站首页>Unity picture loading and saving

Unity picture loading and saving

2022-06-23 07:35:00 Niu Shenzi

load :

1. asynchronous :( In fact, it is loaded in the main thread )
Use UnityWebRequest( perhaps WWW) Load pictures , You can load local ( Slow speed ,2048*1024 Size of the picture loading time 780ms, No card interface ) And online pictures .

UnityWebRequest request = UnityWebRequestTexture.GetTexture(Application.streamingAssetsPath + "/Test3.jpg");
yield return request.SendWebRequest();
if (request.downloadHandler.isDone)
{
    
    var tex = DownloadHandlerTexture.GetContent(request);
}

2. Sync :
Use File To read ( Fast ,2048*1024 Size of the picture loading time 22ms, Card main thread interface , Short time for small pictures , Imperceptible )

var bytess = File.ReadAllBytes(Application.streamingAssetsPath + "/Test3.jpg");
var temp = new Texture2D(1, 1);
temp.LoadImage(bytess);

preservation :

1. Sync :

// This is a time-consuming step , The picture is big , Card master thread 
var bytes3 = tex.EncodeToJPG();
// The following save can also be done asynchronously , But the storage time is very short , It can also be done without ,2048*1024 The storage time is about 1ms,
// Large pictures can be saved asynchronously 
File.WriteAllBytes(Application.streamingAssetsPath + "/Test3.jpg", bytes3);

2. asynchronous :

var pixels = tex.GetPixels32(0);
GraphicsFormat format = tex.graphicsFormat;
uint width = (uint)tex.width;
uint height = (uint)tex.height;
Task.Run(() =>
{
    
    var bytes3 = ImageConversion.EncodeArrayToJPG(pixels, format, width, height);
    File.WriteAllBytes(Application.streamingAssetsPath + "/Test3.jpg", bytes3);
});
原网站

版权声明
本文为[Niu Shenzi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230640286637.html