steam注册事件
SteamCallBacks.OnLobbyKicked = Callback<LobbyKicked_t>.Create(OnLobbyKicked);
SteamCallBacks.OnLobbyCreated = Callback<LobbyCreated_t>.Create(OnLobbyCreated);
SteamCallBacks.OnLobbyEnter = Callback<LobbyEnter_t>.Create(OnLobbyEnter);
SteamCallBacks.OnLobbyDataUpdate = Callback<LobbyDataUpdate_t>.Create(OnLobbyDataUpdate);
SteamCallBacks.OnLobbyChatMsg = Callback<LobbyChatMsg_t>.Create(OnLobbyChatMsg);
SteamCallBacks.OnLobbyInvited = Callback<LobbyInvite_t>.Create(OnLobbyInvited);
SteamCallBacks.OnGameLobbyJoinRequested = Callback<GameLobbyJoinRequested_t>.Create(OnGameLobbyJoinRequested);
SteamCallBacks.OnGameRichPresenceJoinRequested = Callback<GameRichPresenceJoinRequested_t>.Create(OnGameRichPresenceJoinRequested);
标题
if (GUI.Button(new Rect(100,190,100,50),"Request"))
{
SteamMatchmaking.RequestLobbyData((CSteamID)NowLobbyId);
}
public struct LobbyDataUpdate_t {
public const int k_iCallback = Constants.k_iSteamMatchmakingCallbacks + 5;
public ulong m_ulSteamIDLobby;
public ulong m_ulSteamIDMember;
public byte m_bSuccess;
}
更多steam回调接口
steam官方文献库
steam sdk 消息工具类
using Steamworks;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class ST
{
public static void SENDMS_Private(CSteamID lobbyId,CSteamID sendWho,byte msId)
{
byte[] msg = new byte[] { (byte)sendWho, msId };
SteamMatchmaking.SendLobbyChatMsg(lobbyId, msg, 12);
}
public static void SENDMS_Private(ulong lobbyId, CSteamID sendWho, byte msId)
{
byte[] msg = new byte[] { (byte)sendWho, msId };
SteamMatchmaking.SendLobbyChatMsg((CSteamID)lobbyId, msg, 12);
}
public static void SENDMS_Public(CSteamID lobbyId,byte msId)
{
byte[] msg = new byte[] {0, msId };
SteamMatchmaking.SendLobbyChatMsg(lobbyId, msg, 12);
}
public static void SENDMS_Public(ulong lobbyId, byte msId)
{
byte[] msg = new byte[] { 0, msId };
SteamMatchmaking.SendLobbyChatMsg((CSteamID)lobbyId, msg, 12);
}
public static void SEND_LOOBY_MS(CSteamID lobbyId,string key,string value)
{
SteamMatchmaking.SetLobbyData(lobbyId, key, value);
SteamMatchmaking.RequestLobbyData(lobbyId);
}
public static void SEND_LOOBY_MS(ulong lobbyId, string key, string value)
{
SteamMatchmaking.SetLobbyData((CSteamID)lobbyId, key, value);
SteamMatchmaking.RequestLobbyData((CSteamID)lobbyId);
}
}
成就
using Steamworks;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FightClub
{
public class SteamAchievements : MonoBehaviour
{
public static SteamAchievements Instance { get; set; }
public enum Achievement : int
{
Achievement1,
};
private Achievment_t[] m_Achievements = new Achievment_t[]
{
new Achievment_t(Achievement.Achievement1, "第一个成就", "启动游戏立即获得"),
};
private class Achievment_t
{
public Achievement m_eAchievementID;
public string m_strName;
public string m_strDescription;
public bool m_bAchieved;
public Achievment_t(Achievement achivementID, string name, string desc)
{
m_eAchievementID = achivementID;
m_strName = name;
m_strDescription = desc;
m_bAchieved = false;
}
}
private CGameID m_GameID;
[Header("从Steam获取统计数据")]
public bool m_bRequestedStats;
private bool m_bStatsValid;
private bool m_bStoresStats;
private int m_nTotalNumWins;
protected Callback<UserStatsReceived_t> m_UserStatsReceived;
protected Callback<UserStatsStored_t> m_UserStatsStored;
protected Callback<UserAchievementStored_t> m_UserAchievementStored;
private void OnEnable()
{
if (!SteamManager.Initialized) return;
m_GameID = new CGameID(SteamUtils.GetAppID());
m_UserStatsReceived = Callback<UserStatsReceived_t>.Create(OnUserStatsReceived);
m_UserStatsStored = Callback<UserStatsStored_t>.Create(OnUserStatsStored);
m_UserAchievementStored = Callback<UserAchievementStored_t>.Create(OnAchievementStored);
m_bRequestedStats = false;
m_bStatsValid = false;
}
private void Awake()
{
Instance = this;
}
private void Start()
{
m_nTotalNumWins = 0;
}
public void UnLock(Achievement type)
{
if (UnityEngine.PlayerPrefs.GetInt(type.ToString()) !=2)
{
UnityEngine.PlayerPrefs.SetInt(type.ToString(),1);
}
}
private void Update()
{
if (!SteamManager.Initialized) return;
if (!m_bRequestedStats)
{
if (!SteamManager.Initialized)
{
m_bRequestedStats = true;
return;
}
bool bSuccess = SteamUserStats.RequestCurrentStats();
m_bRequestedStats = bSuccess;
}
foreach (Achievment_t achievement in m_Achievements)
{
if (achievement.m_bAchieved) continue;
switch (achievement.m_eAchievementID)
{
case Achievement.Achievement1:
if (UnityEngine.PlayerPrefs.GetInt(achievement.m_eAchievementID.ToString()) == 1)
{
Debug.Log("解锁::" + achievement.m_strName);
UnlockAchievement(achievement);
UnityEngine.PlayerPrefs.SetInt(achievement.m_eAchievementID.ToString(), 2);
}
break;
}
}
if (m_bStoresStats)
{
SteamUserStats.SetStat("NumWins", m_nTotalNumWins);
bool bSuccess = SteamUserStats.StoreStats();
m_bStoresStats = !bSuccess;
}
}
private void OnUserStatsReceived(UserStatsReceived_t pCallback)
{
if (!SteamManager.Initialized) return;
if ((ulong)m_GameID == pCallback.m_nGameID)
{
if (EResult.k_EResultOK == pCallback.m_eResult)
{
Debug.Log("Received stats and achievements from Steam");
m_bStatsValid = true;
foreach (Achievment_t ach in m_Achievements)
{
bool ret = SteamUserStats.GetAchievement(ach.m_eAchievementID.ToString(), out ach.m_bAchieved);
if (ret)
{
ach.m_strName =
SteamUserStats.GetAchievementDisplayAttribute(ach.m_eAchievementID.ToString(), "name");
ach.m_strDescription =
SteamUserStats.GetAchievementDisplayAttribute(ach.m_eAchievementID.ToString(), "desc");
}
else
{
Debug.LogWarning("failed for getting achievement" + ach.m_eAchievementID);
}
}
SteamUserStats.GetStat("NumWins", out m_nTotalNumWins);
}
else
Debug.Log("RequestStats - failed, " + pCallback.m_eResult);
}
}
private void OnUserStatsStored(UserStatsStored_t pCallback)
{
if ((ulong)m_GameID == pCallback.m_nGameID)
{
if (EResult.k_EResultOK == pCallback.m_eResult)
{
Debug.Log("StroeStats-success");
}
else if (EResult.k_EResultInvalidParam == pCallback.m_eResult)
{
Debug.Log("StoreStats-failed to validate");
UserStatsReceived_t callback = new UserStatsReceived_t();
callback.m_eResult = EResult.k_EResultOK;
callback.m_nGameID = (ulong)m_GameID;
OnUserStatsReceived(callback);
}
else
{
Debug.Log("failed-StroeStats");
}
}
}
private void UnlockAchievement(Achievment_t achievement)
{
achievement.m_bAchieved = true;
SteamUserStats.SetAchievement(achievement.m_eAchievementID.ToString());
m_bStoresStats = true;
}
private void OnAchievementStored(UserAchievementStored_t pCallback)
{
if ((ulong)m_GameID == pCallback.m_nGameID)
{
if (0 == pCallback.m_nMaxProgress)
{
Debug.Log("Achievement" + pCallback.m_rgchAchievementName + "unlocked");
}
else
{
Debug.Log("Achievement '" + pCallback.m_rgchAchievementName + "' progress callback, (" +
pCallback.m_nCurProgress + "," + pCallback.m_nMaxProgress + ")");
}
}
}
}
}
调用成就
SteamAchievements.Instance.UnLock(SteamAchievements.Achievement.Achievement1);
|