123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- using UnityEngine;
- using UnityEngine.UI;
- using agora_gaming_rtc;
- using agora_utilities;
- // this is an example of using Agora Unity SDK
- // It demonstrates:
- // How to enable video
- // How to join/leave channel
- //
- public class TestHelloUnityVideo
- {
- // instance of agora engine
- private IRtcEngine mRtcEngine;
- private Text MessageText;
- // a token is a channel key that works with a AppID that requires it.
- // Generate one by your token server or get a temporary token from the developer console
- private string token = "";
- // load agora engine
- public void loadEngine(string appId)
- {
- // start sdk
- Debug.Log("initializeEngine");
- if (mRtcEngine != null)
- {
- Debug.Log("Engine exists. Please unload it first!");
- return;
- }
- // init engine
- mRtcEngine = IRtcEngine.GetEngine(appId);
- // enable log
- mRtcEngine.SetLogFilter(LOG_FILTER.DEBUG | LOG_FILTER.INFO | LOG_FILTER.WARNING | LOG_FILTER.ERROR | LOG_FILTER.CRITICAL);
- }
- public void join(string channel)
- {
- Debug.Log("calling join (channel = " + channel + ")");
- if (mRtcEngine == null)
- return;
- // set callbacks (optional)
- mRtcEngine.OnJoinChannelSuccess = onJoinChannelSuccess;
- mRtcEngine.OnUserJoined = onUserJoined;
- mRtcEngine.OnUserOffline = onUserOffline;
- mRtcEngine.OnWarning = (int warn, string msg) =>
- {
- Debug.LogWarningFormat("Warning code:{0} msg:{1}", warn, IRtcEngine.GetErrorDescription(warn));
- };
- mRtcEngine.OnError = HandleError;
- // enable video
- mRtcEngine.EnableVideo();
- // allow camera output callback
- mRtcEngine.EnableVideoObserver();
- // join channel
- /* This API Assumes the use of a test-mode AppID
- mRtcEngine.JoinChannel(channel, null, 0);
- */
- /* This API Accepts AppID with token; by default omiting info and use 0 as the local user id */
- mRtcEngine.JoinChannelByKey(channelKey: token, channelName: channel);
- }
- public string getSdkVersion()
- {
- string ver = IRtcEngine.GetSdkVersion();
- return ver;
- }
- public void leave()
- {
- Debug.Log("calling leave");
- if (mRtcEngine == null)
- return;
- // leave channel
- mRtcEngine.LeaveChannel();
- // deregister video frame observers in native-c code
- mRtcEngine.DisableVideoObserver();
- }
- // unload agora engine
- public void unloadEngine()
- {
- Debug.Log("calling unloadEngine");
- // delete
- if (mRtcEngine != null)
- {
- IRtcEngine.Destroy(); // Place this call in ApplicationQuit
- mRtcEngine = null;
- }
- }
- public void EnableVideo(bool pauseVideo)
- {
- if (mRtcEngine != null)
- {
- if (!pauseVideo)
- {
- mRtcEngine.EnableVideo();
- }
- else
- {
- mRtcEngine.DisableVideo();
- }
- }
- }
- // accessing GameObject in Scnene1
- // set video transform delegate for statically created GameObject
- public void onSceneHelloVideoLoaded()
- {
- // Attach the SDK Script VideoSurface for video rendering
- GameObject quad = GameObject.Find("Quad");
- if (ReferenceEquals(quad, null))
- {
- Debug.Log("failed to find Quad");
- return;
- }
- else
- {
- quad.AddComponent<VideoSurface>();
- }
- GameObject cube = GameObject.Find("Cube");
- if (ReferenceEquals(cube, null))
- {
- Debug.Log("failed to find Cube");
- return;
- }
- else
- {
- cube.AddComponent<VideoSurface>();
- }
- GameObject text = GameObject.Find("MessageText");
- if (!ReferenceEquals(text, null))
- {
- MessageText = text.GetComponent<Text>();
- }
- GameObject bobj = GameObject.Find("HelpButton");
- if (bobj != null)
- {
- Button button = bobj.GetComponent<Button>();
- if (button!=null)
- {
- button.onClick.AddListener(HandleHelp);
- }
- }
- }
- void HandleHelp()
- {
- #if UNITY_2020_3_OR_NEWER && PLATFORM_STANDALONE_OSX
- // this very easy to forget for MacOS
- HandleError(-2, "if you don't see any video, did you set the MacOS plugin bundle to AnyCPU?");
- #else
- HandleError(-1, "if you don't see any video, please check README for help");
- #endif
- }
- // implement engine callbacks
- private void onJoinChannelSuccess(string channelName, uint uid, int elapsed)
- {
- Debug.Log("JoinChannelSuccessHandler: uid = " + uid);
- GameObject textVersionGameObject = GameObject.Find("VersionText");
- textVersionGameObject.GetComponent<Text>().text = "SDK Version : " + getSdkVersion();
- }
- // When a remote user joined, this delegate will be called. Typically
- // create a GameObject to render video on it
- private void onUserJoined(uint uid, int elapsed)
- {
- Debug.Log("onUserJoined: uid = " + uid + " elapsed = " + elapsed);
- // this is called in main thread
- // find a game object to render video stream from 'uid'
- GameObject go = GameObject.Find(uid.ToString());
- if (!ReferenceEquals(go, null))
- {
- return; // reuse
- }
- // create a GameObject and assign to this new user
- VideoSurface videoSurface = makeImageSurface(uid.ToString());
- if (!ReferenceEquals(videoSurface, null))
- {
- // configure videoSurface
- videoSurface.SetForUser(uid);
- videoSurface.SetEnable(true);
- videoSurface.SetVideoSurfaceType(AgoraVideoSurfaceType.RawImage);
- }
- }
- public VideoSurface makePlaneSurface(string goName)
- {
- GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane);
- if (go == null)
- {
- return null;
- }
- go.name = goName;
- // set up transform
- go.transform.Rotate(-90.0f, 0.0f, 0.0f);
- float yPos = Random.Range(3.0f, 5.0f);
- float xPos = Random.Range(-2.0f, 2.0f);
- go.transform.position = new Vector3(xPos, yPos, 0f);
- go.transform.localScale = new Vector3(0.25f, 0.5f, .5f);
- // configure videoSurface
- VideoSurface videoSurface = go.AddComponent<VideoSurface>();
- return videoSurface;
- }
- private const float Offset = 100;
- public VideoSurface makeImageSurface(string goName)
- {
- GameObject go = new GameObject();
- if (go == null)
- {
- return null;
- }
- go.name = goName;
- // to be renderered onto
- go.AddComponent<RawImage>();
- // make the object draggable
- go.AddComponent<UIElementDragger>();
- GameObject canvas = GameObject.Find("Canvas");
- if (canvas != null)
- {
- go.transform.parent = canvas.transform;
- }
- // set up transform
- go.transform.Rotate(0f, 0.0f, 180.0f);
- float xPos = Random.Range(Offset - Screen.width / 2f, Screen.width / 2f - Offset);
- float yPos = Random.Range(Offset, Screen.height / 2f - Offset);
- go.transform.localPosition = new Vector3(xPos, yPos, 0f);
- go.transform.localScale = new Vector3(3f, 4f, 1f);
- // configure videoSurface
- VideoSurface videoSurface = go.AddComponent<VideoSurface>();
- return videoSurface;
- }
- // When remote user is offline, this delegate will be called. Typically
- // delete the GameObject for this user
- private void onUserOffline(uint uid, USER_OFFLINE_REASON reason)
- {
- // remove video stream
- Debug.Log("onUserOffline: uid = " + uid + " reason = " + reason);
- // this is called in main thread
- GameObject go = GameObject.Find(uid.ToString());
- if (!ReferenceEquals(go, null))
- {
- Object.Destroy(go);
- }
- }
- #region Error Handling
- private int LastError { get; set; }
- private void HandleError(int error, string msg)
- {
- if (error == LastError)
- {
- return;
- }
- if (string.IsNullOrEmpty(msg))
- {
- msg = string.Format("Error code:{0} msg:{1}", error, IRtcEngine.GetErrorDescription(error));
- }
- switch (error)
- {
- case 101:
- msg += "\nPlease make sure your AppId is valid and it does not require a certificate for this demo.";
- break;
- }
- Debug.LogError(msg);
- if (MessageText != null)
- {
- if (MessageText.text.Length > 0)
- {
- msg = "\n" + msg;
- }
- MessageText.text += msg;
- }
- LastError = error;
- }
- #endregion
- }
|