【问题标题】:Unity texture2d getPixel return wrong colorunity texture2d getPixel 返回错误的颜色
【发布时间】:2021-10-01 10:16:56
【问题描述】:

我正在将 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, 1.000, 0.000)" UnityEngine.Color

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

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

使用的代码:

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);
        }
    }
}

【问题讨论】:

    标签: image unity3d png pixel texture2d


    【解决方案1】:

    根据您在Texture Import Settings -> Platform specific overrides -> MaxSize 底部配置的内容,问题很可能会降低纹理的分辨率

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

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

    【讨论】:

      【解决方案2】:

      可能是 Y 轴索引问题。 可能是自上而下、自下而上的翻转问题。

      编辑: 加载 byte[] 后,您缺少“texture.Apply()”。如果没有 apply 功能,您的纹理默认仍为白色。

      texture.Apply();
      

      【讨论】:

      • 嗨,不是这样的,我将编辑我的问题以准确查看。
      • 您在加载 byte[] 后缺少“texture.Apply()”。如果没有 apply 功能,您的纹理默认仍为白色。
      猜你喜欢
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      相关资源
      最近更新 更多