快速入门:适用于 C# 的 PlayFab 客户端库

适用于 C# 的 PlayFab 客户端库入门。 按照步骤安装程序包,并尝试基本任务的示例代码。

本快速入门帮助你使用 C# 客户端库进行首次 PlayFab API 调用。

API 参考文档

要求

CSharp 项目设置

安装

  1. 打开 Visual Studio 并选择“创建新项目”。
  2. 选择适用于 C# 的“控制台应用 (.Net Core)
  3. 安装用于 PlayFabAllSDK 的 Nuget 程序包。

VS - 安装用于 PlayFab SDK 的 nuget 程序包。

此时,应能够成功编译项目。 输出窗口应包含类似于以下示例的内容。

1>------ Build started: Project: CSharpGettingStarted, Configuration: Debug Any CPU ------
1>  CSharpGettingStarted -> c:\dev\CSharpGettingStarted\CSharpGettingStarted\bin\Debug\CSharpGettingStarted.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

设置首次 API 调用

本指南提供了进行首次 PlayFab API 调用的最少步骤。 通过控制台打印完成确认。

新项目应包含名为 Program.cs 的文件(由 Visual Studio 自动创建)。 打开该文件,然后将内容替换为下面示例中的代码(粘贴该代码之后,可能需要刷新文件才能看到它)。 将使用写入控制台输出的消息对 API 调用进行确认。

using System;
using System.Threading;
using System.Threading.Tasks;
using PlayFab;
using PlayFab.ClientModels;

public static class Program
{
    private static bool _running = true;
    static void Main(string[] args)
    {
        PlayFabSettings.staticSettings.TitleId = "144"; // Please change this value to your own titleId from PlayFab Game Manager

        var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true };
        var loginTask = PlayFabClientAPI.LoginWithCustomIDAsync(request);
        // If you want a synchronous result, you can call loginTask.Wait() - Note, this will halt the program until the function returns

        while (_running)
        {
            if (loginTask.IsCompleted) // You would probably want a more sophisticated way of tracking pending async API calls in a real game
            {
                OnLoginComplete(loginTask);
            }

            // Presumably this would be your main game loop, doing other things
            Thread.Sleep(1);
        }

        Console.WriteLine("Done! Press any key to close");
        Console.ReadKey(); // This halts the program and waits for the user
    }

    private static void OnLoginComplete(Task<PlayFabResult<LoginResult>> taskResult)
    {
        var apiError = taskResult.Result.Error;
        var apiResult = taskResult.Result.Result;

        if (apiError != null)
        {
            Console.ForegroundColor = ConsoleColor.Red; // Make the error more visible
            Console.WriteLine("Something went wrong with your first API call.  :(");
            Console.WriteLine("Here's some debug information:");
            Console.WriteLine(PlayFabUtil.GenerateErrorReport(apiError));
            Console.ForegroundColor = ConsoleColor.Gray; // Reset to normal
        }
        else if (apiResult != null)
        {
            Console.WriteLine("Congratulations, you made your first successful API call!");
        }

        _running = false; // Because this is just an example, successful login triggers the end of the program
    }
}

完成并执行

执行此程序时,将在 consol 中显示以下输出

“恭喜,首次 API 调用成功! 完成! 请按任意键关闭。”

  • 此时,可以开始进行其他 API 调用并生成游戏。

  • 要生成管理实用工具,请查看位于 {CSharpSdk}/PlayFabClientSDK/sources 中的 PlayFab CSharpSdk zip 文件中的备用源文件:

有关所有可用客户端 API 调用列表或许多其他文章,请参阅 PlayFab API 参考