当前位置:网站首页>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 .
边栏推荐
- What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?
- 发生了一次 MySQL 误操作导致的事故,「高可用」都顶不住了
- 元宇宙并不是一个像互联网一样将流量看成是终极追求的存在
- 基于共轭梯度法的对抗攻击
- 198. 打家劫舍
- 实操演练 | MySQL PROCESSLIST 表和 Navicat Monitor 识别慢速查询的简单方法
- wireshark抓包工具基本使用
- How do test / development programmers break through? All roads lead to Rome
- The most detailed explanation of the output of three numbers from large to small
- K3S - 轻量级Kubernetes集群
猜你喜欢

UGUI源码解析——IMaterialModifier

MGRE 网络的构建

OSI七层模型有哪七层?每一层分别有啥作用,这篇文章讲的明明白白!

测试/开发程序员如何突破?条条大路通罗马......

ADB 命令结合 monkey 的简单使用,超详细

Network synchronous IO model - IO multiplexing

Construction of mGRE network

K3s - lightweight kubernetes cluster

Print prime numbers between 100 and 200

Ali II: why do MySQL indexes use b+ trees instead of jump tables?
随机推荐
1.5万字概括ES6全部特性
BGP实验
UGUI源码解析——Mask
Family fraud is prevalent, and Sogou number builds a security firewall
User defined type details: structure, enumeration, union
Idea export jar package to JMeter
[openvx] VX for basic use of objects_ reference
股票开户网上开户安全吗,银河证券怎么样
数论 —— 整除分块,常见经典例题。
50道经典计算机网络面试题,你答得上几个?(三)
深度讲解CAS,面试实践必备
NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南
数据可视化平台的下一站 | 来自国产开源数据可视化 datart「超级铁粉」的馈赠
Must I make money by buying REITs funds? Is it safe to open an account
Geely Xingrui: from product technology empowerment to cultural confidence
Unity3D学习笔记9——加载纹理
Is it safe for CICC fortune to open an account? How to buy stocks
股票开户网上开户安全吗,银河证券怎么样
UGUI源码解析——StencilMaterial
打印100~200之间的素数