如何在 Unity3d 中将纹理格式从 Alpha8 更改为 RGBA?

How to change texture format from Alpha8 to RGBA in Unity3d?

我一直在尝试将在 Alpha8 中提供纹理的相机的格式更改为 RGBA 并且到目前为止没有成功.

这是我试过的代码:

   public static class TextureHelperClass
    {
        public static Texture2D ChangeFormat(this Texture2D oldTexture, TextureFormat newFormat)
        {
            //Create new empty Texture
            Texture2D newTex = new Texture2D(2, 2, newFormat, false);
            //Copy old texture pixels into new one
            newTex.SetPixels(oldTexture.GetPixels());
            //Apply
            newTex.Apply();

            return newTex;
        }
    }

我这样调用代码:

  Texture imgTexture = Aplpha8Texture.ChangeFormat(TextureFormat.RGBA32);

但图像已损坏且不可见。

有谁知道如何将 Alpha8 更改为 RGBA 以便我可以像在 OpenCV 中处理其他图像一样处理它?

朋友给我的答案:

    Color[] cs =oldTexture.GetPixels();
    for(int i = 0; i < cs.Length; i++){//we want to set the r g b values to a
        cs[i].r = cs[i].a;
        cs[i].g = cs[i].a;
        cs[i].b = cs[i].a;
        cs[i].a = 1.0f;                                                
    }
    //set the pixels in the new texture
    newTex.SetPixels(cs);
    //Apply
    newTex.Apply();

这将占用大量资源,但肯定会奏效。 如果您知道进行此更改的更好方法,请在此主题中添加答案。