本章節為您介紹了 Unity Demo 的內建操作步驟。
前提條件
您需要開通阿裡雲 RTC 服務并在控制台成功建立應用,具體操作請參見
入門概述。
擷取 Token,詳情請參見
服務端生成 Token建立 Unity 項目
- 打開 Unity,單擊建立。
- 輸入項目名稱、項目儲存位置,并選擇 3D 模版。
- 單擊建立。
內建 SDK
- 下載下傳 Unity SDK 。解壓後包含 sample 和 sdk 兩個目錄,sample 目錄為 Unity 對接工程,sdk 目錄為 Unity 版本 SDK。
- 在項目 Assets 目錄下建立 Plugins 目錄。
- 複制 SDK 中以下目錄檔案到項目 Plugins 目錄。
平台 | 檔案或檔案夾 | 項目路徑 |
Android | /Android/AliRtcCwrapper.aar | /Assets/Plugins/Android |
/Android/AliRTCSdk.jar | ||
/Android/alivc-core-rtc.aar | ||
/Android/Sophonsdk.aar | ||
/Android/utdid4all-1.5.0-proguard.jar | ||
/Android/webrtclib.aar | ||
iOS | /iOS/AliRTCSdk.framework | /Assets/Plugins/iOS |
/iOS/libAliRTCSdkCInterface.a | ||
macOS | /macOS/AliRTCSdkCWrapper.bundle | /Assets/Plugins/macOS |
Windows | /x86/AliRTCSdk.dll | /Assets/Plugins/x86 |
/x86/AliRTCSdkCWrapper.dll | ||
/x64/AliRTCSdk.dll | /Assets/Plugins/x64 | |
/x64/AliRTCSdkCWrapper.dll |
- 在項目 Assets 目錄下建立 Scripts 目錄。
- 複制 SDK 中以下目錄檔案到項目 Scripts 目錄。
檔案 | |
/Scripts/AliRtcEngine.cs | /Assets/Scripts |
/Scripts/IAliRtcEngineBase.cs | |
/Scripts/VideoDisplaySurface.cs |
實作音視訊通話功能
- 添加 UI 控制。您可以建立使用者界面,以 Unity Sample 為例,建立本地視訊視窗 GameObject(LocalVideoCube)和遠端視訊視窗 GameObject(RemoteVideoCube),以及控制按鈕 ControlButton。
- UI 界面儲存到 /Assets/Scenes/SampleScene.unity。
- 建立控制音視訊通信功能的 Home.cs 檔案,并将該檔案添加到主 Main Camera,使得 Home.cs 檔案在 Main Camera 啟動時就被加載。
- 擷取權限(僅 Android 操作)。在 UNITY_2018_3_OR_NEWER 及以上版本,Android 裝置需要在 Unity 中設定主動向使用者擷取麥克風和相機權限,需要調用 CheckPermission 主動擷取權限。
private ArrayList permissionList = new ArrayList();
void Start ()
{
#if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
permissionList.Add(Permission.Microphone);
permissionList.Add(Permission.Camera);
#endif
}
void Update ()
{
#if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
CheckPermission();
#endif
}
private void CheckPermission()
{
#if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
foreach (string permission in permissionList)
{
if (Permission.HasUserAuthorizedPermission(permission))
{
}
else
{
Permission.RequestUserPermission(permission);
}
}
#endif
}
- 此外,內建 Android 平台時,需要 AndroidManifest.xml 檔案和 project.properties 檔案來管理項目權限和項目屬性,可以直接拷貝 AliRTCSdkEngine.plugin 目錄到 / Assets/Plugins/Android。
- 初始化 IAliRtcEngine。執行建立 IAliRtcEngine,根據需要注冊偵聽通知回調,設定自動推流訂閱模式。
string extra = "";
IAliRtcEngine mRtcEngine = IAliRtcEngine.GetEngine (extra);
mRtcEngine.OnJoinChannelNotify = onJoinChannelNotify;
mRtcEngine.OnPublishNotify = onPublishNotify;
mRtcEngine.OnSubscribeNotify = onSubscribeNotify;
mRtcEngine.OnRemoteUserOnLineNotify = onRemoteUserOnLineNotify;
mRtcEngine.OnRemoteUserOffLineNotify = onRemoteUserOffLineNotify;
mRtcEngine.OnRemoteTrackAvailableNotify = onRemoteTrackAvailableNotify;
mRtcEngine.OnSubscribeChangedNotify = onSubscribeChangedNotify;
mRtcEngine.OnLeaveChannelResultNotify = onLeaveChannelResultNotify;
mRtcEngine.SetAutoPublish(true, true);
- 打開本地預覽。進入頻道前需要啟動本地視訊采集預覽顯示,擷取 UI 中 VideoCube 對象,添加 VideoDisplaySurface 到 VideoCube 上。說明VideoDisplaySurface 是 SDK 封裝用于視訊渲染的 C# 類,任何 Unity 中 GameObject 物體上需要顯示視訊内容,必須要綁定 VideoDisplaySurface。VideoDisplaySurface 主要有以下三個功能:
-
- void SetUserId (string uid):設定渲染視訊的使用者 ID,本地 uid 填空字元串
""
- void SetVideoTrack (AliRTCVideoTrack track):設定視訊流類型,有 camera(相機流)和 screen(共享流)兩種,用于區分同一使用者 ID 推送的兩種不同的是視訊流。
- void SetEnable (bool enable):控制視訊是否顯示。
- void SetUserId (string uid):設定渲染視訊的使用者 ID,本地 uid 填空字元串
- 添加 surface 後,調用 IAliRtcEngine 接口設定本地預覽和開啟預覽。
GameObject go = GameObject.Find("LocalVideoCube");
VideoDisplaySurface surface = go.GetComponent<VideoDisplaySurface>();
surface.SetUserId("");
surface.SetVideoTrack(AliRTCVideoTrack.VIDEO_TRACK_CAMERA);
surface.SetEnable(true);
mRtcEngine.SetLocalViewConfig(true);
mRtcEngine.ConfigExternalVideoRendering(true);
mRtcEngine.StartPreview();
- 擷取頻道鑒權資訊。該步驟主要通過和 AppServer 進行通信,擷取進入的頻道名稱以及對應的鑒權資訊,成功擷取到的資訊需要儲存在 AliRTCAuthInfo 結構中。
- 加入頻道。成功擷取到入會鑒權資訊後,通過調用接口 JoinChannel 加入頻道,需要傳遞參數有 AliRTCAuthInfo 和使用者名稱。調用加入頻道接口後,SDK 會通過 OnJoinChannelNotify 通知回調是否入會成功,參數 errorCode 為 0 表示入會成功,其他表示失敗。
mRtcEngine.JoinChannel(authInfo, "userName");
private void onJoinChannelNotify (int errorCode)
{
if (errorCode == 0)
{
Debug.Log("加入頻道成功");
}
else
{
Debug.Log("加入頻道失敗");
}
}
- 遠端使用者視訊顯示。訂閱遠端使用者的音視訊流相關的回調是 onSubscribeChangedNotify,控制遠端使用者視訊的顯示在該回調中實作。onSubscribeChangedNotify 有三個參數:
-
- userId 表示遠端使用者 ID。
- audioTrack 有 mic(麥克風)和 none 兩種類型,none 表示目前沒有遠端音頻流。
- videoTrack 有 camera、screen、both 和 none 四種類型,分别表示該使用者的視訊流狀态,both 表示同時訂閱遠端使用者的 camera(相機流)和 screen(共享流),none 表示目前沒有遠端視訊流。
- 在回調中,檢查參數 videoTrack 是否為 camera、screen、both 三者中的一種,如果是需要使用 GameObject 對象進行顯示,并添加 VideoDisplaySurface,如果 videoTrack 為 none,需要控制 GameObject 對象取消顯示。
private void onSubscribeChangedNotify (string userId, int audioTrack, int videoTrack)
{
if (videoTrack == (int)AliRTCVideoTrack.VIDEO_TRACK_CAMERA)
{
GameObject go = GameObject.Find("RemoteVideoCube");
if (!ReferenceEquals(go, null))
{
VideoDisplaySurface surface = go.GetComponent<VideoDisplaySurface>();
surface.SetUserId(userId);
surface.SetVideoTrack(AliRTCVideoTrack.VIDEO_TRACK_CAMERA);
surface.SetEnable(true);
}
}
else if (videoTrack == (int)AliRTCVideoTrack.VIDEO_TRACK_NONE)
{
GameObject go = GameObject.Find("RemoteVideoCube");
if (!ReferenceEquals(go, null))
{
VideoDisplaySurface surface = go.GetComponent<VideoDisplaySurface>();
surface.SetEnable(false);
}
}
}
- 離開頻道。結束音視訊通話時調用接口 LeaveChannel 離開頻道,離開頻道前需要關閉本地預覽以及移除遠端視訊顯示。
mRtcEngine.StopPreview();
GameObject go = GameObject.Find("LocalVideoCube");
VideoDisplaySurface surface = go.GetComponent<VideoDisplaySurface>();
surface.SetEnable(false);
go = GameObject.Find("RemoteVideoCube");
surface = go.GetComponent<VideoDisplaySurface>();
surface.SetEnable(false);
mRtcEngine.LeaveChannel();
- 銷毀 IAliRtcEngine。調用 IAliRtcEngine 的 Destroy 方法退出 App 或者釋放記憶體。
Unity Editor 中調試
Unity Editor 中調試隻需要確定放入 macOS 或 Windows 所需的檔案或檔案夾到 /Assets/Plugins 目錄即可。
工程導出設定
- Mac 平台:Unity 編譯導出 Mac 平台應用,macOS10.15 以下版本無需做額外設定,隻需選中 Target Platform 為 Mac OS, 勾上 Development Build 即可;macOS10.15 及以上版本需要選擇 Player Settings,在 Player 的 Other Settings 選項中,将 Camera Usage Description 文本框中添加 open camera 字元串,Microphone Usage Description 文本框中添加 open microphone 字元串。
- iOS 平台:設定 Graphics APIs 為 OpenGLES2,去掉多線程渲染 Multithreaded Rendering。 iOS 導出為 xcode 工程,在 xcode 工程中需要先關閉 bitcode,在 info.plist 檔案中添加麥克風和相機通路,并且在 Embed Framework 中添加 AliRTCSdk.framework,開啟 background 模式,以上設定也可以在 Unity 打包後處理腳本中添加導出的工程設定。
- Android 平台:Android 應用導出前,切換到 Android Platform,選擇 Player Settings,在 Player 的 Other Settings 選項中,設定 Graphics APIS 為 OpenGLES2,去掉多線程渲染 Multithreaded Rendering,Scripting Backend 選擇 IL2CPP。
說明 預設 Android 打包 ARMv7 版本,如果打包 ARM64 版本,需要選中 Target Architectures 中 ARM64 選項。
選中該 Export Project 後導出目錄需要有子級檔案夾,例如可導出在以下目錄:Aliyun/UnityRTC。
- Windows 平台:Windows 應用導出前,切換到 PC Platform,選擇 Player Settings,在 Player 的 Scripting Backend 選項選中 Mono。
「視訊雲技術」你最值得關注的音視訊技術公衆号,每周推送來自阿裡雲一線的實踐技術文章,在這裡與音視訊領域一流工程師交流切磋。