组件功能

把3D角色的动画录制成PNG一帧一帧输出,这是一个件多么美好的事!

可能遇到问题

有可能当你新建完脚本时会出现下面的错误:

`System.IO.File' does not contain a definition for `WriteAllBytes'

解决办法:切换当前的Platform为其它平台(WINDOWS)

3D模型和导出的png

使用方法

新建一个空的GameObject,附加上此脚本,配置好参数,按Play,会自动完成,可以到Project Path/Folder 目录下找到输出的文件

AnimationToPNG源码

CSharp - AnimationToPNG.cs

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;/*
The MIT License (MIT)Copyright (c) 2014 Brad Nelson and Play-Em Inc.Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/// AnimationToPNG is based on Twinfox and bitbutter's Render Particle to Animated Texture Scripts,
// this script will render out an animation that is played when "Play" is pressed in the editor./*
Basically this is a script you can attach to any gameobject in the scene. If you have Unity Pro, you can use Render Textures, which can accurately
render the transparent background for your animations easily in full resolution
of the camera. The script will autodetect if you have Unity Pro and use
Render Textures automatically. If you are using Unity Free, then the screen will have a split area using
half of the screen width to render the animations. You can change the "animationName" to a string of your choice for a
prefix for the output file names, if it is left empty then no filename
will be added. The destination folder is relative to the Project Folder root, so you
can change the string to a folder name of your choice and it will be
created. If it already exists, it will simply create a new folder with a
number incremented as to how many of those named folders exist. Choose how many frames per second the animation will run by changing the
"frameRate" variable, and how many frames of the animation you wish to
capture by changing the "framesToCapture" variable. Once "Play" is pressed in the Unity Editor, it should output all the
animation frames to PNGs output in the folder you have chosen, and will
stop capturing after the number of frames you wish to capture is
completed.
*/public class AnimationToPNG : MonoBehaviour
{// Animation Name to be the prefix for the output filenamespublic string animationName = "";// Default folder name where you want the animations to be outputpublic string folder = "PNG_Animations";// Framerate at which you want to play the animationpublic int frameRate = 25;// How many frames you want to capture during the animationpublic int framesToCapture = 25;// White Cameraprivate Camera whiteCam;// Black Cameraprivate Camera blackCam;// Pixels to World Unit sizepublic float pixelsToWorldUnit = 74.48275862068966f;// If you have Unity Pro you can use a RenderTexture which will render the full camera width, otherwise it will only render halfprivate bool useRenderTexture = false;private int videoframe = 0; // how many frames we've renderedprivate float originaltimescaleTime; // track the original time scale so we can freeze the animation between framesprivate string realFolder = ""; // real folder where the output files will beprivate bool done = false; // is the capturing finished?private bool readyToCapture = false;  // Make sure all the camera setup is complete before capturingprivate float cameraSize; // Size of the orthographic camera established from the current screen resolution and the pixels to world unitprivate Texture2D texb; // black camera textureprivate Texture2D texw; // white camera textureprivate Texture2D outputtex; // final output textureprivate RenderTexture blackCamRenderTexture; // black camera render texureprivate RenderTexture whiteCamRenderTexture; // white camera render texurepublic void Start(){useRenderTexture = Application.HasProLicense();// Set the playback framerate!// (real time doesn't influence time anymore)Time.captureFramerate = frameRate;// Create a folder that doesn't exist yet. Append number if necessary.realFolder = folder;int count = 1;while (Directory.Exists(realFolder)){realFolder = folder + count;count++;}// Create the folderDirectory.CreateDirectory(realFolder);originaltimescaleTime = Time.timeScale;// Force orthographic camera to render out sprites per pixel size designated by pixels to world unitcameraSize = Screen.width / (((Screen.width / Screen.height) * 2) * pixelsToWorldUnit);GameObject bc = new GameObject("Black Camera");bc.transform.localPosition = new Vector3(0, 0, -1);blackCam = bc.AddComponent<Camera>();blackCam.backgroundColor = Color.black;blackCam.orthographic = true;blackCam.orthographicSize = cameraSize;blackCam.tag = "MainCamera";GameObject wc = new GameObject("White Camera");wc.transform.localPosition = new Vector3(0, 0, -1);whiteCam = wc.AddComponent<Camera>();whiteCam.backgroundColor = Color.white;whiteCam.orthographic = true;whiteCam.orthographicSize = cameraSize;// If not using a Render Texture then set the cameras to split the screen to ensure we have an accurate image with alphaif (!useRenderTexture){// Change the camera rects to have split on screen to capture the animation properlyblackCam.rect = new Rect(0.0f, 0.0f, 0.5f, 1.0f);whiteCam.rect = new Rect(0.5f, 0.0f, 0.5f, 1.0f);}// Cameras are set ready to capture!readyToCapture = true;}void Update(){// If the capturing is not done and the cameras are set, then Capture the animationif (!done && readyToCapture){StartCoroutine(Capture());}}void LateUpdate(){// When we are all done capturing, clean up all the textures and RenderTextures from the sceneif (done){DestroyImmediate(texb);DestroyImmediate(texw);DestroyImmediate(outputtex);if (useRenderTexture){//Clean UpwhiteCam.targetTexture = null;RenderTexture.active = null;DestroyImmediate(whiteCamRenderTexture);blackCam.targetTexture = null;RenderTexture.active = null;DestroyImmediate(blackCamRenderTexture);}}}IEnumerator Capture(){if (videoframe < framesToCapture){// name is "realFolder/animationName0000.png"// string name = realFolder + "/" + animationName + Time.frameCount.ToString("0000") + ".png";string filename = String.Format("{0}/" + animationName + "{1:D04}.png", realFolder, Time.frameCount);// Stop timeTime.timeScale = 0;// Yield to next frame and then start the renderingyield return new WaitForEndOfFrame();// If we are using a render texture to make the animation frames then set up the camera render texturesif (useRenderTexture){//Initialize and render texturesblackCamRenderTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);whiteCamRenderTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);blackCam.targetTexture = blackCamRenderTexture;blackCam.Render();RenderTexture.active = blackCamRenderTexture;texb = GetTex2D(true);//Now do it for Alpha CamerawhiteCam.targetTexture = whiteCamRenderTexture;whiteCam.Render();RenderTexture.active = whiteCamRenderTexture;texw = GetTex2D(true);}// If not using render textures then simply get the images from both cameraselse{// store 'black background' imagetexb = GetTex2D(true);// store 'white background' imagetexw = GetTex2D(false);}// If we have both textures then create final output textureif (texw && texb){int width = Screen.width;int height = Screen.height;// If we are not using a render texture then the width will only be half the screenif (!useRenderTexture){width = width / 2;}outputtex = new Texture2D(width, height, TextureFormat.ARGB32, false);// Create Alpha from the difference between black and white camera rendersfor (int y = 0; y < outputtex.height; ++y){ // each rowfor (int x = 0; x < outputtex.width; ++x){ // each columnfloat alpha;if (useRenderTexture){alpha = texw.GetPixel(x, y).r - texb.GetPixel(x, y).r;}else{alpha = texb.GetPixel(x + width, y).r - texb.GetPixel(x, y).r;}alpha = 1.0f - alpha;Color color;if (alpha == 0){color = Color.clear;}else{color = texb.GetPixel(x, y) / alpha;}color.a = alpha;outputtex.SetPixel(x, y, color);}}// Encode the resulting output texture to a byte array then write to the filebyte[] pngShot = outputtex.EncodeToPNG();File.WriteAllBytes(filename, pngShot);print(filename+"  ");// Reset the time scale, then move on to the next frame.Time.timeScale = originaltimescaleTime;videoframe++;}// Debug.Log("Frame " + name + " " + videoframe);}else{Debug.Log("Complete! " + videoframe + " videoframes rendered (0 indexed)");done = true;}}// Get the texture from the screen, render all or only half of the cameraprivate Texture2D GetTex2D(bool renderAll){// Create a texture the size of the screen, RGB24 formatint width = Screen.width;int height = Screen.height;if (!renderAll){width = width / 2;}Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);// Read screen contents into the texturetex.ReadPixels(new Rect(0, 0, width, height), 0, 0);tex.Apply();return tex;}
}

WIKI资料

http://wiki.unity3d.com/index.php/AnimationToPNG

Unity-WIKI 之 AnimationToPNG相关推荐

  1. 【Unity3D基础教程】给初学者看的Unity教程(零):如何学习Unity3D

    转自:https://www.cnblogs.com/neverdie/p/How_To_Learn_Unity3D.html(http://www.cnblogs.com/neverdie/) Un ...

  2. unity 3d开发的大型网络游戏 1

    unity 3d开发的大型网络游戏 一.总结 1.unity的官网上面应该有游戏列表 2.unity3D是很好的3d游戏引擎,也支持2d,也能做很多画面精良的3A级游戏 3.范围:电脑游戏,手机游戏, ...

  3. Unity学习资源指南[精心整理]

    前言 进入一个领域,最直接有效的方法就是寻找相关综述性文章,首先你需要对你入门的领域有个概括性的了解,这些包括: 1.主流的学习社区与网站. 2.该领域的知名大牛与热心分享的从业者. 3.如何有效的激 ...

  4. Unity Shaderlab: Object Outlines 转

    转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...

  5. Unity3d快速入门

    https://www.zhihu.com/question/313621072 Unity3d如何快速入门 前言 进入一个领域,最直接有效的方法就是,寻找相关综述性文章,首先你需要对你入门的领域有个 ...

  6. 自学unity3d能找到工作吗

    Unity3D的功能令人印象深刻,也能够适应不同的游戏开发要求.游戏开发人员可以使用Unity3D创建任意类型的游戏,从世界级的RPG游戏到备受欢迎的增强现实游戏Pokemon Go.也因为如此,Un ...

  7. unit3d 初次接触

    最近, 有朋友告我,他们做那个 vr 视频啥的,告我看后,感觉很好,故 ,就去网上搜索一下,了解如下: 1..unit 3d 是啥? Unity3D是一个跨平台的游戏引擎 是由Unity Techno ...

  8. Cg Programming In Unity Silhouette Enhancement(Wiki翻译自用)

    Contents 光滑表面的轮廓 增加轮廓上的不透明度 在shader中实现方程 shader 代码 更多的艺术控制 总结 本教程介绍了曲面法线向量的变换. 本教程的目的是实现一种效果:半透明对象的轮 ...

  9. 在Unity内利用混融公式剔除背景颜色导出透明PNG以及半透明遮挡相关问题的研究

    最近听到了一个想法,是将Unity内的一些特效进行截图,导出带透明度的png用于视频制作,于是回顾了一下以前研究过的混融相关的问题,研究了一下如何实现,也学到了一些新的技巧和知识. 混融公式 (图1: ...

最新文章

  1. Windows10 C盘爆满如何清理
  2. 红米note3android驱动,红米note3 mtp驱动
  3. 计算机软考中级网络工程师,如何复习计算机软考中级网络工程师更有效
  4. SQL Server 2008 修改安装路径后安装出错的解决方法
  5. 洛谷【P2758】-编辑距离
  6. Redis 缓存 Key
  7. java: PO,VO,TO,BO,DAO,POJO 解释
  8. 彩色手绘元宵节插画风素材图片
  9. php 合并数组对象,JS内数组合并方法与对象合并实现步骤详解
  10. 《SolidWorks 2017中文版机械设计从入门到精通)》——2.7 复合草图实例操作
  11. svn对项目权限进行管理
  12. jq实现图片拖动滑块验证码
  13. 计算机软件如何永久删除,电脑上如何卸载软件? 如何从电脑上彻底删除一个软件?...
  14. vue3子组件调用父组件的方法
  15. 赠人玫瑰,手有余香,今日份黑科技软件五款奉上
  16. CE 开启 DBVM
  17. u9搜索引擎推送破解版
  18. 1.14 JavaScript5:常用DOM操作
  19. java泡妞代码_java泡妞小程序
  20. illumina 双末端测序

热门文章

  1. 中国AI城市格局突变:杭州反超深圳,南京上海平起平坐,济南首次跻身前十...
  2. 苹果为了不让AirTag被用来跟踪,将推出一个安卓应用
  3. 百度被曝将成立芯片公司!头部互联网玩家,为何纷纷入局造芯?
  4. SQL server 2005中无法新建作业(Job)的问题
  5. Spring Cloud Gateway重试机制
  6. fon循环总是返回最后值问题
  7. 深入浅出 消息队列 ActiveMQ(转)
  8. C++中文转码问题(GB2312 - UTF8)
  9. [原创]ExtAspNet秘密花园(十六) — 表格之排序与分页
  10. 泛型技巧系列:简单类型选择器