如何通过脚本切片精灵?(不使用编辑器) [英] How to slice sprite by script ?(not use Editor)
问题描述
我试图通过脚本切片Sprite(类型转换为Texture2D)
I trying to slice Sprite(type casted to Texture2D) by script,
当项目在Android或IOS平台上运行时
when project is running on Android or IOS Platform
可以通过脚本吗?
我尝试使用UnityEditor类,并且可以在计算机上使用
I trying to use UnityEditor Class and it is work on Computer
但是当我尝试构建Android或IOS时失败.
but When I trying to Build Android or IOS It is failed.
void OnPreprocessTexture()
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.textureType = TextureImporterType.Sprite;
textureImporter.spriteImportMode = SpriteImportMode.Multiple;
textureImporter.mipmapEnabled = false;
textureImporter.filterMode = FilterMode.Point;
}
public void OnPostprocessTexture(Texture2D texture)
{
Debug.Log("Texture2D: (" + texture.width + "x" + texture.height + ")");
int spriteSize = 350;
int colCount = texture.width / spriteSize;
int rowCount = texture.height / spriteSize;
List<SpriteMetaData> metas = new List<SpriteMetaData>();
for (int r = 0; r < rowCount; ++r)
{
for (int c = 0; c < colCount; ++c)
{
SpriteMetaData meta = new SpriteMetaData();
meta.rect = new Rect(c * spriteSize, r * spriteSize, spriteSize, spriteSize);
meta.name = c + "-" + r;
metas.Add(meta);
}
}
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.spritesheet = metas.ToArray();
}
public void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
{
Debug.Log("Sprites: " + sprites.Length);
}
在Android或IOS上运行项目时不起作用
It is not working When running project on Android or IOS
[我想要什么]
程序
在Android或IOS平台上运行期间
During running on Android or IOS Platform
1)从服务器(URL或文件)接收一些图像
1) Receive some Images from server (Url or file)
2)在C#脚本上加载图片
2) Load Image on C# script
3)将图像类型更改为纹理"或精灵" ...
3) Change type Images to Texture or Sprite ect...
4)切片图像(不要使用编辑器)
4) Slice Images(Don't use Editor)
5)保存图像
6)使用一张图片
我想要的是脚本执行的所有过程
What I want is all procedure worked by Script
推荐答案
TextureImporter
属于UnityEditor
名称空间,该名称空间在构建的应用程序中不存在,而仅在Unity Editor自身内部存在. &右箭头;您不能使用它!
TextureImporter
belongs to the UnityEditor
namespace which doesn't exist in a built app but only within the Unity Editor itself. → You can not use this!
您可以使用 Sprite.Create
生成一个精灵来自给定的Texture2D
.
如果实际上仅是要切除一部分纹理以将其用作精灵,则只需在rect
参数中定义要从该图像中使用的部分纹理即可.
If it is actually only about cutting out a certain part of the texture to use it as sprite than you only need to define in the rect
parameter the part of the texture you want to use from that image.
// Wherever you get the texture from
Texture texture = ...;
// E.g. for using the center of the image
// but half of the size
var rect = new Rect(texture.width / 4, texture.height / 4, texture.width / 2, texture.height / 2);
// Create the sprite
var sprite = Sprite.Create(texture, text, Vector2.one * 0.5f);
其中 rect
是
精灵在原始纹理上的位置,以像素为单位.
Location of the Sprite on the original Texture, specified in pixels.
切片
如果您还需要切片边界(通常在Unity中的Sprite Editor
中定义),则Sprite.Create
的重载将另外需要一个border
参数,例如
Slicing
If you additionally want a slicing border (which you usually define in the Sprite Editor
within Unity) there is an overload of Sprite.Create
that additionally takes a border
parameter e.g.
var borders = new Vector4(2, 2, 2, 2);
var sprite = Sprite.Create(texture, rect, Vector2.one * 0.5f, 100, SpriteMeshType.FullRect, borders);
其中 border
返回精灵的边框大小.
Returns the border sizes of the sprite.
X =左,Y =下,Z =右,W =上
X=left, Y=bottom, Z=right, W=top.
API没有说出来,但我想像rect
值一样,这些值也以像素为单位.
API doesn't say it but I guess like the rect
values those values are also in pixels.
这篇关于如何通过脚本切片精灵?(不使用编辑器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!