Unity IOS 录屏

用到的是ios的replaykit,现在使用的unity 2017.1.f3版本已经自己集成了,所以调用相应提供的接口就可以,很简单

但是目前测试会在安装后第一次录屏的时候出现黑屏录屏失败的情况,所以可以采用第一次初始化的时候先录制然后放弃保存避免这个问题,间隔1s,马上执行放弃保存操作会失败,以后正常录制没有问题

代码如下

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Apple.ReplayKit;

public class PluginsForIOS : MonoBehaviour
{

    public const string Name = "PluginsForIOS";
    static PluginsForIOS() { }
    protected PluginsForIOS() { }
    protected static volatile PluginsForIOS instance = null;
    protected readonly object syncRoot = new object();
    protected static readonly object staticSyncRoot = new object();

    public static PluginsForIOS Instance
    {
        get
        {
            if (instance == null)
            {
                lock (staticSyncRoot)
                {
                    if (instance == null)
                    {
                        GameObject PluginsForIOSObj = new GameObject(Name);
                        instance = PluginsForIOSObj.AddComponent<PluginsForIOS>();
                    }
                }
            }
            return instance;
        }
    }
#if UNITY_IPHONE

   
    #region ReplayKit

    public static string LastError = "";
    public static bool Recording = false;
    public static void StartRecord()
    {
        if (ReplayKit.APIAvailable)//表示ReplayKit API是否可用(True表示可用)
        {
            if (!Recording)
            {
                try
                {
                    Recording = true;
                    ReplayKit.StartRecording(true, false);//开始录像,第一个参数是否开采集麦克风,第二个使用预览视图(目前没找到怎么用,判断跟广播功能有关)
                }
                catch (Exception e)
                {
                    LastError = e.ToString();
                }
            }
        }
        else
        {
            Debug.Log("StartRecording");
        }
    }

    public static void StopRecord()
    {
        if (ReplayKit.APIAvailable)//表示ReplayKit API是否可用(True表示可用)
        {
            Instance.StartCoroutine(Instance.IEStopRecord());
        }
        else
        {
            Debug.Log("StopRecord");
        }
    }

    IEnumerator IEStopRecord()
    {
        try
        {
            ReplayKit.StopRecording();//停止录屏
        }
        catch (Exception e)
        {
            LastError = e.ToString();
        }
        yield return new WaitForSeconds(2f);
        Recording = false;
        PreviewRecord();//开启预览窗口
    }
    public static void PreviewRecord()
    {
        if (ReplayKit.recordingAvailable)//表示新录制可用于预览(True表示可用)
        {
            ReplayKit.Preview();//预览当前录像
        }
        else
        {
            Debug.Log("PreviewRecord");
        }
    }
    public static void DiscardRecord()
    {
        if (ReplayKit.recordingAvailable)//表示新录制可用于预览(True表示可用)
        {
            ReplayKit.Discard();
        }
        else
        {
            Debug.Log("DiscardRecord");
        }
    }

    public static void PauseRecord()//丢弃录制
    {

    }
    #endregion

#endif
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值