Making textures from screen content

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

Getting ready

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.

How to do it...

To create textures from screen content, follow these steps:

  1. Import the Unity package and open the 02_04_05 scene.
  2. We need to create a script. In the Project view, click on the Create drop-down menu and choose C# Script. Rename it ScreenTexture and open it in your editor.
  3. Open your script and replace everything with the following code:
    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;
        }
    }
  4. Save your script and apply it to the Main Camera game object.
  5. In the Inspector view, change the values for the Screen Capture component, leaving Photo Width and Photo Height as 25 and Thumb Proportion as 75, as shown here:
    How to do it...
  6. Play the scene. You will be able to take a snapshot of the screen (and have it displayed on the top-left corner) by clicking the mouse button.
    How to do it...

How it works...

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.

There's more...

Apart from displaying the texture as a GUI element, you could use it in other ways.

Applying your texture to a material

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.

Using your texture as a screenshot

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.186