2015-11-25 18 views
1

我通過vertics數據在Unity3d中生成了一個模型。我創建了BoxCollider,它包含模型的所有遊戲對象。如何通過界限獲取場景中所有對象的屏幕截圖

如何根據模型的邊界(BoxCollider)獲取相機視圖的截圖?

這裏是我的模型

enter image description here

+0

歡迎堆棧溢出!爲了將來的參考,請確保你的問題實際上包含一個問題。 – APC

回答

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

+0

此代碼給我黑色圖像( –

+0

變化textureWidth,texturHeight,X,Y值,以這些: 'INT textureWidth = Screen.width;'和 'INT texturHeight = Screen.height;'和 '浮X = 0F; '和' 浮Y = 0F;' 你有沒有像現在 –

+0

是的,也 我的代碼: VAR textureWidth = Screen.width; VAR texturHeight = Screen.height; 浮動X = 0; 浮動? Y = 0; 的Texture2D screenTexture =新的Texture2D((int)的textureWidth,(INT)texturHeight,TextureFormat.RGB24,FALSE); screenTexture.ReadPixels(新的Rect(0,0,textureWidth,texturHeight),0,0); screenTexture.Apply(); byte [] bytes = screenTexture.EncodeToPNG(); string filename =「Assets/img/Save.png」; System.IO.File.WriteAllBytes(filename,bytes); –

相關問題