【问题标题】:Photon, PlayFab, & Unity3D - Players not appearing to one another after joining Photon RoomPhoton、PlayFab 和 Unity3D - 玩家在加入 Photon Room 后不会互相出现
【发布时间】:2018-10-08 08:50:10
【问题描述】:

我正在 Unity 中开发一款多人游戏,该游戏使用 Playfab 以及托管多人游戏的 Authentication and Photon。我可以成功让玩家进入同一个房间,并且可以在玩家“加入”房间后加载场景,但是,当两个玩家在同一个房间时,他们看不到对方。

这是我的身份验证服务:

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);
}

这会成功加载我命名为“Test_scene1”的场景,并在我的场景中显示房间名称和房间中活跃玩家的数量。当我运行和构建时,我让用户的 playerPrefab 加载到房间中。当我通过 unity 运行游戏时,我可以让第二个玩家登录房间。问题是,球员们看不到对方,我不知道为什么会这样。我正在关注他们各自网站上的 PLayerfab/Photon 教程,但我找不到任何我做错的地方。

从我读到的内容看来,我的实例化方法可能是错误的,但我不知道为什么。下面是我的播放器 Prefab,显示了附加到它的组件:

对于这个巨大的问题,我深表歉意,我只是想提供尽可能多的信息。

【问题讨论】:

    标签: unity3d networking photon


    【解决方案1】:

    此问题由 PlayFab 论坛 here 上的 OP 回答。

    【讨论】:

    • 我真的不会称之为答案......从来没有真正发现它为什么会发生。总而言之,玩家预制件加载为“禁用”,这就是为什么我的玩家看不到对方。除了我必须在错误的时间或错误的线程在关卡加载之前实例化玩家之外,仍然无法理解为什么。
    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2020-12-18
    • 2019-11-10
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多