unity3d 截图制作视频代码
2018-03-16 14:46
399 查看
先来效果
直接上代码
直接上代码
#if UNITY_EDITOR using UnityEngine; using System.Collections; using System.IO; using System; public class AnimationToPNG : MonoBehaviour { public string animationName = ""; public string folder = "PNG_Animations"; public string folderPath = ""; public int frameRate = 30; public int framesToCapture = 90; private Camera whiteCam; private Camera blackCam; public float fieldOfView = 20; private bool useRenderTexture = false; public int videoframe = 0; private float originaltimescaleTime; private string realFolder = ""; private bool done = false; private bool readyToCapture = false; private Texture2D texb; private Texture2D texw; private Texture2D outputtex; private RenderTexture blackCamRenderTexture; // black camera render texure private RenderTexture whiteCamRenderTexture; // white camera render texure public void OnStart() { useRenderTexture = Application.HasProLicense(); Time.captureFramerate = frameRate; realFolder = folder + "/" + folderPath; int count = 1; while (Directory.Exists(realFolder)) { realFolder = realFolder + count; count++; } Directory.CreateDirectory(realFolder); originaltimescaleTime = Time.timeScale; if (blackCam == null) { GameObject bc = new GameObject("Black Camera"); bc.transform.parent = gameObject.transform; bc.transform.localPosition = Vector3.zero; bc.transform.localScale = Vector3.one; bc.transform.localRotation = new Quaternion(0, 0, 0, 0); blackCam = bc.AddComponent<Camera>(); blackCam.backgroundColor = Color.black; blackCam.fieldOfView = fieldOfView; blackCam.tag = "MainCamera"; blackCam.cullingMask = ~(1 << LayerMask.NameToLayer("Default")); } if (whiteCam == null) { GameObject wc = new GameObject("White Camera"); wc.transform.parent = gameObject.transform; wc.transform.localPosition = Vector3.zero; wc.transform.localScale = Vector3.one; wc.transform.localRotation = new Quaternion(0, 0, 0, 0); whiteCam = wc.AddComponent<Camera>(); whiteCam.backgroundColor = Color.white; whiteCam.fieldOfView = fieldOfView; whiteCam.cullingMask = ~(1 << LayerMask.NameToLayer("Default")); if (!useRenderTexture) { blackCam.rect = new Rect(0.0f, 0.0f, 0.5f, 1.0f); whiteCam.rect = new Rect(0.5f, 0.0f, 0.5f, 1.0f); } readyToCapture = true; } } void Update() { if (!done && readyToCapture) { StartCoroutine(Capture()); } } void LateUpdate() { if (done) { DestroyImmediate(texb); DestroyImmediate(texw); DestroyImmediate(outputtex); if (useRenderTexture) { //Clean Up whiteCam.targetTexture = null; RenderTexture.active = null; DestroyImmediate(whiteCamRenderTexture); blackCam.targetTexture = null; RenderTexture.active = null; DestroyImmediate(blackCamRenderTexture); } } } IEnumerator Capture() { if (videoframe < framesToCapture) { animationName = videoframe + "_"; string filename = String.Format("{0}/" + animationName + "{1:D04}.png", realFolder, Time.frameCount); Time.timeScale = 0; yield return new WaitForEndOfFrame(); if (useRenderTexture) { //Initialize and render textures blackCamRenderTexture = 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 Camera whiteCam.targetTexture = whiteCamRenderTexture; whiteCam.Render(); RenderTexture.active = whiteCamRenderTexture; texw = GetTex2D(true); } else { texb = GetTex2D(true); texw = GetTex2D(false); } if (texw && texb) { int width = Screen.width; int height = Screen.height; if (!useRenderTexture) { width = width / 2; } outputtex = new Texture2D(width, height, TextureFormat.ARGB32, false); for (int y = 0; y < outputtex.height; ++y) { // each row for (int x = 0; x < outputtex.width; ++x) { // each column float 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 file byte[] pngShot = outputtex.EncodeToPNG(); File.WriteAllBytes(filename, pngShot); // Reset the time scale, then move on to the next frame. Time.timeScale = originaltimescaleTime; videoframe++; } // Debug.Log("Frame " + name + " " + videoframe); } else { Debug.Log("完成"); done = true; GameObject.DestroyImmediate(whiteCam.gameObject); GameObject.DestroyImmediate(blackCam.gameObject); GameObject.DestroyImmediate(this); } } private Texture2D GetTex2D(bool renderAll) { int width = Screen.width; int height = Screen.height; if (!renderAll) { width = width / 2; } Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false); tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex.Apply(); return tex; } } #endif
相关文章推荐
- 一行代码完成 调用系统相册 和 视频,语音库,选择图片,视频,和音频文件。在选择界面实现了录制视频和录制音频功能。 截图
- Unity3d制作一个简单的打开保存对话框-编写核心代码(1)
- 【Unity3D自学记录】制作VR视频播放器
- 网页制作之在线视频播放代码
- VB代码VB小程序:实现USB摄像头视频图像的监控、截图、录像
- C#(asp.net)上传视频转换为flv格式并给视频截图代码
- 巧用工具制作视频截图索引文件
- oCam 中文绿色版 - 免费实用的屏幕录像与截图软件 (制作视频教程/录制直播视频)
- 巧用工具制作视频截图索引文件
- VB程序VB代码:摄像头视频图像的监控,截图,录像(改进)
- html制作简单框架网页二 实现自己的影音驿站 操作步骤及源文件下载 (可播放mp4、avi、mpg、asx、swf各种文件的视频播放代码)
- DirectShow 制作在Unity3D中可以设置进度的视频播放插件
- Android平台美颜相机/Camera实时滤镜/视频编解码/影像后期/人脸技术探索——2.4 滤镜以及配套代码的制作方法
- 网页制作之在线视频播放代码
- ASP.NET 视频截图功能的C#代码
- android中 代码实现截图功能(静态+动态视频)
- unity3d开发2d游戏:制作动画砖块实例代码
- unity3d 屏幕截图 核心代码
- android视频播放截图并制作成gif图片
- ASP.NET 视频截图功能的C#代码