将Sprite对象数组合并为一个Sprite-Unity [英] Combine Array of Sprite objects into One Sprite - Unity

查看:692
本文介绍了将Sprite对象数组合并为一个Sprite-Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Unity中有一个Sprite对象数组.它们的大小取决于所加载的图像. 我想将它们像平铺的地图一样并排合并为一个图像. 我希望它们的布局像您正在形成一排又一排的图像. (注意:不要一个放在另一个之上) 我怎么能做到这一点?

I have an array of Sprite objects in Unity. Their size vary depending on the image loaded. I want to combine them side by side like a tiled map into one image. I want them to be layout like your are forming a line of images, one after the other. (note: NOT one on top of the other) How may I be able to do this?

之所以要合并(仅针对那些想知道的人),是因为我使用的是polygon2D Collider.由于当我并排使用多个对撞机时会发生一些奇怪的行为,因此我决定只合并图像,然后再添加一个大的多边形对撞机.请注意,这些事情在运行时发生.我不能只创建一个大图像并加载它,因为图像的顺序仅在运行时确定.

The reason why I'm combining (just for those who want to know) is because I'm using a polygon2D Collider. Since there are some weird behaviors happening when I use multiple colliders side by side, I decided to just combine the images before adding one big polygon collider. Note that these things happen during runtime. I can't just create a big image and load that because the order of the images is only determined at runtime.

我希望能对此有所帮助.谢谢.

I hope to receive some help with this. Thanks.

推荐答案

PackTextures方法在Texture2D类中,但是由于它使地图集变成正方形,因此您无法制作一排精灵,因此还有另一种方法可以通过读取图像像素并将其设置为新图像来完成,这样做确实很昂贵在运行时,但可以为您提供结果.

There is PackTextures method in Texture2D class, but since it makes your atlas square you can't make a line of sprites, so there is another way to do it by reading pixels of images and setting them to new image,it's really expensive to do in runtime but gives you the result.

// your textures to combine
// !! after importing as sprite change to advance mode and enable read and write property !!
public Sprite[] textures;
// just to see on editor nothing to add from editor
public Texture2D atlas;
public Material testMaterial;
public SpriteRenderer testSpriteRenderer;

int textureWidthCounter = 0;
int width,height;
void Start () {
    // determine your size from sprites
    width = 0;
    height = 0;
    foreach(Sprite t in textures)
    {
        width += t.texture.width;
        // determine the height
        if(t.texture.height > height)height = t.texture.height;
    }

    // make your new texture
    atlas = new Texture2D(width,height,TextureFormat.RGBA32,false);
    // loop through your textures
    for(int i= 0; i<textures.Length; i++)
    {
        int y = 0;
        while (y < atlas.height) {
            int x = 0;
            while (x < textures[i].texture.width ){
                if(y < textures[i].texture.height){
                    // fill your texture
                    atlas.SetPixel(x + textureWidthCounter, y, textures[i].texture.GetPixel(x,y));
                }
                else {
                    // add transparency
                    atlas.SetPixel(x + textureWidthCounter, y,new Color(0f,0f,0f,0f));
                }
                ++x;
            }
            ++y;
        }
        atlas.Apply();
        textureWidthCounter +=  textures[i].texture.width;
    }
    // for normal renderers
    if(testMaterial != null)testMaterial.mainTexture = atlas;
    // for sprite renderer just make  a sprite from texture
    Sprite s = Sprite.Create(atlas,new Rect(0f,0f,atlas.width,atlas.height),new Vector2(0.5f, 0.5f));
    testSpriteRenderer.sprite = s;
    // add your polygon collider
    testSpriteRenderer.gameObject.AddComponent<PolygonCollider2D>();
}

这篇关于将Sprite对象数组合并为一个Sprite-Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆