c# - 创建尺寸大小的 Sprite
问题描述
我有一个画廊场景,我想从 Persistence path 加载 PNG。
问题是我希望它们作为缩略图,它们不需要我加载完整大小的文件。
如何定义精灵的比例?创建精灵的相关行:
Sprite sp1 = Sprite.Create(texture1, new Rect(0, 0, texture1.width, texture1.height), new Vector2(0.5f, 0.5f), 100, 0, SpriteMeshType.FullRect);
以及纹理创建代码:
Texture2D takeScreenShotImage(string filePath)
{
Texture2D texture = null;
byte[] fileBytes;
if (File.Exists(filePath))
{
fileBytes = File.ReadAllBytes(filePath);
texture = new Texture2D(1, 1, TextureFormat.ETC2_RGB, false);
texture.LoadImage(fileBytes);
}
return texture;
}
解决方案
进行更改的正确位置是在Texture2D
加载纹理之后。如果您真的想将它们作为缩略图加载并且希望尺寸更小以节省内存,请Texture2D.Resize
在加载纹理后使用该功能调整其大小。60的高度和宽度应该没问题。然后,您可以使用Sprite.Create
.
Texture2D takeScreenShotImage(string filePath)
{
Texture2D texture = null;
byte[] fileBytes;
if (File.Exists(filePath))
{
fileBytes = File.ReadAllBytes(filePath);
texture = new Texture2D(1, 1, TextureFormat.ETC2_RGB, false);
texture.LoadImage(fileBytes);
}
//RE-SIZE THE Texture2D
texture.Resize(60, 60);
texture.Apply();
return texture;
}
注意TextureFormat.ETC2_RGB
是压缩的。要调整它的大小,您可能必须创建它的副本。请参阅我的另一篇文章中的#1 解决方案,了解如何执行此操作。
推荐阅读
- linux - 如何将用户 www-data 写入权限分配给用户文件夹而不是用户分配给其他用户文件夹?
- vb.net - VB.NET模拟按钮点击,如何恢复正常?
- angular - 过滤管角度 - 多个参数
- arrays - Matlab上矩阵中矩阵的平均值
- python - 我的布尔 if 语句出错(Django)
- scala - 调用者任务如何避免它的调度程序被其 flatMap 结束的子任务更改
- dailymotion-api - 在 dailymotion api 上上传视频时缩略图 URL 无效
- neo4j - 如何使用 neo4j 高效查询深度链接节点
- firebase - Firebase RTDB:使用 CLI 中的旧数据更新/合并新数据
- python - Tensorflow Objection Detection API:访问给定边界框的所有类分数?