UnityIOS錄屏

su9257_海瀾發表於2017-11-03

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



相關文章