我正在开发一个类似于unity个智力竞赛游戏的应用程序,其中包括一些matchmaking和multiplayer功能.我的问题是,我已经设置了Firebase身份验证,然后我已经设置了Firebase实时数据库,让点击开始按钮的玩家,之后他会看到搜索游戏标签来匹配其他玩家,我现在正在使用不同的设备进行测试,所以当玩家1点击开始按钮,然后玩家2点击开始按钮时,他们两个都应该进入游戏场景,但在我的情况下,只有玩家2可以进入游戏场景,而玩家1停留在同一场景.我想知道这里有什么问题,这是我第一次做在线应用程序/游戏,所以我还没有
using UnityEngine;
using UnityEngine.SceneManagement;
using Firebase;
using Firebase.Database;
using Firebase.Auth;
using System;
using System.Collections.Generic;
public class MatchmakingManager : MonoBehaviour
{
private DatabaseReference databaseReference;
private FirebaseAuth auth;
private FirebaseUser currentUser;
private bool isMatchmaking = false;
private bool sceneTransitioned = false;
void Start()
{
// Initialize Firebase components
databaseReference = FirebaseDatabase.DefaultInstance.RootReference;
auth = FirebaseAuth.DefaultInstance;
currentUser = auth.CurrentUser;
if (currentUser != null)
{
// Check if the current user is already in matchmaking
CheckMatchmakingStatus();
}
else
{
Debug.LogError("User is not logged in.");
}
}
private void CheckMatchmakingStatus()
{
// Check if the current user is already in matchmaking
databaseReference.Child("matchmaking").Child(currentUser.UserId).GetValueAsync().ContinueWith(task =>
{
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled)
{
DataSnapshot snapshot = task.Result;
if (snapshot != null && snapshot.Exists)
{
// User is already in matchmaking
isMatchmaking = true;
}
else
{
// User is not in matchmaking
isMatchmaking = false;
}
}
else
{
Debug.LogError("Failed to check matchmaking status: " + task.Exception);
}
});
}
public void StartMatchmaking()
{
if (!isMatchmaking)
{
// Add the current user to matchmaking
databaseReference.Child("matchmaking").Child(currentUser.UserId).SetValueAsync(true).ContinueWith(task =>
{
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled)
{
// Successfully added to matchmaking
isMatchmaking = true;
// Check for available matches
FindMatch();
}
else
{
Debug.LogError("Failed to start matchmaking: " + task.Exception);
}
});
}
else
{
Debug.LogWarning("User is already in matchmaking.");
}
}
public void CancelMatchmaking()
{
// Remove the player from the matchmaking pool
string playerId = auth.CurrentUser.UserId;
databaseReference.Child("matchmaking").Child(playerId).RemoveValueAsync().ContinueWith(task =>
{
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled)
{
// Successfully removed from matchmaking
isMatchmaking = false;
}
else
{
Debug.LogError("Failed to cancel matchmaking: " + task.Exception);
}
});
}
private void FindMatch()
{
// Query the database for game rooms with two players
databaseReference.Child("matchmaking").OrderByKey().LimitToFirst(2).GetValueAsync().ContinueWith(task =>
{
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled)
{
DataSnapshot snapshot = task.Result;
if (snapshot != null && snapshot.HasChildren && snapshot.ChildrenCount == 2)
{
// Found two players, remove them from matchmaking
List<string> playerIds = new List<string>();
foreach (DataSnapshot playerSnapshot in snapshot.Children)
{
string playerId = playerSnapshot.Key;
playerIds.Add(playerId);
}
// Create a game room for the matched players
CreateGameRoom(playerIds[0], playerIds[1]);
}
else
{
// No available matches yet or insufficient players in the queue
Debug.Log("No available matches yet or insufficient players in the queue, continuing waiting...");
}
}
else
{
Debug.LogError("Failed to find a match: " + task.Exception);
}
});
}
private void CreateGameRoom(string player1Id, string player2Id)
{
// Generate a unique ID for the game room
string roomId = databaseReference.Child("gameRooms").Push().Key;
// Create a data structure representing the game room
Dictionary<string, object> roomData = new Dictionary<string, object>();
roomData["player1Id"] = player1Id;
roomData["player2Id"] = player2Id;
roomData["status"] = "active"; // You can set initial status to "active" or "waiting" or any other status you prefer
// Set the data in the database under the generated room ID
databaseReference.Child("gameRooms").Child(roomId).SetValueAsync(roomData)
.ContinueWith(task =>
{
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled)
{
Debug.Log("Game room created successfully!");
// Transition the matched players to the gameplay scene only if it hasn't been done before
if (!sceneTransitioned)
{
sceneTransitioned = true;
SceneManager.LoadScene("GamePlay");
}
}
else if (task.IsFaulted)
{
// Handle the error
foreach (Exception exception in task.Exception.InnerExceptions)
{
Debug.LogError("Failed to create game room: " + exception.Message);
}
}
else if (task.IsCanceled)
{
Debug.LogWarning("Creating game room task was canceled.");
}
else
{
Debug.LogError("Unknown error occurred while creating game room.");
}
});
}
}
以前有过什么经历吗.
我希望两个玩家都go 游戏现场,然后我需要处理其他功能,如分数,玩家姓名等.