【问题标题】:How get screenshot of all objects in scene by its bounds如何按边界获取场景中所有对象的屏幕截图
【发布时间】:2015-11-25 04:34:41
【问题描述】:

我在 Unity3d 中通过顶点数据生成了一个模型。我创建了 BoxCollider,其中包含模型的所有游戏对象。

如何根据模型的边界(BoxCollider)获取相机视图的截图?

这是我的模型

【问题讨论】:

  • 欢迎来到 Stack Overflow!为了将来参考,请确保您的问题确实包含问题。

标签: unity3d camera


【解决方案1】:

你可以用这个sn-p,我用注释解释代码:

int textureWidth = GameObject.find("UnicGroups").GetComponent<BoxCollider>.size.x; // Width of photo
int texturHeight = GameObject.find("UnicGroups").GetComponent<BoxCollider>.size.y; // Height of photo

// You can add EmptyGameObject to your model
// and move that game object to upper left corner of
// your model. Then you can replace x and y value
// with position of that game object
float x = GameObject.find("UnicGroups/YourEmptyGameObject").transform.Position.x; // x position of upper left corner of screenshot
float y = GameObject.find("UnicGroups/YourEmptyGameObject").transform.Position.y; // Y position of upper left corner of screenshot

// create the texture
Texture2D screenTexture = new Texture2D(textureWidth, texturHeight,TextureFormat.RGB24,true);
screenTexture.ReadPixels(new Rect(x, y, textureWidth, texturHeight),0,0);
screenTexture.Apply();

// Save image to file
byte[] dataToSave = screenTexture.EncodeToPNG();
// Set destination path for saving file
string destination = Path.Combine(Application.persistentDataPath,System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");
File.WriteAllBytes(destination, dataToSave);

【讨论】:

  • 将 textureWidth、texturHeight、x、y 值更改为:int textureWidth = Screen.width;int texturHeight = Screen.height;float x = 0f; float y = 0f; 你现在有图像吗?
  • 是的,也是。我的代码: var textureWidth = Screen.width; var texturHeight = Screen.height;浮动 x = 0;浮动 y = 0; Texture2D screenTexture = new Texture2D((int)textureWidth, (int)texturHeight,TextureFormat.RGB24, false); screenTexture.ReadPixels(new Rect(0, 0, textureWidth, texturHeight), 0, 0); screenTexture.Apply(); byte[] bytes = screenTexture.EncodeToPNG();字符串文件名=“资产/img/Save.png”; System.IO.File.WriteAllBytes(filename, bytes);
  • 这是您的相机尺寸的屏幕截图,要获得您的对象尺寸的屏幕截图,您需要将textureWidthtexturHeight更改为您的模型的宽度和高度并更改@987654330 @ 和 y 到模型左上角的位置。如果有帮助,请将其标记为答案。
  • 我确实设置了 textureWidth = 我的模型的宽度和 textureHeight = 我的模型的高度,购买我得到黑屏。还有textureWidth = Screen.width,textureHeight = Screen.height - 屏幕太黑。我被卡住了(
  • 你也设置float x = 0float y =0吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多