【问题标题】:Unity Texture2D.ReadPixels() producing grey textureunity Texture2D.ReadPixels() 产生灰色纹理
【发布时间】:2016-08-30 21:40:19
【问题描述】:

这是我的代码:

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

【问题讨论】:

  • 能否提供在 UI 元素上应用纹理的代码?
  • 我相信这是问题所在显示。 GUI.DrawTexture (new Rect (cursor, GUIConstanst.flatRect), sigTexture, ScaleMode.ScaleToFit);
  • 如果以下答案有帮助,请考虑勾选答案。谢谢

标签: unity3d texture2d


【解决方案1】:

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

【讨论】:

  • 非常感谢。这让我发疯了。现在我查看 Apply() 的文档,这一切都说得通。普通内存中的纹理正在更新,因此 .EncodeToPNG() 可以工作,但它没有被移动到 GPU 内存中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 2023-03-19
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多