如何将 PlayFab 的 UserDataRecord 转换为字符串数组

How Can I convert PlayFab's UserDataRecord to String array

我试图从特定播放器获取一些变量,然后将它们存储在数组中以操作和显示它们,我将键放在数组上没有任何问题,但在将returns 用于获取用户数据的字典的值不允许我将它们存储在数组中

显示Cannot convert from 'string[]' to ' PlayFabClientModels.UserDataRecord[]'

private String[] qDataKeys;
private String[] qDataValues;

void GetQuestionsData()
{
    PlayFabClientAPI.GetUserData(new GetUserDataRequest()
    {
        PlayFabId = "someID",
        Keys = null
    }, 
    OnQuestionsDataReceived, 
    OnError
    );
    
}

void OnQuestionsDataReceived(GetUserDataResult result)
{

    result.Data.Keys.CopyTo(qDataKeys, 0);
    result.Data.Values.CopyTo(qDataValues, 0);// This is the one that gives the error, above one is okay
}

问题在错误中很清楚。此调用返回的类型是 PlayFabClientModels.UserDataRecord[] 类型。查看 docs,UserDataRecord 类型设置为

Ordered by:
Data name
Data type
Data Description

LastUpdated 
string
Timestamp for when this data was last updated.

Permission  
UserDataPermission
Indicates whether this data can be read by all users (public) or only the user (private). This is used for GetUserData requests being made by one player about another player.

Value   
string
Data stored for the specified user data key.

我假设您想要记录中的 Value。我不确定您当前是如何访问 Key 的,因为文档指定 GetUserDataResult returns Data 类型为 UserDataRecordDataVersion 类型为 number.

错误当前告诉您您正在使用的 copyTo 失败,因为当前类型是 PlayFabClientModels.UserDataRecord[] 而您期望 string[]。您需要遍历每个 UserDataRecord 并从每个索引中获取 Value 数据。

我相信

string[] yourData = result.Data.Value.Select(x => x.Value).ToArray();

应该可以。您需要在文件顶部包含 using System.Linq。如果它不起作用,我只需要知道 result.Data.Values 的输入,但由于错误,我假设它是一个无数的数据数组。我不熟悉 PlayFab,因此如果当前文档已过时,请在评论中更正我,我会尝试更新答案。