当前位置:网站首页>Unity3d learning note 9 - loading textures
Unity3d learning note 9 - loading textures
2022-07-23 09:03:00 【charlee44】
List of articles
1. summary
Theoretically ,Unity There is no difficulty loading textures in , Just place the picture in Assets In the folder , It will be recognized as a texture , So it can be loaded into the scene . But once there is some extra demand , You have to use other ways to load textures .
2. Detailed discussion
2.1. Resources The way
Use Resources Mechanism to load Unity Various resources , Include texture :
Texture2D texture = Resources.Load<Texture2D>("ImageDemo");
Resources yes Unity The resource management provided by the program .Resources This mechanism is especially suitable for games , For games , All the textures 、 texture of material 、Shader Etc. can be used as the built-in resources of the game .
2.2. API The way
Consider this situation : If the data we want to process comes from outside resources , That is, it is stored in the resource folder of the operating system . This situation Resources Can't use . In this case, we should understand the principle of texture : Texture images are first stored in hard disk space , And then through CPU Read into memory , Finally, the data is exchanged to GPU In the video memory of .
One way is to use image processing for reference , Such as GDAL、OpenCV etc. ( You can even pass by yourself File Realization ) Components , Read the image into memory , Organized into Unity Script class Texture2d Needed buffer:
int width = 512;
int height = 512;
texture = new Texture2D(width, height, TextureFormat.RGB24, false);
byte[] imgData = new byte[width * height * 3];
for(int yi = 0; yi< 128; yi++)
{
for(int xi = 0; xi < 128; xi++)
{
int m = width * 3 * yi + 3 * xi;
imgData[m] = 135;
imgData[m+1] = 206;
imgData[m+2] = 235;
}
}
texture.SetPixelData(imgData, 0, 0);
texture.Apply(false, false);
I will memory buffer front 128*128 Pixel buffer Fill in blue , It turns out to be :
That is, texture buffer It also starts from the lower left corner .
Apply() This function cannot be less , Because this function uploads the texture data in memory to GPU In the video memory of , And it costs more performance . It is best to change the pixel value after , Just once Apply() Upload to GPU.
2.3. Web The way
Except for the local , Resources are sometimes placed on remote servers . Regarding this Unity Provides WWW Class as the access interface , But it has been abandoned . Now? Unity Provide UnityWebRequest Class as and Web Interface for server communication , And access remote Texture Related classes include UnityWebRequestTexture. This set of interfaces is briefly studied , It also seems to provide asynchronous methods .
But I didn't use this interface in actual use . Because I want to put part of my work into multithreading . We know Unity Content related to rendering or resources cannot be put into multithreading , But accessing remote data 、 And the content read into memory can be put into multithreading . So the way of improvement is also very simple :
- Use C# Of WebRequest Interface ( Other network transmission interfaces are also ok ), Generally, the interface of network transmission can be placed in multithreading .
- Parse the obtained file stream , obtain RGB Format memory Buffer. This step can also be placed in multithreading .
- In the main thread, the memory Buffer Insert into Texture2D in .
边栏推荐
猜你喜欢
随机推荐
[ctfshow-web入门]SSRF
讲一讲HART协议
After planning job hopping for nearly half a year, there were finally four interviews and three interviews
(example of dynamic planning) stone merging
LiveQing直播点播流媒体OBS推流直播如何获得接口校验token视频校验streamToken及配置token有效期
There was an accident caused by MySQL misoperation, and "high availability" couldn't stand it
OSPF的路由控制
IDM下载器免费高质量的Win下载工具无使用限制
In depth explanation of CAS is necessary for interview practice
[ctfshow web getting started]ssrf
网络同步IO模型——多路复用(IO Multiplexing)
带你走进MySQL MVCC的世界
SIP账号的作用-告诉你什么是SIP线路
【零基础玩转BLDC系列】基于霍尔传感器的无刷直流电机控制原理
发现了一个好用到爆的数据分析利器
以后面试官问你 为啥不建议使用Select *,请你大声回答他!
买reits基金一定赚钱吗 开户安全吗
canal实现Mysql数据同步
Kali 2022.2 installation
发生了一次 MySQL 误操作导致的事故,「高可用」都顶不住了









