Unity3D 中的简单 Steam 创意工坊实现

添加 Steamworks 软件包

  1. 转到 https://github.com/rlabrecque/Steamworks.NET/releases 并下载最新版本,确保下载 .unitypackage。然后,您需要将其导入您的 Unity 项目。

  2. 在项目文件夹的根目录中打开 steam_appid.txt 文件并将 480 替换为您的 Steam AppId。重新启动 Unity。

  3. 在您的初始场景中,创建一个新游戏对象,将其命名为“Steam Manager”,并添加在 Scripts>Steamworks.NET 中找到的 SteamManager.cs 脚本

这就是将 Steamworks 集成到您的项目中所需的全部内容。只要确保 Steam 客户端在后台运行,您应该能够在编辑器中点击播放并且没有错误。

 

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

internal struct SteamWorkshopItem
{
    public string ContentFolderPath;
    public string Description;
    public string PreviewImagePath;
    public string[] Tags;
    public string Title;
}

public class SteamWorkshop : MonoBehaviour
{
    private SteamWorkshopItem currentSteamWorkshopItem;
    private PublishedFileId_t publishedFileID;
    public static SteamWorkshop Instance { get; private set; }

    public List<string> GetListOfSubscribedItemsPaths()
    {
        var subscribedCount = SteamUGC.GetNumSubscribedItems();
        PublishedFileId_t[] subscribedFiles = new PublishedFileId_t[subscribedCount];
        SteamUGC.GetSubscribedItems(subscribedFiles, (uint)subscribedFiles.Length);

        ulong sizeOnDisk = 0;
        string installLocation = string.Empty;
        uint timeStamp = 0;

        var result = new List<string>();

        foreach (var file in subscribedFiles)
        {
            SteamUGC.GetItemInstallInfo(file, out sizeOnDisk, out installLocation, 1024, out timeStamp);
            result.Add(installLocation);
        }

        return result;
    }

    public void UploadContent(string itemTitle, string itemDescription, string contentFolderPath, string[] tags, string previewImagePath)
    {
        currentSteamWorkshopItem = new SteamWorkshopItem
        {
            Title = itemTitle,
            Description = itemDescription,
            ContentFolderPath = contentFolderPath,
            Tags = tags,
            PreviewImagePath = previewImagePath
        };

        CreateItem();
    }

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void CreateItem()
    {
        var steamAPICall = SteamUGC.CreateItem(SteamUtils.GetAppID(), EWorkshopFileType.k_EWorkshopFileTypeCommunity);
        var steamAPICallResult = CallResult<CreateItemResult_t>.Create();
        steamAPICallResult.Set(steamAPICall, CreateItemResult);
    }

    private void CreateItemResult(CreateItemResult_t param, bool bIOFailure)
    {
        if (param.m_eResult == EResult.k_EResultOK)
        {
            publishedFileID = param.m_nPublishedFileId;
            UpdateItem();
        }
        else
        {
            Debug.Log("Couldn't create a new item");
        }
    }

    private void UpdateItem()
    {
        var updateHandle = SteamUGC.StartItemUpdate(SteamUtils.GetAppID(), publishedFileID);

        SteamUGC.SetItemTitle(updateHandle, currentSteamWorkshopItem.Title);
        SteamUGC.SetItemDescription(updateHandle, currentSteamWorkshopItem.Description);
        SteamUGC.SetItemContent(updateHandle, currentSteamWorkshopItem.ContentFolderPath);
        SteamUGC.SetItemTags(updateHandle, currentSteamWorkshopItem.Tags);
        SteamUGC.SetItemPreview(updateHandle, currentSteamWorkshopItem.PreviewImagePath);
        SteamUGC.SetItemVisibility(updateHandle, ERemoteStoragePublishedFileVisibility.k_ERemoteStoragePublishedFileVisibilityPublic);

        var steamAPICall = SteamUGC.SubmitItemUpdate(updateHandle, "");
        var steamAPICallResult = CallResult<SubmitItemUpdateResult_t>.Create();
        steamAPICallResult.Set(steamAPICall, UpdateItemResult);
    }

    private void UpdateItemResult(SubmitItemUpdateResult_t param, bool bIOFailure)
    {
        if (param.m_eResult == EResult.k_EResultOK)
        {
            Debug.Log("Sucessfully submitted item to Steam");
        }
        else
        {
            Debug.Log("Couldn't submit the item to Steam");
        }
    }
}

 

转载自

https://www.salusgames.com/2019/01/27/simple-steam-workshop-implementation-in-unity3d/

posted @ 2022-02-15 23:19  三页菌  阅读(390)  评论(0编辑  收藏  举报