我想在运行时将精灵的二维数组变成一个精灵图像。所有的精灵都是正方形的,大小完全相同,但由于阵列的宽度和高度可能会有所不同,因此生成的图像不一定需要为正方形。Unity2D组合精灵
我还没有发现这个资源:Combine Array of Sprite objects into One Sprite - Unity
但我不认为它适合我的目的。
我想在运行时将精灵的二维数组变成一个精灵图像。所有的精灵都是正方形的,大小完全相同,但由于阵列的宽度和高度可能会有所不同,因此生成的图像不一定需要为正方形。Unity2D组合精灵
我还没有发现这个资源:Combine Array of Sprite objects into One Sprite - Unity
但我不认为它适合我的目的。
如果你在你的项目已经可以简单地编辑自己的进口设置高级和检查读那些精灵/写使能切换。
那么你应该能够读取你的精灵内容并将其合并这样的:
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);
}
警告:未测试的代码
要知道,这样的操作是沉重和异步做在协同程序中可能是避免冻结的一个好主意。
编辑:
因为你似乎新的堆栈溢出,请记住它不是提供服务的脚本,人们来这里是为了帮助对方:这意味着提供的代码不会永远是完美的,但可能只是引导你到正确的道路(这也是为什么我加了“警告:代码未经测试”后我的代码)。
你声称代码“完全破碎”并且“在所有地方输出错误”。我写了一小块脚本的测试脚本和我唯一的错误是一个(同意它的弹起多次):所以,在谷歌搜索之后
(你应该什么)我注意到有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);
}
}
如果有任何答案都不能解决您的问题,请考虑接受它是正确的。 _(接受答案有助于未来的访客进入本页)_ – Kardux