Unity Texture2D.ReadPixels()生成灰色纹理

时间:2016-05-05 07:26:41

标签: unity3d texture2d

这是我的代码:

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中使用它或在检查器中查看它时,我留下了一个均匀灰色的空白纹理。 奇怪的是,生成的文件是正确的。

1 个答案:

答案 0 :(得分:4)

试试这个。我添加了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 ();