Google Play Game Services(GPGS)를 사용하는 Unity 프로젝트에서 친구 추가 기능을 구현할 때는 몇 가지 중요한 점이 있습니다:
❗ GPGS 친구 시스템 제한사항
- Google Play Game Services는 직접적인 '친구 추가' API를 제공하지 않습니다.
- 대신, Google+ 연결 기반으로 친구가 자동 동기화됩니다 (단, Google+ 서비스 종료 후 일부 기능은 deprecated 됨).
- 현재는 초대(invitation) 또는 멀티플레이어 방 참여 기반으로 유저 간 관계를 관리합니다.
✅ 해결 방법: 초대(invite) 기반 멀티플레이어
GPGS에서는 "친구" 개념보다는 게임 내 멀티플레이어 초대 기능을 통해 특정 유저와 플레이하는 방식을 지원합니다.
1. 멀티플레이어 초대 화면 표시
void ShowInviteUI()
{
const int MinOpponents = 1;
const int MaxOpponents = 3;
PlayGamesPlatform.Instance.RealTime.ShowInvitationsMenu();
}
또는 직접 초대 화면을 띄워서 다른 사람을 초대할 수 있습니다:
PlayGamesPlatform.Instance.RealTime.CreateWithInvitationScreen(
MinOpponents,
MaxOpponents,
OnRoomCreated);
2. 방 생성 및 초대 콜백 처리
private void OnRoomCreated(RealTimeRoom room)
{
if (room != null && room.Valid)
{
Debug.Log("방 생성 성공! ID: " + room.RoomId);
SceneManager.LoadScene("MultiplayerScene"); // 멀티플레이어 게임씬 로드
}
else
{
Debug.LogError("방 생성 실패");
}
}
✅ 해결 방법: 자체 서버에서 친구 관리
GPGS에서 직접적인 "친구 추가"가 불가능하므로, 일반적으로 아래와 같은 방식으로 구현합니다:
1. GPGS 유저 ID 저장
로그인 후, 유저의 고유 ID를 얻어옵니다:
string userId = Social.localUser.id;
Debug.Log("내 ID: " + userId);
2. 서버 DB에 친구 정보 저장
예를 들어, REST API나 Firebase 등을 이용해 다음과 같이 저장:
{
"user_id": "gp_1234567890",
"friends": [
{ "friend_id": "gp_0987654321", "name": "John" },
{ "friend_id": "gp_1122334455", "name": "Alice" }
]
}
3. 친구 목록 조회
서버에서 해당 유저의 친구 목록을 가져오고, 각 친구의 friend_id를 바탕으로 GPGS 리더보드나 멀티플레이어 초대에 활용할 수 있습니다.
✅ 예제: 친구 목록에 추가하는 간단한 코드
[System.Serializable]
public class FriendData
{
public string friend_id;
public string name;
}
public List<FriendData> friends = new List<FriendData>();
public void AddFriend(string friendId, string friendName)
{
if (!friends.Exists(f => f.friend_id == friendId))
{
friends.Add(new FriendData { friend_id = friendId, name = friendName });
SaveFriends(); // PlayerPrefs or Server 저장
Debug.Log($"{friendName}님이 친구 목록에 추가되었습니다.");
}
else
{
Debug.Log("이미 친구입니다.");
}
}
// 예시: PlayerPrefs에 저장
void SaveFriends()
{
string json = JsonHelper.ToJson(friends.ToArray());
PlayerPrefs.SetString("friends_list", json);
PlayerPrefs.Save();
}
void LoadFriends()
{
if (PlayerPrefs.HasKey("friends_list"))
{
string json = PlayerPrefs.GetString("friends_list");
FriendData[] loadedFriends = JsonHelper.FromJson<FriendData>(json);
friends = new List<FriendData>(loadedFriends);
}
}