image - 以编程方式在图像中加载纹理并为图像设置边框 Unity

标签 image unity3d border

我正在开发一款无尽的游戏,想在玩家死亡时拍摄快照。我几乎已经使用 Texture2D 做到了。我已经以编程方式在图像中加载纹理。但想为图像设置边框。我怎样才能做到这一点。?我如何在运行时为该图像设置边框?

此代码用于在我的播放器死机时在运行时将纹理加载到图像。

void LoadImage(){

    byte[] bytes = File.ReadAllBytes (Application.dataPath +"/GameOverScreenShot" + "/BirdDiedScreenShot.png");
    Texture2D texture = new Texture2D (900, 900, TextureFormat.RGB24, false);
    texture.filterMode = FilterMode.Trilinear;
    texture.LoadImage (bytes);
    Sprite sprite = Sprite.Create (texture, new Rect (0, 0, 700, 380), new Vector2 (0.5f, 0.0f), 1.0f);
    imgObject.GetComponent<UnityEngine.UI.Image> ().sprite = sprite;


} 

我想在运行时为该图像设置边框。任何人都可以帮助我真的很感激。提前致谢。

最佳答案

在将图像加载到 Texture2D 变量后执行此操作,这会将图像的边框更改为您想要的任何颜色。

 //color should be a variable that holds the color of the border you want.

for (int i = 0; i< texture.width; i++){
    texture.SetPixel(i, 0, color); //top border
    texture.SetPixel(i, texture.height - 1, color); //bottom border
}

for (int j = 0; j < texture.height; j++){
    texture.SetPixel(0, j, color); // left border
    texture.SetPixel(texture.width - 1, j, color); //right border   
}
texture.Apply();

这将替换原始图像边缘的任何像素,因此如果您需要这些边缘像素,则需要寻找其他解决方案。此外,texture.Apply 需要一段时间才能运行,因此如果您需要不断应用此边框,您可能会遇到减速,但您提到只有当玩家死亡时才会出现这种情况,所以这应该不是问题。

关于image - 以编程方式在图像中加载纹理并为图像设置边框 Unity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35643553/

相关文章:

c# - 光子 bolt |创建后将房间设为私有(private)

javascript - 从 Javascript 检测真正的边框、填充和边距

qt - 如何防止在 QLineEdit 焦点上绘制默认的蓝色边框

html - 边框与框对齐

HTML - Bootstrap - 如何裁剪图像的中心部分

image - 将内部透明像素转换为白色像素

android - Unity创建移动图案背景

c# - 删除其中一个 using 语句后仍然存在不明确的 void 名称

ios - 是否可以在UIImageView中使用aspect fill content mode结合top content mode?

image - 转置矩阵/难以理解 bsxfun 的工作原理