微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

unity texture2d getPixel 返回错误颜色

如何解决unity texture2d getPixel 返回错误颜色

我正在加载 png 作为纹理:

 byte[] bytes = File.ReadAllBytes(path);
 Texture2D texture = new Texture2D(1,1);
 texture.LoadImage(bytes);

问题在于 texture.GetPixel(23,23) 检索到的像素

在调试中好像是白色的,调试日志:texture.GetPixel(23,23) "RGBA(1.000,1.000,0.000)" UnityEngine.Color

但根据我在图像中看到的,它应该是一种蓝色:

enter image description here

我不知道如何获得该像素的正确值。 我在这里放了带有纹理的照片,以及我从像素颜色中获得的绘图

enter image description here

enter image description here

使用的代码

public void Justtest()
{
    ClearGrid();
    byte[] bytes = File.ReadAllBytes(path);
    Texture2D texture = new Texture2D(1,1);
    texture.LoadImage(bytes);
    for (int i = 0; i <= texture.width; i++)
    {
        for (int j = 0; j <= texture.height; j++)
        {
            GameObject sa = Instantiate(_testTilePrefab,new Vector3(i,0.2f,j),_testTilePrefab.transform.rotation);
            sa.GetComponent<Renderer>().material.color = texture.GetPixel(i,j);
        }
    }
}

解决方法

很可能是 Y 轴索引问题。 可能是上下翻转问题。

编辑: 加载字节 [] 后,您缺少“texture.Apply()”。没有应用功能,你的纹理默认仍然是白色的。

texture.Apply();
,

根据您在 Texture Import Settings -> Platform specific overrides -> MaxSize

底部配置的内容,该问题很可能会降低纹理的分辨率

以像素为单位设置最大导入纹理尺寸 .艺术家通常更喜欢使用大尺寸的纹理,但您可以将纹理缩小到合适的尺寸。

对于您的瓷砖场景,我几乎可以肯定您的瓷砖太大且彼此重叠,因此图像看起来很奇怪。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。