我想采取一个二维的雪碧数组,并把它变成一个单一的雪碧图像在运行时。所有的精灵都是方形的,大小完全相同,但是最终的图像不一定是方形的,因为数组的宽度和高度可能不同。
我还没有找到这个资源:将雪碧对象阵列组合成一个雪碧-单位
但我觉得这不适合我的目的。
发布于 2017-03-02 09:52:01
如果项目中已经有这些精灵,您只需编辑它们的导入设置到Advanced,并检查读/写启用切换。
然后,您应该能够阅读您的精灵内容,并将它们合并如下:
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溢出很陌生,请记住它不是提供服务的脚本,人们在这里互相帮助:这意味着提供的代码并不总是完美的,但可能只是引导您找到正确的路径(这也是为什么我在代码后面添加了“警告:未经测试的代码”)。
您声称代码“完全被破坏”,并且“到处都是错误”。我写了一小块脚本来测试这个脚本,唯一的错误就是那个错误(同意它多次弹出):

因此,在Google上搜索它(您应该自己做的事情)之后,我注意到还有一些GetPixels32() / SetPixels32()方法也可以用来代替GetPixels() / SetPixels() (这是显示这种方法的第三和第五位结果)。通过简单地改变这一点,代码现在运行得完美无缺。
我得到的唯一问题是,精灵在纹理的左下角聚集在一起:我在这个问题上犯了一个小错误。在哪里不难找到:只要改变
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);
}
}https://stackoverflow.com/questions/42539257
复制相似问题