스팀 로그인
Last updated
Last updated
Steamworks.NET 에서 최신 버전을 다운로드 후에 Unity Import 합니다.
프로젝트 루트 폴더에 steam_appid.txt 파일을 생성 후에 스팀에서 발급받은 app ID 많을 넣고 저장해 주세요.
Users & Permissions > Manage Group > Create Group > Create Web API KEY 를 생성해 주세요.
Steam APP ID 와 Web API 키를 게임팟 대시보드 > 연동 > 스팀란에 추가해 주세요.
Assets > Scripts > Steamworks.NET > SteamManager.cs를 Hierarchy의 빈 오브젝스를 생성 추가합니다.
NBaseSDK.NBase.signInWithToken("steam", memberId, sessionTicket, (user, error) =>
{
try
{
if(error != null)
{
Debug.Log("Login Success - " + user.ToString());
}
memberId = user.Id;
}
catch (Exception ex)
{
Debug.Log($"An exception occurred during sign-in: {ex.Message}");
}
});
private Callback<GetAuthSessionTicketResponse_t> m_GetAuthSessionTicketResponse;
private byte[] m_Ticket;
private uint m_pcbTicket;
private HAuthTicket m_HAuthTicket;
// NBase Initialize 완료 후에 실행해 주세요.
private void Initialize()
{
try
{
InitializeSteamAuth();
// Steam이 초기화되었는지 확인
if (SteamManager.Initialized)
{
// Steam 로그인 상태 확인
if (SteamUser.BLoggedOn())
{
// 이미 로그인된 경우
CSteamID steamId = SteamUser.GetSteamID();
string steamName = SteamFriends.GetPersonaName();
Log($"Steam User Connected - Name: {steamName}, SteamID: {steamId}");
}
else
{
// 로그인되지 않은 경우
Log("Steam user not logged in. Opening Steam login overlay.");
ShowBox("Steam must be running to play this game.");
}
}
else
{
Log("Steam is not initialized. Please make sure Steam is running.");
ShowBox("Steam must be running to play this game.\nPlease start Steam and restart the game.");
}
}
catch (Exception ex)
{
Log($"Failed to initialize Steam login: {ex.Message}");
ShowBox("Failed to connect to Steam. Please restart the game.");
}
}
private void OnDestroy()
{
CleanupAuthTicket();
}
private void InitializeSteamAuth()
{
if (!SteamManager.Initialized)
{
Log("Steam is not initialized. Skipping auth ticket generation.");
return;
}
try
{
// Create callback for auth ticket response
m_GetAuthSessionTicketResponse = Callback<GetAuthSessionTicketResponse_t>.Create(OnGetAuthSessionTicketResponse);
// Initialize ticket buffer
m_Ticket = new byte[1024];
// Create SteamNetworkingIdentity instance
SteamNetworkingIdentity networkingIdentity = new SteamNetworkingIdentity();
// Get auth session ticket with networking identity
m_HAuthTicket = SteamUser.GetAuthSessionTicket(
m_Ticket,
1024,
out m_pcbTicket,
ref networkingIdentity
);
if (m_HAuthTicket == HAuthTicket.Invalid)
{
throw new Exception("Failed to get valid Steam auth ticket");
}
Log($"Successfully generated Steam auth ticket. Ticket size: {m_pcbTicket} bytes");
}
catch (Exception ex)
{
Log($"Error during Steam auth ticket generation: {ex.Message}");
CleanupAuthTicket();
}
}
private void OnGetAuthSessionTicketResponse(GetAuthSessionTicketResponse_t pCallback)
{
try
{
if (pCallback.m_hAuthTicket != m_HAuthTicket)
{
Log("Received response for different auth ticket");
return;
}
// Resize to buffer of actual ticket size
System.Array.Resize(ref m_Ticket, (int)m_pcbTicket);
// Format as Hex
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (byte b in m_Ticket)
{
sb.AppendFormat("{0:x2}", b);
}
sessionTicket = sb.ToString();
NBaseSDK.NBase.signInWithToken("steam", memberId, sessionTicket, (user, error) =>
{
try
{
if(error != null)
{
Debug.Log("Login Success - " + user.ToString());
}
memberId = user.Id;
}
catch (Exception ex)
{
Debug.Log($"An exception occurred during sign-in: {ex.Message}");
}
});
switch (pCallback.m_eResult)
{
case EResult.k_EResultOK:
Log("Steam auth ticket successfully generated");
break;
case EResult.k_EResultInvalidParam:
Log("Steam auth ticket generation failed: Invalid parameters");
break;
case EResult.k_EResultLimitExceeded:
Log("Steam auth ticket generation failed: Limit exceeded");
break;
default:
Log($"Steam auth ticket generation failed: {pCallback.m_eResult}");
break;
}
}
catch (Exception ex)
{
Log($"Error processing auth ticket response: {ex.Message}");
}
}
private void CleanupAuthTicket()
{
if (m_HAuthTicket != HAuthTicket.Invalid)
{
try
{
SteamUser.CancelAuthTicket(m_HAuthTicket);
m_HAuthTicket = HAuthTicket.Invalid;
m_Ticket = null;
Log("Steam auth ticket cleaned up successfully");
}
catch (Exception ex)
{
Log($"Error cleaning up auth ticket: {ex.Message}");
}
}
}