gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:09:32 25 4
gpt4 key购买 nike

我正在开发一款无尽的游戏,想在玩家死亡时拍摄快照。我几乎已经使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com