Unity 工具 之 Natcorder 插件實現錄頻、拍照jpg、生成Gif動圖功能(適用移動端)

 

 

Unity 工具 之  插件實現錄頻、拍照jpg、生成Gif動圖功能(適用移動端)

 

目錄

Unity 工具 之  插件實現錄頻、拍照jpg、生成Gif動圖功能(適用移動端)

一、簡單介紹

二、插件導入使用

三、幾個 Demo 演示效果

四、關鍵代碼

五、下載地址


 

一、簡單介紹

Unity 工具類,自己整理的一些遊戲開發可能用到的模塊,單獨獨立使用,方便遊戲開發。

本節介紹,使用Unity插件,進行屏幕錄製,生成圖片,gif 動圖,以及mp4文件等;能錄製場景,也能錄製攝像頭,多功能錄製,也適用於移動端。

 

二、插件導入使用

1、打開Unity,新建工程

 

2、導入 Natcorder 插件

 

3、幾個場景使用 demo 如下

 

三、幾個 Demo 演示效果

1、Giffy.scene(拍攝場景畫面動圖,可帶攝像頭畫面)

 

2、JPG.scene(拍攝場景畫面)

 

3、ReplayCam.scene(拍攝錄製指定場景 camera 的畫面)

(保存爲MP4文件,可以到指定路徑查看文件(移動端的話,demo中,會自動預覽視頻))

 

4、WebCam .scene(拍攝錄製攝像頭的畫面)

(保存爲MP4文件,可以到指定路徑查看文件(移動端的話,demo中,會自動預覽視頻))

 

四、關鍵代碼

1、Giffy.cs

/* 
*   NatCorder
*   Copyright (c) 2020 Yusuf Olokoba
*/

namespace NatSuite.Examples {

    using UnityEngine;
    using Recorders;
    using Recorders.Clocks;
    using Recorders.Inputs;

    public class Giffy : MonoBehaviour {
        
        [Header("GIF Settings")]
        public int imageWidth = 640;
        public int imageHeight = 480;
        public float frameDuration = 0.1f; // seconds

        private GIFRecorder recorder;
        private CameraInput cameraInput;

        public void StartRecording () {
            // Start recording
            recorder = new GIFRecorder(imageWidth, imageHeight, frameDuration);
            cameraInput = new CameraInput(recorder, new RealtimeClock(), Camera.main);
            // Get a real GIF look by skipping frames
            cameraInput.frameSkip = 4;
        }

        public async void StopRecording () {
            // Stop the recording
            cameraInput.Dispose();
            var path = await recorder.FinishWriting();
            Debug.Log($"Saved animated GIF image to: {path}");
            var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
            Application.OpenURL($"{prefix}{path}");
        }
    }
}

 

2、JPG.cs

using NatSuite.Recorders;
using NatSuite.Recorders.Clocks;
using NatSuite.Recorders.Inputs;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JPG : MonoBehaviour
{
    [Header("GIF Settings")]
    public int imageWidth = 640;
    public int imageHeight = 480;

    private JPGRecorder recorder;
    private CameraInput cameraInput;
    public Camera cam;

    private bool isPhoto = false;

    public void StartRecording()
    {
        if (isPhoto == false) {
            isPhoto = true;
            // Start recording
            recorder = new JPGRecorder(imageWidth, imageHeight);
            cameraInput = new CameraInput(recorder, new RealtimeClock(), cam);
            cameraInput.frameSkip = 40;
            Debug.Log(" StartRecording ");
        }
    }

    public async void StopRecording()
    {
        isPhoto = false;

        // Stop the recording
        cameraInput.Dispose();
        var path = await recorder.FinishWriting();
        Debug.Log($"Saved animated jpg image to: {path}");
        var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
        Application.OpenURL($"{prefix}{path}");
    }
}

 

3、ReplayCam.cs

using UnityEngine;
    using System.Collections;
    using Recorders;
    using Recorders.Clocks;
    using Recorders.Inputs;
    
    public class ReplayCam : MonoBehaviour {

        [Header("Recording")]
        public int videoWidth = 1280;
        public int videoHeight = 720;
        public bool recordMicrophone;

        private IMediaRecorder recorder;
        private CameraInput cameraInput;
        private AudioInput audioInput;
        private AudioSource microphoneSource;
        public AudioListener audioListener;
        public Camera cam;

        private IEnumerator Start () {
            // Start microphone
            microphoneSource = gameObject.AddComponent<AudioSource>();
            microphoneSource.mute =
            microphoneSource.loop = true;
            microphoneSource.bypassEffects =
            microphoneSource.bypassListenerEffects = false;
            if(recordMicrophone)
                microphoneSource.clip = Microphone.Start(null, true, 10, AudioSettings.outputSampleRate);
            yield return new WaitUntil(() => Microphone.GetPosition(null) > 0);
            microphoneSource.Play();
        }

        private void OnDestroy () {
            // Stop microphone
            if (recordMicrophone)
            {
                microphoneSource.Stop();
                Microphone.End(null);

            }
               
        }

        public void StartRecording () {
            // Start recording
            var frameRate = 30;
            //var sampleRate = recordMicrophone ? AudioSettings.outputSampleRate : 0;
            //var channelCount = recordMicrophone ? (int)AudioSettings.speakerMode : 0;
            var sampleRate =  AudioSettings.outputSampleRate;
            var channelCount = (int)AudioSettings.speakerMode ;
            var clock = new RealtimeClock();
            recorder = new MP4Recorder(videoWidth, videoHeight, frameRate, sampleRate, channelCount);
            // Create recording inputs
            cameraInput = new CameraInput(recorder, clock, cam);
            //audioInput = recordMicrophone ? new AudioInput(recorder, clock, microphoneSource, true) : null;
            audioInput = new AudioInput(recorder, clock, audioListener);
            // Unmute microphone
            microphoneSource.mute = audioInput == null;
        }

        public async void StopRecording () {
            // Mute microphone
            microphoneSource.mute = true;
            // Stop recording
            audioInput?.Dispose();
            cameraInput.Dispose();
            var path = await recorder.FinishWriting();
            // Playback recording
            Debug.Log($"Saved recording to: {path}");
            var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";

            // 移動端,錄頻完後,播放錄製的視頻
            Handheld.PlayFullScreenMovie($"{prefix}{path}");
        }
    }

 

4、WebCam.cs

 using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using Recorders;
    using Recorders.Clocks;
    using Recorders.Inputs;

    public class WebCam : MonoBehaviour {

        public RawImage rawImage;
        public AspectRatioFitter aspectFitter;

        private WebCamTexture webCamTexture;
        private MP4Recorder recorder;
        private WebCamTextureInput webCamTextureInput;

        private IEnumerator Start () {
            // Request camera permission
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
                yield break;
            // Start the WebCamTexture
            webCamTexture = new WebCamTexture(1280, 720, 30);
            webCamTexture.Play();
            // Display webcam
            yield return new WaitUntil(() => webCamTexture.width != 16 && webCamTexture.height != 16); // Workaround for weird bug on macOS
            rawImage.texture = webCamTexture;
            aspectFitter.aspectRatio = (float)webCamTexture.width / webCamTexture.height;
        }

        public void StartRecording () {
            // Start recording
            var clock = new RealtimeClock();
            recorder = new MP4Recorder(webCamTexture.width, webCamTexture.height, 30);
            webCamTextureInput = new WebCamTextureInput(recorder, clock, webCamTexture);
        }

        public async void StopRecording () {
            // Stop recording
            webCamTextureInput.Dispose();
            var path = await recorder.FinishWriting();
            // Playback recording
            Debug.Log($"Saved recording to: {path}");
            var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
            //Handheld.PlayFullScreenMovie($"{prefix}{path}");
        }
    }

 

五、下載地址

建議購買正版插件

Unity Asset Store:https://assetstore.unity.com/packages/tools/integration/natcorder-video-recording-api-102645

CSDN 下載地址:https://download.csdn.net/download/u014361280/12488838

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章