天天看點

EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能

螢幕錄像不是增強現實的功能,隻是EasyAR SDK自帶的一個功能。該功能限制頗多,隻能在移動裝置上使用,而且沒有辦法直接錄制UI界面。螢幕錄像功能本質上錄的是RenderTexture。

總體說明

主要是在基礎結構上添加了一個【VideoRecorder】遊戲對象。另外,需要動态的往【Main Camera】主錄影機遊戲對象上添加【CameraRecorder】腳本。

EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能

CameraRecorder腳本相關

  • CameraRecorder腳本需要動态添加到Main Camera主錄影機遊戲對象上,該腳本可以通過Setup方法設定錄像内容的水印。

VideoCameraDevice遊戲對象相關

  • Enable屬性可以用于關閉攝像頭内容,這樣,就能隻錄制螢幕内容。
  • Camera Size屬性用于設定攝像頭擷取的視訊的分辨率。

VideoRecorder遊戲對象相關

  • Profile屬性用于設定錄制效果,最高可以錄制1080高清内容。
  • Qrientation屬性用于設定錄像時候是橫屏(Landscape)還是豎屏(Portratit),或者根據目前螢幕情況設定(Screen Adaptive)。
  • Record Zoom Mode屬性設定螢幕錄制。NoZoomAndClip:如果輸出寬高比與輸入不符,内容會被剪裁到适合輸出比例。ZoomInWithAllContent:如果輸出寬高比與輸入不符,内容将不會被剪裁,在某個次元上會有黑邊。
  • File Path Type屬性設定檔案的路徑類型,“Absolute”是絕對路徑,“Persistent Data Path”是相對于持久資料路徑的相對路徑。通常使用後者,因為該路徑下能保證可寫入。
  • File Path屬性用于設定錄制後儲存的視訊檔案的具體路徑及檔案名。

取消多線程渲染

取消Other Settings标簽下Multithreaded Rendering選項

EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能

使用螢幕錄像功能

  • 隻導入EasyAR4.0 SDK,要使用螢幕錄像功能還比較麻煩,還需要将官方示例中的一個腳本導入過來,這樣使其起來會簡單很多。
EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能
  • 設定場景中的Main Camera的Clear Flags屬性為Solid Color。
  • 将EasyAR/Prefabs/Composites目錄下的EasyAR_VideoCamera預制件拖到場景中。
EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能
  • 将EasyAR/Prefabs/Primitives目錄下的VideoRecorder預制件拖到場景中。
  • 設定Videorecorder屬性,設定File Path Type為Persistent Data Path,設定File Path為具體檔案名。
EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能

添加2個按鈕用于開始和停止錄像,添加一個文本框顯示提示。

編寫腳本。

  • 在腳本的“Awake”事件中,添加對“StatusUpdate”事件的偵聽内容。将擷取到的資訊顯示到文本框。
  • 添加開始錄制的方法。在運作了“StartRecording”事件後,還需要動态的往主錄影機上添加“CameraRecoder”腳本,并運作該腳本的“Setup”方法。
  • 添加停止錄像的方法,停止後,還需要删除動态添加的腳本。
using UnityEngine;
using UnityEngine.UI;
using easyar;
using VideoRecording;

public class RecorderController : MonoBehaviour
{
    public Text uiText;
    public VideoRecorder videoRecorder;
    private CameraRecorder cameraRecorder;

    private void Awake()
    {
        videoRecorder.StatusUpdate += (status, msg) =>
        {
            if (status == RecordStatus.OnStarted)
            {
                uiText.text = "Recording start";
            }
            if (status == RecordStatus.FailedToStart || 
status == RecordStatus.FileFailed || status == RecordStatus.LogError)
            {
                uiText.text = "Recording Error: " + status + ", details: " + msg;
            }
            Debug.Log("RecordStatus: " + status + ", details: " + msg);
        };
    }
    public void StartRecorder()
    {
        videoRecorder.StartRecording();
        cameraRecorder = 
Camera.main.gameObject.AddComponent<CameraRecorder>();
        cameraRecorder.Setup(videoRecorder, null);
    }
    public void StopRecorder()
    {
        if (videoRecorder.StopRecording())
        {
            uiText.text = "Recording stop " + videoRecorder.FilePath;
        }
        else
        {
            uiText.text = "Recording failed";
        }
        if (cameraRecorder)
        {
            cameraRecorder.Destroy();
        }
    }
}

           

運作以後,點選按鈕就可以開始錄像,文本框會顯示開始錄制的提示“Recording start”。點選停止按鈕,就會停止錄像。

EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能

錄像的結果會儲存在持久資料目錄下,具體目錄位置請檢視Unity官方文檔

https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Application-persistentDataPath.html

EasyAR4.0使用說明(Unity3D)(九)----螢幕錄像總體說明使用螢幕錄像功能

視訊版位址:https://www.bilibili.com/video/bv1eK4y1k7QX

繼續閱讀