Render Texture的使用(截取rendertexture的一帧到Texture2D)

游戏里人物角色太多,每个角色都要有张头像或全身照等,这样就必须截取大量的图片,花费大量的时间,有时截取的不满意还得重新截,即浪费时间又浪费精力.所以就想了个投机取巧的方法.那就是用unity搭建一个照相馆.

首先用CreateEmpty创建一个空的GameObject,命名为GetPicsObject,将他的Transform.position置为0;

接着创建一个Camera,命名为ShowCamera,把这个Camera放到上面GetPicsObject下,作为GetPicsObject的子物体.

接着用CreateEmpty创建另一个空的GameObject,命名为charactorPos,用来确定创建角色所在的位置,也放到GetPicsObject下,作为GetPicsObject的子物体.

调整Camera的位置,确保相机视图为你想要截取照,层次如下图所示

.Render Texture的使用(截取rendertexture的一帧到Texture2D)_第1张图片

接着创建C#脚本,命名为GetPicTest

 

using UnityEngine; using System.Collections; using System.Collections.Generic; public class GetPicTest : MonoBehaviour { /// <summary>
    /// 创建角色所在位置 /// </summary>
    public Transform playerPos; /// <summary>
    /// 声明一个全身照的RenderTexture /// </summary>
    public RenderTexture fullBodyRender; /// <summary>
    /// 将fullBodyRender的特定一帧动画保存到fullBodyTex /// </summary>
    public Texture2D fullBodyTex; /// <summary>
    /// 全身像相机 /// </summary>
    public Camera showCamera; /// <summary>
    /// 保存角色预置 /// </summary>
    public List<GameObject> charactorsPrefabs = new List<GameObject>(); public int _width = 120; public int _height = 120; public GameObject obj; // Use this for initialization
    void Start() { fullBodyRender = new RenderTexture(_width, _height, 32); //fullBodyRender = RenderTexture.GetTemporary(_width, _height, 0, RenderTextureFormat.ARGBFloat);
           fullBodyRender.anisoLevel = 0; fullBodyRender.filterMode = FilterMode.Point; showCamera.targetTexture = fullBodyRender; showCamera.pixelRect = new Rect(0, 0, _width, _height); } void OnGUI() { GUI.DrawTexture(new Rect(Screen.width - _width, Screen.height - _height, _width, _height), fullBodyRender, ScaleMode.ScaleToFit);//全身
                int j = -1; for (int i = 0; i < charactorsPrefabs.Count; i++) { if (i % 10 == 0) { j++; } if (GUI.Button(new Rect(j*40,i%10*30,40,30),"" + charactorsPrefabs[i].name)) { if (obj) { Destroy(obj); } obj = GameObject.Instantiate(charactorsPrefabs[i]) as GameObject; obj.transform.parent = playerPos; obj.transform.localPosition = Vector3.zero; obj.animation.Play("idle"); obj.animation.wrapMode = WrapMode.Loop; CharacterController controller = obj.GetComponent<CharacterController>(); float h = controller.height; float scaleY = obj.transform.localScale.y; float multiNum = 1.5f / h / scaleY; obj.transform.localScale = new Vector3(obj.transform.localScale.x * multiNum, obj.transform.localScale.y * multiNum, obj.transform.localScale.z * multiNum); } } GUI.Box(new Rect(Screen.width - _width, 0, _width, _height),""); if (GUI.Button(new Rect(Screen.width / 2 - 70, 0, 140, 30), "GetPic")) { fullBodyTex = getTexture2d(fullBodyRender); } if (fullBodyTex) { GUI.DrawTexture(new Rect(Screen.width - _width, 0, _width, _height), fullBodyTex, ScaleMode.ScaleToFit); } } public Texture2D getTexture2d(RenderTexture renderT) { if (renderT == null) return null;
int width = renderT.width; int height = renderT.height; Texture2D tex2d = new Texture2D(width, height, TextureFormat.ARGB32, false); RenderTexture.active = renderT; tex2d.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex2d.Apply(); //byte[] b = tex2d.EncodeToPNG(); //Destroy(tex2d); //File.WriteAllBytes(Application.dataPath + "1.jpg", b); return tex2d; } }

 

将GetPicTest赋给到GetPicsObject,把charactor赋给Playerpos,把ShowCamera赋给Show Camera,如下图。
Render Texture的使用(截取rendertexture的一帧到Texture2D)_第2张图片

左边一排按钮选择角色,中心上方GetPic按钮截取图标显示在右上角.右下角为RenderTexture,如下图。

Render Texture的使用(截取rendertexture的一帧到Texture2D)_第3张图片

 

你可能感兴趣的:(text)