用到的是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
}