Unity截图

来源:互联网 发布:怎样卸载windows游戏 编辑:程序博客网 时间:2024/05/18 12:32
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class CCOpyText : MonoBehaviour {    public Text txt;    public MeshRenderer r;    public Camera txtCamera;    public int picSize = 256;    void Start () {    }    Texture2D copytext()    {        RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);        txtCamera.targetTexture = rt;        Texture2D screenShot = new Texture2D(picSize, picSize, TextureFormat.RGB24, false);        txtCamera.Render();        RenderTexture.active = rt;        screenShot.ReadPixels(new Rect((int)(Screen.width / 2 - picSize / 2), (int)(Screen.height / 2 - picSize / 2), picSize, picSize), 0, 0);        screenShot.Apply();        txtCamera.targetTexture = null;        RenderTexture.active = null;         Destroy(rt);        byte[] bytes = screenShot.EncodeToPNG();        string filename = "1.png";        System.IO.File.WriteAllBytes(filename, bytes);        return screenShot;    }    void OnGUI()    {        if (GUI.Button(new Rect(0, 10, 100, 30), "click"))        {            Texture2D t = copytext();            r.material.mainTexture = (Texture)t;        }    }}