HelloUnity3D.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. #if(UNITY_2018_3_OR_NEWER)
  4. using UnityEngine.Android;
  5. #endif
  6. using agora_gaming_rtc;
  7. public class HelloUnity3D : MonoBehaviour
  8. {
  9. public InputField mChannelNameInputField;
  10. public Text mShownMessage;
  11. public Text versionText;
  12. public Button joinChannel;
  13. public Button leaveChannel;
  14. public Button muteButton;
  15. private IRtcEngine mRtcEngine = null;
  16. // PLEASE KEEP THIS App ID IN SAFE PLACE
  17. // Get your own App ID at https://dashboard.agora.io/
  18. // After you entered the App ID, remove ## outside of Your App ID
  19. [SerializeField]
  20. private string AppID = "app_id";
  21. void Awake()
  22. {
  23. QualitySettings.vSyncCount = 0;
  24. Application.targetFrameRate = 30;
  25. muteButton.enabled = false;
  26. CheckAppId();
  27. }
  28. // Use this for initialization
  29. void Start()
  30. {
  31. #if (UNITY_2018_3_OR_NEWER)
  32. if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
  33. {
  34. }
  35. else
  36. {
  37. Permission.RequestUserPermission(Permission.Microphone);
  38. }
  39. #endif
  40. joinChannel.onClick.AddListener(JoinChannel);
  41. leaveChannel.onClick.AddListener(LeaveChannel);
  42. muteButton.onClick.AddListener(MuteButtonTapped);
  43. mRtcEngine = IRtcEngine.GetEngine(AppID);
  44. versionText.GetComponent<Text>().text = "Version : " + getSdkVersion();
  45. mRtcEngine.OnJoinChannelSuccess += (string channelName, uint uid, int elapsed) =>
  46. {
  47. string joinSuccessMessage = string.Format("joinChannel callback uid: {0}, channel: {1}, version: {2}", uid, channelName, getSdkVersion());
  48. Debug.Log(joinSuccessMessage);
  49. mShownMessage.GetComponent<Text>().text = (joinSuccessMessage);
  50. muteButton.enabled = true;
  51. };
  52. mRtcEngine.OnLeaveChannel += (RtcStats stats) =>
  53. {
  54. string leaveChannelMessage = string.Format("onLeaveChannel callback duration {0}, tx: {1}, rx: {2}, tx kbps: {3}, rx kbps: {4}", stats.duration, stats.txBytes, stats.rxBytes, stats.txKBitRate, stats.rxKBitRate);
  55. Debug.Log(leaveChannelMessage);
  56. mShownMessage.GetComponent<Text>().text = (leaveChannelMessage);
  57. muteButton.enabled = false;
  58. // reset the mute button state
  59. if (isMuted)
  60. {
  61. MuteButtonTapped();
  62. }
  63. };
  64. mRtcEngine.OnUserJoined += (uint uid, int elapsed) =>
  65. {
  66. string userJoinedMessage = string.Format("onUserJoined callback uid {0} {1}", uid, elapsed);
  67. Debug.Log(userJoinedMessage);
  68. };
  69. mRtcEngine.OnUserOffline += (uint uid, USER_OFFLINE_REASON reason) =>
  70. {
  71. string userOfflineMessage = string.Format("onUserOffline callback uid {0} {1}", uid, reason);
  72. Debug.Log(userOfflineMessage);
  73. };
  74. mRtcEngine.OnVolumeIndication += (AudioVolumeInfo[] speakers, int speakerNumber, int totalVolume) =>
  75. {
  76. if (speakerNumber == 0 || speakers == null)
  77. {
  78. Debug.Log(string.Format("onVolumeIndication only local {0}", totalVolume));
  79. }
  80. for (int idx = 0; idx < speakerNumber; idx++)
  81. {
  82. string volumeIndicationMessage = string.Format("{0} onVolumeIndication {1} {2}", speakerNumber, speakers[idx].uid, speakers[idx].volume);
  83. Debug.Log(volumeIndicationMessage);
  84. }
  85. };
  86. mRtcEngine.OnUserMutedAudio += (uint uid, bool muted) =>
  87. {
  88. string userMutedMessage = string.Format("onUserMuted callback uid {0} {1}", uid, muted);
  89. Debug.Log(userMutedMessage);
  90. };
  91. mRtcEngine.OnWarning += (int warn, string msg) =>
  92. {
  93. string description = IRtcEngine.GetErrorDescription(warn);
  94. string warningMessage = string.Format("onWarning callback {0} {1} {2}", warn, msg, description);
  95. Debug.Log(warningMessage);
  96. };
  97. mRtcEngine.OnError += (int error, string msg) =>
  98. {
  99. string description = IRtcEngine.GetErrorDescription(error);
  100. string errorMessage = string.Format("onError callback {0} {1} {2}", error, msg, description);
  101. Debug.Log(errorMessage);
  102. };
  103. mRtcEngine.OnRtcStats += (RtcStats stats) =>
  104. {
  105. string rtcStatsMessage = string.Format("onRtcStats callback duration {0}, tx: {1}, rx: {2}, tx kbps: {3}, rx kbps: {4}, tx(a) kbps: {5}, rx(a) kbps: {6} users {7}",
  106. stats.duration, stats.txBytes, stats.rxBytes, stats.txKBitRate, stats.rxKBitRate, stats.txAudioKBitRate, stats.rxAudioKBitRate, stats.userCount);
  107. Debug.Log(rtcStatsMessage);
  108. int lengthOfMixingFile = mRtcEngine.GetAudioMixingDuration();
  109. int currentTs = mRtcEngine.GetAudioMixingCurrentPosition();
  110. string mixingMessage = string.Format("Mixing File Meta {0}, {1}", lengthOfMixingFile, currentTs);
  111. Debug.Log(mixingMessage);
  112. };
  113. mRtcEngine.OnAudioRouteChanged += (AUDIO_ROUTE route) =>
  114. {
  115. string routeMessage = string.Format("onAudioRouteChanged {0}", route);
  116. Debug.Log(routeMessage);
  117. };
  118. mRtcEngine.OnRequestToken += () =>
  119. {
  120. string requestKeyMessage = string.Format("OnRequestToken");
  121. Debug.Log(requestKeyMessage);
  122. };
  123. mRtcEngine.OnConnectionInterrupted += () =>
  124. {
  125. string interruptedMessage = string.Format("OnConnectionInterrupted");
  126. Debug.Log(interruptedMessage);
  127. };
  128. mRtcEngine.OnConnectionLost += () =>
  129. {
  130. string lostMessage = string.Format("OnConnectionLost");
  131. Debug.Log(lostMessage);
  132. };
  133. mRtcEngine.SetLogFilter(LOG_FILTER.INFO);
  134. mRtcEngine.SetChannelProfile(CHANNEL_PROFILE.CHANNEL_PROFILE_COMMUNICATION);
  135. // mRtcEngine.SetChannelProfile (CHANNEL_PROFILE.CHANNEL_PROFILE_LIVE_BROADCASTING);
  136. // mRtcEngine.SetClientRole (CLIENT_ROLE.BROADCASTER);
  137. }
  138. private void CheckAppId()
  139. {
  140. Debug.Assert(AppID.Length > 10, "Please fill in your AppId first on Game Controller object.");
  141. GameObject go = GameObject.Find("AppIDText");
  142. if (go != null)
  143. {
  144. Text appIDText = go.GetComponent<Text>();
  145. if (appIDText != null)
  146. {
  147. if (string.IsNullOrEmpty(AppID))
  148. {
  149. appIDText.text = "AppID: " + "UNDEFINED!";
  150. appIDText.color = Color.red;
  151. }
  152. else
  153. {
  154. appIDText.text = "AppID: " + AppID.Substring(0, 4) + "********" + AppID.Substring(AppID.Length - 4, 4);
  155. }
  156. }
  157. }
  158. }
  159. public void JoinChannel()
  160. {
  161. string channelName = mChannelNameInputField.text.Trim();
  162. Debug.Log(string.Format("tap joinChannel with channel name {0}", channelName));
  163. if (string.IsNullOrEmpty(channelName))
  164. {
  165. return;
  166. }
  167. mRtcEngine.JoinChannel(channelName, "extra", 0);
  168. }
  169. public void LeaveChannel()
  170. {
  171. mRtcEngine.LeaveChannel();
  172. string channelName = mChannelNameInputField.text.Trim();
  173. Debug.Log(string.Format("left channel name {0}", channelName));
  174. }
  175. void OnApplicationQuit()
  176. {
  177. if (mRtcEngine != null)
  178. {
  179. IRtcEngine.Destroy();
  180. }
  181. }
  182. public string getSdkVersion()
  183. {
  184. string ver = IRtcEngine.GetSdkVersion();
  185. return ver;
  186. }
  187. bool isMuted = false;
  188. void MuteButtonTapped()
  189. {
  190. string labeltext = isMuted ? "Mute" : "Unmute";
  191. Text label = muteButton.GetComponentInChildren<Text>();
  192. if (label != null)
  193. {
  194. label.text = labeltext;
  195. }
  196. isMuted = !isMuted;
  197. mRtcEngine.EnableLocalAudio(!isMuted);
  198. }
  199. }