游戏里人物角色太多,每个角色都要有张头像或全身照等,这样就必须截取大量的图片,花费大量的时间,有时截取的不满意还得重新截,即浪费时间又浪费精力.所以就想了个投机取巧的方法.那就是用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. 导入MyEclipse项目乱码

    1.右键项目属性→text file encoding →修改为UTF-8,即可自动变成正常的.

  2. 如何使用MVP模式搭建我们的Android应用?

    听到很多人在讨论MVVM,我自己早些时候也写过一篇介绍MVVM的文章(玩转Android之MVVM开发模式实战,炫酷的DataBinding!),实际上,在Android开发领域中,除了MVVM之外, ...

  3. nodeValue和innerHTML的比较

    <ul id="parent"> <li>1</li> <li>2</li> <li>3</li> ...

  4. js事件(Event)知识整理[转]

    事件注册 平常我们绑定事件的时候用dom.onxxxx=function(){}的形式 这种方式是给元素的onxxxx属性赋值,只能绑定有一个处理句柄. 但很多时候我们需要绑定多个处理句柄到一个事件上 ...

  5. gvim设置字体和隐藏菜单栏工具栏

    liunx下面设置字体 set guifont=Monaco\ 注意空格的位置,其他写法不认哦! Windows下面设置 set guifont=Monaco:h 隐藏菜单栏 set guioptio ...

  6. 服务器的SVN项目版本较低,check out 下来后报错

    check out下来后报错提示: svn: E155036: Please see the 'svn upgrade' commandsvn: E155036: Working copy '/hom ...

  7. Lodash Filter

    var persons = [{name:'1',age:'20'}, {name:'2', age:'25'}];_.filter(persons, {'age': '25'}); //return ...

  8. sqlserver 2012 重启是 ID 自动增长 1000的问题

    1. Open "SQL Server Configuration Manager"2. Click "SQL Server Services" on the ...

  9. web app开发中 iPhone、iPad默认按钮样式问题

    webapp开发过程中,用html5+css3很方便,而且可以很方便的编译到Android ios等不同平台,但是ios需要单独处理一下,不然会出现一些想象不到的问题.下面就介绍一下各种问题的解决方法 ...

  10. 使用Linq 来解决Datatable 去除数据重复

    在这也说明下,除此之外还可以通过 DataView 的方式来处理,当个人觉得这有点不好用.这里就不多作说明了 代码比较简单,直接看代码 using System; using System.Colle ...