IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> [二]Unity接入Steam技术[大厅邀请好友Steam消息事件成就] -> 正文阅读

[游戏开发][二]Unity接入Steam技术[大厅邀请好友Steam消息事件成就]

steam注册事件

      //当退出大厅
      SteamCallBacks.OnLobbyKicked = Callback<LobbyKicked_t>.Create(OnLobbyKicked);
      //当大厅创建
      SteamCallBacks.OnLobbyCreated = Callback<LobbyCreated_t>.Create(OnLobbyCreated);
      //当加入steam大厅
      SteamCallBacks.OnLobbyEnter = Callback<LobbyEnter_t>.Create(OnLobbyEnter);
      //当大厅数据更新
      SteamCallBacks.OnLobbyDataUpdate = Callback<LobbyDataUpdate_t>.Create(OnLobbyDataUpdate);
    /// SendLobbyChatMsg。这将向大厅内的所有用户发送一条简单的二进制消息。 
    /// 大厅成员需要听取 ISteamMatchmaking::LobbyChatMsg_t 回调。 
    /// 收到回调后,您可以使用 ISteamMatchmaking::GetLobbyChatEntry 获取消息内容
      SteamCallBacks.OnLobbyChatMsg = Callback<LobbyChatMsg_t>.Create(OnLobbyChatMsg);
      ///当收到邀请通知
      SteamCallBacks.OnLobbyInvited = Callback<LobbyInvite_t>.Create(OnLobbyInvited);
    /// 如果用户已在游戏中,将提交 ISteamFriends::GameLobbyJoinRequested_t 回调,
    /// 其中包含了用户希望加入的大厅的 Steam ID。 这将由您的游戏决定是否接受。
      SteamCallBacks.OnGameLobbyJoinRequested = Callback<GameLobbyJoinRequested_t>.Create(OnGameLobbyJoinRequested);
     /// 如果目标用户接受了邀请,启动游戏时 pchConnectString 会添加入命令行。
    /// 如果该用户已在运行游戏,对方便会收到带有连接字符串的
      SteamCallBacks.OnGameRichPresenceJoinRequested = Callback<GameRichPresenceJoinRequested_t>.Create(OnGameRichPresenceJoinRequested);

标题

 if (GUI.Button(new Rect(100,190,100,50),"Request")) 
 {
//刷新你现在不一定在的大厅的元数据
//这将发送与说客相关的所有元数据
//如果本地用户没有连接到Steam服务器,返回false
//结果将由dataupdate_t callback 触发
  SteamMatchmaking.RequestLobbyData((CSteamID)NowLobbyId);
 }
 //steam  API   LobbyDataUpdate_t 回调将会被触发
public struct LobbyDataUpdate_t {
		public const int k_iCallback = Constants.k_iSteamMatchmakingCallbacks + 5;
		
		public ulong m_ulSteamIDLobby;		// steamID of the Lobby
		public ulong m_ulSteamIDMember;		// steamID of the member whose data changed, or the room itself
		public byte m_bSuccess;				// true if we lobby data was successfully changed;
										// will only be false if RequestLobbyData() was called on a lobby that no longer exists
	}

更多steam回调接口

steam官方文献库
在这里插入图片描述

steam sdk 消息工具类

using Steamworks;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class ST
{
//给steam发送一些信息,以实现好友之间的状态事件监听。[可自定义]
  public static void SENDMS_Private(CSteamID lobbyId,CSteamID sendWho,byte msId)
  {
    byte[] msg = new byte[] { (byte)sendWho, msId };//{消息类型[0==公共],类容}
    SteamMatchmaking.SendLobbyChatMsg(lobbyId, msg, 12);
  }
  public static void SENDMS_Private(ulong lobbyId, CSteamID sendWho, byte msId)
  {
    byte[] msg = new byte[] { (byte)sendWho, msId };//{消息类型[0==公共],类容}
    SteamMatchmaking.SendLobbyChatMsg((CSteamID)lobbyId, msg, 12);
  }
  public static void SENDMS_Public(CSteamID lobbyId,byte msId)
  {
    byte[] msg = new byte[] {0, msId };//{消息类型[0==公共],类容}
    SteamMatchmaking.SendLobbyChatMsg(lobbyId, msg, 12);
  }
  public static void SENDMS_Public(ulong lobbyId, byte msId)
  {
    byte[] msg = new byte[] { 0, msId };//{消息类型[0==公共],类容}
    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;
      }
    }

    //GameID
    private CGameID m_GameID;

    //从Steam获取统计数据?
    [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;

    //把回调创建放在OnEnable方法内以确保Unity加载完成后重复创建回调
    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;
        }
        //从 Steam 后端调出用户的统计与成就数据
        bool bSuccess = SteamUserStats.RequestCurrentStats();
        m_bRequestedStats = bSuccess;
      }
      //if (!m_bStatsValid) return;

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

    }

    // 数据准备完成后(RequestCurrentStats),回调
    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)
            {
              //在游戏里显示成就的可读属性(name&des)
              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);
      }
    }


    //回调上传的变更(StoreStats)
    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);
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-01 12:17:16  更:2021-09-01 12:18:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 -2025/1/11 11:14:23-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码