为了保证最佳体验效果,本场景需要集成抖音开放平台并接入 RTC 服务,您需要在 RTC 的控制台开通服务,并搭建抖音开放平台开发环境,相应开通指南如下:
房间列表逻辑
游戏业务逻辑
房间列表页,用户可加入或者创建游戏房间
开始游戏时,服务端为每个玩家分配角色
通过广播控制狼人杀流程以及管理不同角色的麦位
//初始化RTCEngine并加入房间 private void initRTCEngine() { var appid = RoomDataManager.Instance.RoomData.app_id; var roomid = RoomDataManager.Instance.RoomData.room_id; var userid = RoomDataManager.Instance.MyUserid; var token = RoomDataManager.Instance.RtcToken; StarkSDK.Init(); // 获取引擎 RtcEngine = StarkSDK.API.GetRTCEngineManager().GetRtcEngine(appid); // 注册监听 RtcEngine.OnJoinChannelSuccessEvent += OnJoinChannelSuccess; RtcEngine.OnUserJoinedEvent += OnUserJoined; RtcEngine.OnUserOfflineEvent += OnUserOffline; RtcEngine.OnUserMuteAudioEvent += OnUserMuteAudio; RtcEngine.OnWarningEvent += OnWarning; RtcEngine.OnErrorEvent += OnError; RtcEngine.OnAudioVolumeIndicationEvent += OnAudioVolumeIndication; // 加入房间 RtcEngine.JoinChannel(roomid, userid, token, () => { }, (code, reason) => { }); } // 加入房间成功回调 private void OnJoinChannelSuccess(int elapsed) { Debug.Log(string.Format("OnJoinChannelSuccess")); // 开启音频采集 RtcEngine.EnableLocalAudio(); // 关闭音频发送 RtcEngine.MuteLocalAudioStream(); // 开启音量回调 RtcEngine.EnableAudioVolumeIndication(1000, null, null); } //按下说话按钮事件 private void addSpeakingButtonEvent() { var touchEventTrigger = SpeakingButton.GetComponent<ButtonTouchEventTrigger>(); if (touchEventTrigger == null) { touchEventTrigger = SpeakingButton.gameObject.AddComponent<ButtonTouchEventTrigger>(); } touchEventTrigger.OnPressDown = startSpeaking; touchEventTrigger.OnPressUp = endSpeaking; } // 开启音频发送 private void startSpeaking() { RtcEngine.UnMuteLocalAudioStream(); isMuteMic = false; } // 关闭音频发送 private void endSpeaking() { RtcEngine.MuteLocalAudioStream(); isMuteMic = true; } // 音量回调 private void OnAudioVolumeIndication(string uid, int volume, int speakerNum) { Debug.Log(string.Format("OnAudioVolumeIndication")); if (RoomDataManager.Instance.MyUserid == uid) { if (isMuteMic) { MicBack.SetActive(false); return; } MicBack.SetActive(volume > 0); BarController.UpdateVolume(volume, 255); return; } for (int i = 0; i < RoomDataManager.Instance.UserList.Count; i++) { if (i > 2) { return; } NetWorkManager.UserObject user = RoomDataManager.Instance.UserList[i]; if (user.user_id != uid) { continue; } var playerController = PlayerControllers[i]; playerController.SpeakingImage.SetActive(volume > 0); playerController.BarController.UpdateVolume(volume, 255); return; } }
所有玩家加入一个RTC房间
天黑,狼人可以互相对话,且其他角色无法收听到对话内容
开始游戏(全部静音) | 天黑(狼人可以互相通话) | 天亮(所有玩家可通话) | |
---|---|---|---|
狼人 | 开启麦克风采集 | 开启本地音频发送 | |
平民 | 开启麦克风采集 | 取消订阅所有远端用户的音频流 | 开启订阅所有远端用户音频流 |
//游戏阶段改变 private void changeGameStatus(GameStatus gameStatus) { RoomDataManager.Instance.GameStatus = gameStatus; RtcEngine.MuteLocalAudioStream(); isMuteMic = true; if (RoomDataManager.Instance.GameStatus == GameStatus.WolfSpeaking && RoomDataManager.Instance.RoleType == RoleType.Person) { RtcEngine.MuteAllRemoteAudioStream(); } else { RtcEngine.UnMuteAllRemoteAudioStream(); } refreshBackgroundUI(); refreshBottomUI(); refreshTipsUI(); } //退出RTC房间 private void leaveRoom() { RtcEngine.OnJoinChannelSuccessEvent -= OnJoinChannelSuccess; RtcEngine.OnUserJoinedEvent -= OnUserJoined; RtcEngine.OnUserOfflineEvent -= OnUserOffline; RtcEngine.OnUserMuteAudioEvent -= OnUserMuteAudio; RtcEngine.OnWarningEvent -= OnWarning; RtcEngine.OnErrorEvent -= OnError; RtcEngine.OnAudioVolumeIndicationEvent -= OnAudioVolumeIndication; // 关闭音频发送 RtcEngine.MuteLocalAudioStream(); // 关闭音频采集 RtcEngine.DisableLocalAudio(); // 离开房间 RtcEngine.LeaveChannel(); NetWorkManager.Instance.OffDisconnected(OnDisconnected); NetWorkManager.Instance.RemoveAllNotification(); RoomDataManager.Instance.ClearData(); RoomDataManager.Instance.OnUserJoinRoom = null; SceneManager.LoadScene("RoomListScene"); }
功能点 | 抖音小游戏RTC |
---|---|
创建引擎 | GetRtcEngine |
加入房间 | JoinChannel |
离开房间 | LeaveChannel |
开启麦克风采集 | EnableLocalAudio |
关闭麦克风采集 | DisableLocalAudio |
开启音频流发送 | UnMuteLocalAudioStream |
关闭音频流发送 | MuteLocalAudioStream |
开启所有远端用户音频拉流 | UnMuteAllRemoteAudioStream |
关闭所有远端用户音频拉流 | MuteAllRemoteAudioStream |
销毁引擎 | Destroy |
功能点 | 抖音小游戏RTC |
---|---|
开启音量回调 | EnableAudioVolumeIndication |
本地用户加房成功回调 | OnJoinChannelSuccessEvent |
远端用户加房成功回调 | OnUserJoinedEvent |
RTC错误信息回调 | OnErrorEvent |
麦克风音量回调 | OnAudioVolumeIndicationEvent |