Unity2D结合精灵

时间:2017-03-01 18:27:13

标签: c# unity3d unity2d

我想在运行时使用Sprites的二维数组并将其转换为单个精灵图像。所有精灵都是正方形且大小完全相同,但所得到的图像不一定需要是正方形,因为阵列的宽度和高度可以变化。

我找到了这个资源:Combine Array of Sprite objects into One Sprite - Unity

但我不认为它适用于我的目的。

1 个答案:

答案 0 :(得分:1)

如果您的项目中有这些精灵,您只需将其导入设置编辑为高级,然后检查读/写启用切换。

然后你应该能够阅读你的精灵内容并合并它们:

public Sprite CombineSpriteArray(Sprite[][] spritesArray)
{
    // Set those two or get them from one the the sprites you want to combine
    int spritesWidth = (int)spritesArray[0][0].rect.width;
    int spritesHeight = (int)spritesArray[0][0].rect.height;

    Texture2D combinedTexture = new Texture2D(spritesWidth * spritesArray.Length, spritesHeight * spritesArray[0].Length);

    for(int x = 0; x < spritesArray.Length; x++)
    {
        for(int y = 0; y < spritesArray[0].Length; y++)
        {
            combinedTexture.SetPixels(x * spritesArray.Length, y * spritesArray[0].Length, spritesWidth, spritesHeight, spritesArray[x][y].texture.GetPixels((int)spritesArray[x][y].textureRect.x, (int)spritesArray[x][y].textureRect.y, (int)spritesArray[x][y].textureRect.width, (int)spritesArray[x][y].textureRect.height));
            // For a working script, use:
            // combinedTexture.SetPixels32(x * spritesWidth, y * spritesHeight, spritesWidth, spritesHeight, spritesArray[x][y].texture.GetPixels32());
        }
    }
    combinedTexture.Apply();

    return Sprite.Create(combinedTexture, new Rect(0.0f, 0.0f, combinedTexture.width, combinedTexture.height), new Vector2(0.5f, 0.5f), 100.0f);
}

警告:代码未经测试

请注意,此类操作很重,并且在协程中异步执行此操作可能是避免冻结的好主意。

编辑:

由于您似乎对Stack Overflow不熟悉,请记住它不是提供服务的脚本,人们在这里互相帮助:这意味着所提供的代码永远不会是完美的,但可能只是引导你走向正确的道路(这也是为什么我在我的代码之后添加&#34;警告:未经测试的代码&#34; )。

您声称代码完全被破坏了#34;并且&#34;在整个地方发出错误&#34;。我写了一小段脚本来测试脚本,我得到的唯一错误就是那个(同意它弹出多次):

所以在Google上搜索之后(你应该自己做什么),我注意到有GetPixels32() / SetPixels32()个方法也可以用来代替GetPixels() / SetPixels()(这里是3rd }和5th结果显示了这种方法)。通过简单地改变它,代码现在可以完美地运行。

我获得的问题只是精灵在纹理的左下方被打包在一起:我的不好,我犯了一个小错误。不难发现在哪里:只需改变
x * spritesArray.Length, y * spritesArray[0].Length, ...

x * spritesWidth, y * spritesHeight, ...
SetPixels方法中。

所以请找到我写的整个测试脚本并随意使用它:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TestScript : MonoBehaviour
{
    public Image m_DisplayImage;
    public Sprite m_Sprite1, m_Sprite2;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(CombineSpritesCoroutine());
        }
    }

    private IEnumerator CombineSpritesCoroutine()
    {
        Sprite[][] spritesToCombine = new Sprite[4][];
        for (int i = 0; i < spritesToCombine.Length; i++)
        {
            spritesToCombine[i] = new Sprite[4];
        }

        for (int x = 0; x < spritesToCombine.Length; x++)
        {
            for (int y = 0; y < spritesToCombine[x].Length; y++)
            {
                spritesToCombine[x][y] = ((x + y) % 2 == 0 ? m_Sprite1 : m_Sprite2);
            }
        }

        Sprite finalSprite = null;
        yield return finalSprite = CombineSpriteArray(spritesToCombine);

        m_DisplayImage.sprite = finalSprite;
    }

    public Sprite CombineSpriteArray(Sprite[][] spritesArray)
    {
        // Set those two or get them from one the the sprites you want to combine
        int spritesWidth = (int)spritesArray[0][0].rect.width;
        int spritesHeight = (int)spritesArray[0][0].rect.height;

        Texture2D combinedTexture = new Texture2D(spritesWidth * spritesArray.Length, spritesHeight * spritesArray[0].Length);

        for (int x = 0; x < spritesArray.Length; x++)
        {
            for (int y = 0; y < spritesArray[0].Length; y++)
            {
                combinedTexture.SetPixels32(x * spritesWidth, y * spritesHeight, spritesWidth, spritesHeight, spritesArray[x][y].texture.GetPixels32());
            }
        }
        combinedTexture.Apply();

        return Sprite.Create(combinedTexture, new Rect(0.0f, 0.0f, combinedTexture.width, combinedTexture.height), new Vector2(0.5f, 0.5f), 100.0f);
    }
}