unity Texture2D.ReadPixels() 产生灰色纹理

Unity Texture2D.ReadPixels() producing grey texture

这是我的代码:

sigTex = new Texture2D ((int)dims.x, (int)dims.y);

sigTex.ReadPixels (new Rect (new Vector2(0.0f, Screen.height - bot) , dims), 0, 0);

byte[] pngBytes = sigTex.EncodeToPNG ();

System.IO.BinaryWriter bw= new System.IO.BinaryWriter(new System.IO.FileStream(filename, System.IO.FileMode.Create));
bw.Write (pngBytes, 0, pngBytes.Length);
bw.Close ();

问题是,在这段代码之后,当我尝试在 GUI 中使用它或在检查器中查看它时,我留下了一个空白纹理,它是均匀的灰色。 奇怪的是生成的文件是正确的

试试这个。我添加了 texture.Apply();

http://docs.unity3d.com/ScriptReference/Texture2D.Apply.html

sigTex = new Texture2D ((int)dims.x, (int)dims.y);

sigTex.ReadPixels (new Rect (new Vector2(0.0f, Screen.height - bot) , dims), 0, 0);

sigTex.Apply();

byte[] pngBytes = sigTex.EncodeToPNG ();

System.IO.BinaryWriter bw= new System.IO.BinaryWriter(new System.IO.FileStream(filename, System.IO.FileMode.Create));
bw.Write (pngBytes, 0, pngBytes.Length);
bw.Close ();