[Unity] Unity3D 实现原始方式获取连拍照片

查看:943 |回复:3 | 2017-3-8 12:08:27

您需要 登录 才可以下载或查看,没有账号?注册

x
     今天为大家分享一下,如何在unity利用原始方式实现获取连拍照片,甚至我们可以把一系列图片的利用一些视频软件后期合成视频。
但是直接在用代码利用这一系列图片合成视频,这种方式还是不推荐的,毕竟效率很低下,如果需要实现录屏功能,ShareREC便是一个比较好的选择。
   
     好吧!废话不多讲,我们直接进入主题噢!
    1.新建一个unity项目,并且在项目中新建Editor文件夹,首先我们先实现一个编辑器,在文件夹下有一个CapResRecorder_Editor.cs;
      代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;

  4. public class CapResRecorder_Editor  {

  5.         [MenuItem("GameObject/Create CapResRecorder", false,10)]
  6.         public static void CreateHiResRecorder(MenuCommand menuCommand) {

  7.                 CapResRecorder recorder = Object.FindObjectOfType<CapResRecorder>();
  8.                 if (recorder) {
  9.                         Debug.LogWarning("An instance of CapResRecorder already exists!");
  10.                         Selection.activeObject = recorder.gameObject;
  11.                 } else {
  12.                        
  13.                         GameObject go = new GameObject("CapResRecorder");
  14.                         go.AddComponent<CapResRecorder>();
  15.                         GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
  16.                         Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
  17.                         Selection.activeObject = go;
  18.                
  19.                 }
  20.         }

  21. }
点击此处复制文本



    2.这一步稍微比较重要,正是生成系列图片的逻辑代码类CapResRecorder.cs;
      
      代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;


  4. public class CapResRecorder : MonoBehaviour {
  5.        
  6.         public KeyCode key = KeyCode.A;
  7.         public int frameRate = 30;
  8.        
  9.         bool isCapturing = false;
  10.         string prefix,path;
  11.         int folderIndex = 0;
  12.         int imgIndex = 0;
  13.         float localDeltaTime,prevTime,fixedDeltaTimeCache;
  14.        
  15.         void Start () {
  16.                 DontDestroyOnLoad(this.gameObject);
  17.                 fixedDeltaTimeCache = Time.fixedDeltaTime;

  18.                 if (Application.isEditor)
  19.                         prefix = Application.dataPath+"/../CapResRecorder/";
  20.                 else
  21.                         prefix = Application.dataPath+"/CapResRecorder/";
  22.                 Debug.Log("IMG sequence will be saved to "+prefix);
  23.         }
  24.        
  25.        
  26.         void LateUpdate () {
  27.                
  28.                 localDeltaTime = Time.realtimeSinceStartup - prevTime;
  29.                 prevTime = Time.realtimeSinceStartup;
  30.                
  31.                 if (Input.GetKeyDown(key)) {
  32.                         isCapturing = !isCapturing;
  33.                         if (isCapturing) {
  34.                                 folderIndex+=1;
  35.                                 imgIndex = 0;
  36.                                 path = prefix+"IMG Sequence "+folderIndex;
  37.                                 if (Directory.Exists(path)==false)
  38.                                         Directory.CreateDirectory(path);
  39.                                 path = path + "/";
  40.                                
  41.                         } else {
  42.                                 Time.timeScale = 1f;
  43.                                 //Time.fixedDeltaTime = fixedDeltaTimeCache;
  44.                         }
  45.                 }
  46.                 if (isCapturing){
  47.                         Application.CaptureScreenshot(path+imgIndex.ToString("D8")+".png");
  48.                         imgIndex+=1;
  49.                         Time.timeScale = 1.0f/localDeltaTime/frameRate;
  50.                         //Time.fixedDeltaTime = fixedDeltaTimeCache / Time.timeScale;
  51.                 }
  52.         }
  53. }

点击此处复制文本



    3.然后我们发现在GameObject下面有一个选项,我们可以直接点击创建一个CapResRecorder对象;
3.png


    4.最后我们直接运行项目,就得到一系列的截图啦!
4_0.png


   
   
    学习交流群:575561285

评分

参与人数 1元素币 +10 展开 理由
狼之独步 + 10 希望多分享拍照拍视频的教学

查看全部评分

2017-3-8 12:08:27  
 赞 赞 1

使用道具 登录

3个回答,把该问题分享到群,邀请大神一起回答。
2#
支持支持支持支持支持支持支持支持
回复 收起回复
2017-3-9 17:03:10   回复
 赞 赞 1

使用道具 登录

3#
顶!感谢楼主分享!
回复 收起回复
2017-3-12 10:14:05   回复
 赞 赞 1

使用道具 登录

4#
回复 收起回复
2023-2-28 11:11:14   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表