Unity 通过Texture生成的Sprite存在边缘黑线问题 Image黑边问题
Color32[] pixels = downLoadTexture.GetPixels32();
int height = downLoadTexture.height;
int width = downLoadTexture.width;
int borderWidth = 2; // 边界宽度
for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {// 检查是否在边界上if (x < borderWidth || y < borderWidth || x >= width - borderWidth || y >= height - borderWidth) {int index = y * width + x; // 计算当前像素的索引pixels[index].a = 0; // 将Alpha值设为0,即设为透明}}
}
downLoadTexture.SetPixels32(pixels);
downLoadTexture.alphaIsTransparency = true;
downLoadTexture.Apply();
将texture的边缘部分改为透明,边缘宽度我设置是2,如果太大可以改为1,之后再进行用这个Texture去创建Sprite
Sprite sprite = Sprite.Create(downLoadTexture, new Rect(0, 0, downLoadTexture.width, downLoadTexture.height), new Vector2(0.5f, 0.5f));
如果还不行,Image再进行
img.type = Image.Type.Tiled; img.pixelsPerUnitMultiplier = 0.999f;