游戏里人物角色太多,每个角色都要有张头像或全身照等,这样就必须截取大量的图片,花费大量的时间,有时截取的不满意还得重新截,即浪费时间又浪费精力.所以就想了个投机取巧的方法.那就是用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 = ;
public int _height = ; public GameObject obj;
// Use this for initialization
void Start()
{
fullBodyRender = new RenderTexture(_width, _height, );
//fullBodyRender = RenderTexture.GetTemporary(_width, _height, 0, RenderTextureFormat.ARGBFloat);
   fullBodyRender.anisoLevel = ;
fullBodyRender.filterMode = FilterMode.Point;
showCamera.targetTexture = fullBodyRender;
showCamera.pixelRect = new Rect(, , _width, _height);
} void OnGUI()
{
GUI.DrawTexture(new Rect(Screen.width - _width, Screen.height - _height, _width, _height), fullBodyRender, ScaleMode.ScaleToFit);//全身
int j = -;
for (int i = ; i < charactorsPrefabs.Count; i++)
{
if (i % == )
{
j++;
}
if (GUI.Button(new Rect(j*,i%*,,),"" + 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, , _width, _height),""); if (GUI.Button(new Rect(Screen.width / - , , , ), "GetPic"))
{
fullBodyTex = getTexture2d(fullBodyRender);
}
if (fullBodyTex)
{
GUI.DrawTexture(new Rect(Screen.width - _width, , _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(, , width, height), , );
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,如下图。

Render Texture的使用(截取rendertexture的一帧到Texture2D)的更多相关文章

  1. 利用Render Texture实现游戏的小雷达效果(摄影机分屏)

    最近游戏蛮牛在举办一个活动,就是要做出这样的效果: 题目:实现游戏分屏效果 要求:1.        分屏,且分割线不规则(即非水平或垂直):2.        各屏可单独操作(移动.缩放),操作指该 ...

  2. camera render texture 游戏里的监控视角

    Camera里: 新建render texture并拖入到target texture里 新建材质球 拖入render texture      camera里的视角会在材质球上出现  新建一个pla ...

  3. Render Texture

    [Render Texture] Render Textures are special types of Textures that are created and updated at runti ...

  4. Render Texture coordinates

    https://docs.unity3d.com/550/Documentation/Manual/SL-PlatformDifferences.html Render Texture coordin ...

  5. iOS截取视频某一帧图片(关键帧,AVAssetImageGenerator)

    获取第一帧图片 导入 AVFoundation.Framework.CoreMedia.Framework 实现代码例如以下: + (UIImage*) thumbnailImageForVideo: ...

  6. 【Unity Shaders】游戏性和画面特效——创建一个夜视效果的画面特效

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  7. 【Unity Shaders】游戏性和画面特效——创建一个老电影式的画面特效

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  8. cocos源码分析--RenderTexture

    cocos中RenderTexture主要用来实现截屏,然后把截取出来的图片保存到磁盘中,除了保存图片和渲染纹理,它还可以得到一些预渲染结果,并将这些结果作为一种纹理数据. 例如我们可以用RGB5_A ...

  9. LittleTools之输出RenderTexture工具

    using UnityEngine; using System.Collections; using System.IO; /// <summary> /// Save render te ...

随机推荐

  1. codereview介绍

    1. 定义: Code review is systematic examination (often known as peer review) of computer source code. I ...

  2. Android(java)学习笔记128:使用proguard混淆android代码

    1.当前是有些工具比如apktool,dextojar等是可以对我们android安装包进行反编译,获得源码的.为了减少被别人破解,导致源码泄露,程序被别人盗取代码,等等.我们需要对代码进行混淆,an ...

  3. jQuery的自定义事件——滚轮

    这个案例类似于在地图上滚动滚轮,能缩小或者放大地图,分别用zoomIn和zoomOut来命名. 代码如下: //JS部分:<script src="jquery-1.10.2.min. ...

  4. Spring 4.2 annotation event Publisher/Listener

    http://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2 Better applicatio ...

  5. Android 自学之相对布局 RelativeLayout

    相对布局(RelativeLayout),相对布局容器内子组件的位置总是相对兄弟组件.父容器来决定的. RelativeLayout的XML属性及相关方法说明 XML属性 相关方法 说明 androi ...

  6. Xilinx 网站资源导

    Xilinx 网站资源导读 ———版权声明———–本文作者 Ricky Suwww.fpganotes.comrickysu.fpga@gmail.com 欢迎转载,转载请保持原样及署名商业使用须得到 ...

  7. hdu 1096 A+B for Input-Output Practice (VIII)

    A+B for Input-Output Practice (VIII) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  8. Spring之IOC容器初始化过程

    Ioc容器的初始化是由refresh()方法来启动的,这个方法标志着Ioc容器的正式启动. 具体来说这个启动过程包括三个基本过程: 1.BeanDifinition的Resource定位 2.Bean ...

  9. 项目开发中常用到的SQL语句

    阅读目录 循环示例 循环示例 循环示例 循环示例 1.循环示例 在前端面试中最常见的问题就是页面优化和缓存(貌似也是页面优化),被问了几次后心虚的不行,平然平时多少会用到一些,但突然问我,很难把自己知 ...

  10. String类中toCharArray()方法的用法

    该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符 eg:  public class class6_3 { public static void main(String arg ...