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

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

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

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

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

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

.

接着创建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,如下图。

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

 

转载于:https://www.cnblogs.com/martianzone/p/3292695.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值