If you want your game or player to take in-game snapshots and apply it as a texture, this recipe will show you how. This can be very useful if you plan to implement an in-game photo gallery or display a snapshot of a past key moment at the end of a level (Race Games and Stunt Sims use this feature a lot).
In order to follow this recipe, please import the basicTerrain
package, available in the 0423_02_04_05
folder, into your project. The package includes a basic terrain and a camera that can be rotated via a mouse.
To create textures from screen content, follow these steps:
02_04_05
scene.ScreenTexture
and open it in your editor.using UnityEngine; using System.Collections; public class ScreenTexture : MonoBehaviour { public int photoWidth = 50; public int photoHeight = 50; public int thumbProportion = 25; public Color borderColor = Color.white; public int borderWidth = 2; private Texture2D texture; private Texture2D border; private int screenWidth; private int screenHeight; private int frameWidth; private int frameHeight; private bool shoot = false; void Start (){ screenWidth = Screen.width; screenHeight = Screen.height; frameWidth = Mathf.RoundToInt(screenWidth * photoWidth * 0.01f); frameHeight = Mathf.RoundToInt(screenHeight * photoHeight * 0.01f); texture = new Texture2D (frameWidth,frameHeight,TextureFormat.RGB24,false); border = new Texture2D (1,1,TextureFormat.ARGB32, false); border.SetPixel(0,0,borderColor); border.Apply(); } void Update (){ if (Input.GetKeyUp(KeyCode.Mouse0)) StartCoroutine(CaptureScreen()); } void OnGUI (){ GUI.DrawTexture(new Rect((screenWidth*0.5f)-(frameWidth*0.5f) - borderWidth*2,((screenHeight*0.5f)-(frameHeight*0.5f)) - borderWidth,frameWidth + borderWidth*2,borderWidth),border,ScaleMode.StretchToFill); GUI.DrawTexture(new Rect((screenWidth*0.5f)-(frameWidth*0.5f) - borderWidth*2,(screenHeight*0.5f)+(frameHeight*0.5f),frameWidth + borderWidth*2,borderWidth),border,ScaleMode.StretchToFill); GUI.DrawTexture(new Rect((screenWidth*0.5f)-(frameWidth*0.5f)- borderWidth*2,(screenHeight*0.5f)-(frameHeight*0.5f),borderWidth,frameHeight),border,ScaleMode.StretchToFill); GUI.DrawTexture(new Rect((screenWidth*0.5f)+(frameWidth*0.5f),(screenHeight*0.5f)-(frameHeight*0.5f),borderWidth,frameHeight),border,ScaleMode.StretchToFill); if(shoot){ GUI.DrawTexture(new Rect (10,10,frameWidth*thumbProportion*0.01f,frameHeight*thumbProportion* 0.01f),texture,ScaleMode.StretchToFill); } } IEnumerator CaptureScreen (){ yield return new WaitForEndOfFrame(); texture.ReadPixels(new Rect((screenWidth*0.5f)-(frameWidth*0.5f),(screenHeight*0.5f)-(frameHeight*0.5f),frameWidth,frameHeight),0,0); texture.Apply(); shoot = true; } }
Clicking the mouse triggers a function that reads pixels within the specified rectangle and applies them into a texture that is drawn by the GUI.
Apart from displaying the texture as a GUI element, you could use it in other ways.
You apply your texture to an existing object's material by adding a line similar to GameObject.Find("MyObject").renderer.material.mainTexture = texture;
at the end of the CaptureScreen
function.
You can encode your texture as a PNG image file and save it. Check out Unity's documentation on this feature at http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.EncodeToPNG.html.
18.220.88.62