Photon,PlayFab,& Unity3D - 玩家在加入Photon Room后没有彼此出现

时间:2018-04-27 13:33:20

标签: unity3d networking photon

我正在Unity中使用Playfab以及托管多人游戏的身份验证和光子的多人游戏。我可以成功让玩家进入同一个房间,我可以在玩家加入后加载场景。然而,当两个玩家在同一个房间时,他们无法看到对方。

这是我的身份验证服务:

public class LoginWithCustomID : MonoBehaviour
{
    private string _playFabPlayerIdCache;
    private bool _isNewAccount;
    private string _playerName;

    // Use this to auth normally for PlayFab
    void Awake()
    {
        PhotonNetwork.autoJoinLobby = false;
        PhotonNetwork.automaticallySyncScene = true;
        DontDestroyOnLoad(gameObject);
        authenticateWithPlayfab();
    }

    private void authenticateWithPlayfab()
    {
        var request = new LoginWithCustomIDRequest
        {
            CustomId = "CustomId123",
            CreateAccount = true,
            InfoRequestParameters = new GetPlayerCombinedInfoRequestParams()
            {
                GetUserAccountInfo = true,
                ProfileConstraints = new PlayerProfileViewConstraints()
                { ShowDisplayName = true }
            }
        };
        PlayFabClientAPI.LoginWithCustomID(request, requestPhotonToken, OnLoginFailure);
    }

    private void requestPhotonToken(LoginResult result)
    {
        PlayerAccountService.loginResult = result;
        _playFabPlayerIdCache = result.PlayFabId;
        _playerName = result.InfoResultPayload.AccountInfo.TitleInfo.DisplayName;
        if (result.NewlyCreated)
        {
            _isNewAccount = true;
            setupNewPlayer(result);
        }

        PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
        {
            PhotonApplicationId = "photonId123"
        }, AuthenticateWithPhoton, OnLoginFailure);
    }

    private void setupNewPlayer(LoginResult result)
    {
        PlayFabClientAPI.UpdateUserData(
            new UpdateUserDataRequest()
            {
                Data = new Dictionary<string, string>()
                {
                    { "Level", "1" },
                    { "xp", "0" }
                }
            }, success =>
            {
                Debug.Log("Set User Data");
            }, failure =>
             {
                 Debug.Log("Failed to set User Data..");
             }
        );
    }

    private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult result)
    {
        Debug.Log("Photon token acquired: " + result.PhotonCustomAuthenticationToken);
        var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
        customAuth.AddAuthParameter("username", _playFabPlayerIdCache);
        customAuth.AddAuthParameter("token", result.PhotonCustomAuthenticationToken);
        PhotonNetwork.AuthValues = customAuth;
        setNextScene();            
    }

    private void setNextScene()
    {
        if(_isNewAccount || _playerName == null)
        {
            SceneManager.LoadSceneAsync("CreatePlayerName", LoadSceneMode.Single);
        }
        else
        {
            SceneManager.LoadSceneAsync("LandingScene", LoadSceneMode.Single);
        }
    }

    private void OnLoginFailure(PlayFabError error)
    {
        Debug.LogWarning("something went wrong in auth login");
        Debug.LogError("Here's some debug info:");
        Debug.LogError(error.GenerateErrorReport());
    }

}

}

这一切都有效,玩家登录PlayFab,以及Photon我会假设我得到了Photon身份验证令牌。这将我带到我的着陆场景,这是一个经过身份验证的用户点击按钮通过Photon加入随机房间的地方:

public static GameManager instance;
public static GameObject localPlayer;

private void Awake()
{
    if (instance != null)
    {
        DestroyImmediate(instance);
        return;
    }
    DontDestroyOnLoad(gameObject);
    instance = this;
    PhotonNetwork.automaticallySyncScene = true;
}

// Use this for initialization
void Start()
{
    PhotonNetwork.ConnectUsingSettings("A_0.0.1");
}

public void JoinGame()
{
    RoomOptions ro = new RoomOptions();
    ro.MaxPlayers = 4;
    PhotonNetwork.JoinOrCreateRoom("Test Room 2", ro, null);
}

public override void OnJoinedRoom()
{
    Debug.Log("Joined Room!");
    if (PhotonNetwork.isMasterClient)
    {
        PhotonNetwork.LoadLevel("Test_Map1");
    }
}

private void OnLevelWasLoaded(int level)
{
    if (!PhotonNetwork.inRoom)
        return;

    localPlayer = PhotonNetwork.Instantiate(
        "Player",
        new Vector3(0, 1f, 0),
        Quaternion.identity,
        0);
}

public void LeaveRoom()
{
    PhotonNetwork.LeaveRoom();
    SceneManager.LoadScene("LandingScene", LoadSceneMode.Single);
}

这会加载我命名的场景&#34; Test_scene1&#34;成功地,我在我的场景中显示了房间中的房间名称和活跃玩家的数量。当我进行运行和构建时,我会将用户的playerPrefab加载到房间中。当我通过团结运行游戏时,我可以让第二个玩家登录到房间。问题是,球员们没有看到彼此,我无法弄清楚为什么会这样。我正在他们各自的网站上关注PLayerfab / Photon教程,但我找不到任何一个我在其中任何一个做错的事。

从我看来,看起来我的实例化方法可能是错误的,但我不确定为什么。下面是我的播放器Prefab显示附加的组件:

Player Prefab

我为这个大问题道歉,我只想提供尽可能多的信息。

1 个答案:

答案 0 :(得分:0)

OP在PlayFab论坛here上回答了这个问题。