版权声明:本文为博主原创文章,转载请表明来源和网址。 https://blog.csdn.net/thinbug/article/details/72769805
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;
}
}
}