You need to enable JavaScript to run this app.
导航
场景搭建(抖音小游戏)
最近更新时间:2023.02.23 21:10:31首次发布时间:2023.02.23 21:10:31

SDK集成

为了保证最佳体验效果,本场景需要集成抖音开放平台并接入 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");
    }

核心功能 API 与回调参考

功能点抖音小游戏RTC
创建引擎GetRtcEngine
加入房间JoinChannel
离开房间LeaveChannel
开启麦克风采集EnableLocalAudio
关闭麦克风采集DisableLocalAudio
开启音频流发送UnMuteLocalAudioStream
关闭音频流发送MuteLocalAudioStream
开启所有远端用户音频拉流UnMuteAllRemoteAudioStream
关闭所有远端用户音频拉流MuteAllRemoteAudioStream
销毁引擎Destroy
功能点抖音小游戏RTC
开启音量回调EnableAudioVolumeIndication
本地用户加房成功回调OnJoinChannelSuccessEvent
远端用户加房成功回调OnUserJoinedEvent
RTC错误信息回调OnErrorEvent
麦克风音量回调OnAudioVolumeIndicationEvent