如何通过其边界获取场景中所有对象的屏幕截图

时间:2015-11-25 04:34:41

标签: unity3d camera

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

如何根据模型的边界(BoxCollider)获取摄像机视图的屏幕截图?

这是我的模特

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用此代码段,我用注释解释代码:

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);

enter image description here