团队使用飞书进行项目管理,使用在线表格进行配置表的编写,而飞书也提供了在线表格操作的Api,这样我们可以直接在Unity中同步云端表格数据

飞书配置

首先需要进入飞书开发者后台创建应用https://open.feishu.cn/app

创建应用后记录AppId和AppSecret,在后面的api请求中会用到

在权限配置中选择需要获取的权限

然后需要发布自建应用

然后需要将自建应用添加到协作者中

Unity端配置

使用RestClient库

Unity中使用了WebRequest获取飞书提供的api

这里我使用了RestClient库 的EditorCoroutines分支 ,支持编辑器协程

https://github.com/proyecto26/RestClient/tree/feature/editor-coroutines

拉取并导入到unity中(因为没有Release版需要自行处理拉取的文件)

获取Token

首先获取token用于获取操作权限

请求Url为https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal

public string app_Id = "cli_xxxx";
public string app_secret = "qrwxxxxx";
public void GetTenantAccessToken(Action action=null)
{
    currentRequest = new RequestHelper
    {
        Uri = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal",
        Params = new Dictionary<string, string>
        {
            {nameof(app_Id), app_Id},
            {nameof(app_secret), app_secret}
        },
        EnableDebug = true,
        EditorCoroutine = true
    };
    RestClient.Post(currentRequest)
        .Then(res =>
        {
            RestClient.ClearDefaultParams();

            JObject myJObject = (JObject) JsonConvert.DeserializeObject(res.Text);

            if (myJObject != null)
            {
                tenant_access_token = myJObject["tenant_access_token"]?.ToString();
                action?.Invoke();
            }
        })
        .Catch(err => { Debug.Log(err.Message); });
}

获取表格信息

https://open.feishu.cn/document/ukTMukTMukTM/uATMzUjLwEzM14CMxMTN/overview

需要获取表格的spreadsheetToken,sheetId 以及范围

请求url为 https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/:spreadsheetToken/values/:range

例如我的云表格地址为https://fox-item.feishu.cn/sheets/shtcndxxxx?sheet=cdxxx

那么spreadsheetToken就是shtcndxxxx,sheetId 就是cdxxx

然后unity编写获取表格行列范围的代码

public void GetSheetTable(string spreadSheetToken, string sheetId, int row, int col, Action<string[,]> getSheet)
{
    var sheet = new string[row, col];
    currentRequest = new RequestHelper
    {
        Uri =
            $"https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{spreadSheetToken}/values/{sheetId}!A1:{(char) (col - 1 + 65)}{row}",
        Headers = new Dictionary<string, string>()
        {
            {"Authorization", $"Bearer {tenant_access_token}"},
        },
        EnableDebug = true,
        EditorCoroutine = true
    };
    RestClient.Get(currentRequest)
        .Then(res =>
        {
            RestClient.ClearDefaultParams();
            JObject myJObject = (JObject) JsonConvert.DeserializeObject(res.Text);

            if (myJObject != null)
            {
                var values = myJObject["data"]?["valueRange"]?["values"];

                if (values != null)
                {
                    //遍历每行
                    for (int i = 0; i < values.Count(); i++)
                    {
                        //遍历每列
                        for (int j = 0; j < values[i]!.Count(); j++)
                        {
                            sheet[i, j] = values[i][j]?.ToString();
                        }
                    }
                }
            }

            getSheet?.Invoke(sheet);
        })
        .Catch(err =>
        {
            Debug.Log(err.Message);
        });
}

该片段返回一个二维数组,对应的是表格从1行A列到某行列的数据,可以根据需要对该数据进行解析

Last modification:December 24th, 2022 at 04:08 pm