unity-代码控制录屏UnityRecorder

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Recorder;
using UnityEditor.Recorder.Input;
using UnityEngine;

public enum RecorderControllerState
{
    Video,
    Animation,
    ImageSequence
}

/// <summary>
/// 录制
/// </summary>
public class NewBehaviourScript : MonoBehaviour
{
    RecorderController m_RecorderController;

    private RecorderControllerState controllerState = RecorderControllerState.Video;
    [Header("下面两个单纯观看数据,不用管")]
    public RecorderControllerSettings controllerSettings;
    public MovieRecorderSettings videoRecorder;

    private string animationOutputFolder;
    private string mediaOutputFolder;

    private void Start()
    {
        controllerSettings = ScriptableObject.CreateInstance<RecorderControllerSettings>();
        m_RecorderController = new RecorderController(controllerSettings);
        animationOutputFolder = Application.dataPath + "/SampleRecordings";
        mediaOutputFolder = Application.dataPath + "../SampleRecordings";

    }

    /// <summary>
    /// 开始记录视频
    /// </summary>
    /// <param name="state">默认视频</param>
    public void StartRecorder(RecorderControllerState state = RecorderControllerState.Video)
    {

        //var outputFolder = Application.dataPath + "/SampleRecordings";
        switch (state)
        {
            case RecorderControllerState.Video:

                // Video
                RecorderVideo();

                break;
            case RecorderControllerState.Animation:

                // Animation
                RecorderAnimation();
                break;
            case RecorderControllerState.ImageSequence:

                // Image Sequence
                RecorderImageSequence();
                break;

            default:
                break;
        }
        // Setup Recording

        controllerSettings.SetRecordModeToManual();
        controllerSettings.frameRate = 10.0f;

        Options.verboseMode = false;
        m_RecorderController.StartRecording();
    }

    /// <summary>
    /// 录制视频
    /// </summary>
    private void RecorderVideo()
    {
        videoRecorder = ScriptableObject.CreateInstance<MovieRecorderSettings>();
        videoRecorder.name = "My Video Recorder";
        videoRecorder.enabled = true;

        videoRecorder.outputFormat = VideoRecorderOutputFormat.WEBM;
        videoRecorder.videoBitRateMode = VideoBitrateMode.High;

        videoRecorder.imageInputSettings = new GameViewInputSettings
        {
            outputWidth = 1920,
            outputHeight = 1080
        };

        videoRecorder.audioInputSettings.preserveAudio = true;
        string str = DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second;
        videoRecorder.outputFile = mediaOutputFolder + "/Magic_" + str;
        controllerSettings.AddRecorderSettings(videoRecorder);
    }

    /// <summary>
    /// 动画
    /// </summary>
    private void RecorderAnimation()
    {
        var animationRecorder = ScriptableObject.CreateInstance<AnimationRecorderSettings>();
        animationRecorder.name = "My Animation Recorder";
        animationRecorder.enabled = true;

        var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

        animationRecorder.animationInputSettings = new AnimationInputSettings
        {
            gameObject = sphere,
            recursive = true,
        };

        animationRecorder.animationInputSettings.AddComponentToRecord(typeof(Transform));

        animationRecorder.outputFile = animationOutputFolder + "/animation_" + DefaultWildcard.GeneratePattern("GameObject") + "_" + DefaultWildcard.Take;

        controllerSettings.AddRecorderSettings(animationRecorder);
    }

    /// <summary>
    /// 图像序列
    /// </summary>
    private void RecorderImageSequence()
    {
        var imageRecorder = ScriptableObject.CreateInstance<ImageRecorderSettings>();
        imageRecorder.name = "My Image Recorder";
        imageRecorder.enabled = true;

        imageRecorder.outputFormat = ImageRecorderOutputFormat.PNG;
        imageRecorder.captureAlpha = true;

        imageRecorder.outputFile = mediaOutputFolder + "/image_" + DefaultWildcard.Frame + "_" + DefaultWildcard.Take;

        imageRecorder.imageInputSettings = new CameraInputSettings
        {
            source = ImageSource.MainCamera,
            outputWidth = 1920,
            outputHeight = 1080,
            captureUI = true
        };

        controllerSettings.AddRecorderSettings(imageRecorder);
    }

    /// <summary>
    /// 停止录制
    /// </summary>
    public void StopRecorder()
    {
        Debug.Log("停止录制");
        m_RecorderController.StopRecording();
        controllerSettings.RemoveRecorder(videoRecorder);
    }

    void OnDisable()
    {
        StopRecorder();
    }
}

由于在代码中引入了using UnityEditor;
using UnityEditor.Recorder;
using UnityEditor.Recorder.Input;

这是unity编辑器的,所以加入此代码不能发布,会报错

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值