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