using System; using System.Linq; using UnityEngine; using UnityEngine.UI; using Agora.Rtc; using Agora.Util; using UnityEngine.Serialization; using Logger = Agora.Util.Logger; namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.ScreenShare { public class ScreenShare : MonoBehaviour { [FormerlySerializedAs("appIdInput")] [SerializeField] private AppIdInput _appIdInput; [Header("_____________Basic Configuration_____________")] [FormerlySerializedAs("APP_ID")] [SerializeField] private string _appID = ""; [FormerlySerializedAs("TOKEN")] [SerializeField] private string _token = ""; [FormerlySerializedAs("CHANNEL_NAME")] [SerializeField] private string _channelName = ""; public Text LogText; internal Logger Log; internal IRtcEngine RtcEngine = null; private Dropdown _winIdSelect; private Button _startShareBtn; private Button _stopShareBtn; private Button _updateShareBtn; private Button _publishBtn; private Button _unpublishBtn; // Use this for initialization private void Start() { LoadAssetData(); if (CheckAppId()) { EnableUI(); InitEngine(); #if UNITY_ANDROID || UNITY_IPHONE GameObject.Find("winIdSelect").SetActive(false); _updateShareBtn.gameObject.SetActive(true); #else _updateShareBtn.gameObject.SetActive(false); PrepareScreenCapture(); #endif JoinChannel(); } } private bool CheckAppId() { Log = new Logger(LogText); return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset"); } //Show data in AgoraBasicProfile [ContextMenu("ShowAgoraBasicProfileData")] private void LoadAssetData() { if (_appIdInput == null) return; _appID = _appIdInput.appID; _token = _appIdInput.token; _channelName = _appIdInput.channelName; } private void JoinChannel() { RtcEngine.EnableAudio(); RtcEngine.EnableVideo(); RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); var ret = RtcEngine.JoinChannel(_token, _channelName); Debug.Log("JoinChannel returns: " + ret); } private void OnPublishButtonClick() { ChannelMediaOptions options = new ChannelMediaOptions(); options.publishCameraTrack.SetValue(false); options.publishScreenTrack.SetValue(true); #if UNITY_ANDROID || UNITY_IPHONE options.publishScreenCaptureAudio.SetValue(true); options.publishScreenCaptureVideo.SetValue(true); #endif var ret = RtcEngine.UpdateChannelMediaOptions(options); Debug.Log("UpdateChannelMediaOptions returns: " + ret); _publishBtn.gameObject.SetActive(false); _unpublishBtn.gameObject.SetActive(true); } private void OnUnplishButtonClick() { ChannelMediaOptions options = new ChannelMediaOptions(); options.publishCameraTrack.SetValue(true); options.publishScreenTrack.SetValue(false); #if UNITY_ANDROID || UNITY_IPHONE options.publishScreenCaptureAudio.SetValue(false); options.publishScreenCaptureVideo.SetValue(false); #endif var ret = RtcEngine.UpdateChannelMediaOptions(options); Debug.Log("UpdateChannelMediaOptions returns: " + ret); _publishBtn.gameObject.SetActive(true); _unpublishBtn.gameObject.SetActive(false); } private void InitEngine() { RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine(); UserEventHandler handler = new UserEventHandler(this); RtcEngineContext context = new RtcEngineContext(_appID, 0, CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT); RtcEngine.Initialize(context); RtcEngine.InitEventHandler(new UserEventHandler(this)); } private void PrepareScreenCapture() { _winIdSelect = GameObject.Find("winIdSelect").GetComponent(); if (_winIdSelect == null || RtcEngine == null) return; _winIdSelect.ClearOptions(); SIZE t = new SIZE(); t.width = 360; t.height = 240; SIZE s = new SIZE(); s.width = 360; s.height = 240; var info = RtcEngine.GetScreenCaptureSources(t, s, true); _winIdSelect.AddOptions(info.Select(w => new Dropdown.OptionData( string.Format("{0}: {1}-{2} | {3}", w.type, w.sourceName, w.sourceTitle, w.sourceId))) .ToList()); } private void EnableUI() { _startShareBtn = GameObject.Find("startShareBtn").GetComponent