2302_80274807 2024-03-17 10:50 采纳率: 36.4%
浏览 63
已结题

unity接motionverse出现问题

unity控制台出现这样的的一段话应用不存在
UnityEngine.Debug:Assert (bool,string)
MotionverseSDK.TokenClient/d__7:MoveNext () (at Packages/cn.deepscience.motionverse/Runtime/TokenClient.cs:33)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<string>:SetResult (string) MotionverseSDK.TokenClient/<Execute>d__8:MoveNext () (at Packages/cn.deepscience.motionverse/Runtime/TokenClient.cs:79) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult (string)
MotionverseSDK.WebRequestDispatcher/d__4:MoveNext () (at Packages/cn.deepscience.motionverse/Runtime/WebRequest/WebRequestDispatcher.cs:135)
UnityEngine.UnitySynchronizationContext:ExecuteTasks ()
已经检查过secretkey和appid,应用程序状态也正常,区域设置也正确
以下是代码

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

namespace MotionverseSDK
{
    public class TokenClient : MonoBehaviour
    {
        public static string Host;
        public Region region;
        [SerializeField]
        private string AppID;
        [SerializeField]
        private string SecretKey;
        private const string TAG = nameof(TokenClient);
        CancellationToken cancellationToken = new CancellationToken();
        public static string token;
        async void Start()
        {
            if(region == Region.ZH_CN)
            {
                Host = "https://motionverseapi.deepscience.cn";
            }
            else
            {
                Host = "https://api.motionverse.ai";
            }
            token = await Execute(AppID, SecretKey);
            Debug.Log(token);
            Debug.Assert(token != null, "应用不存在");
        }
        public async Task<string> Execute(string AppID,string SecretKey)
        {
            try
            {
                Dictionary<string, string> getParameDic = new Dictionary<string, string>();
                long timestamp = Utilities.GetTimeStampSecond();
                getParameDic.Add("appid", AppID);
                getParameDic.Add("secret", SecretKey);
                getParameDic.Add("timestamp", timestamp.ToString());
                getParameDic.Add("sign", Utilities.SHA1(AppID + timestamp + SecretKey));

                StringBuilder stringBuilder = new StringBuilder();
                bool isFirst = true;
                foreach (var item in getParameDic)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                        stringBuilder.Append('?');
                    }
                    else
                        stringBuilder.Append('&');

                    stringBuilder.Append(item.Key);
                    stringBuilder.Append('=');
                    stringBuilder.Append(item.Value);
                }
                var webRequestDispatcher = new WebRequestDispatcher();
                string text = await webRequestDispatcher.Dispatch(Host+Config.GetTokenUrl+stringBuilder, cancellationToken);

                JObject obj = JObject.Parse(text);
                if (int.Parse(obj["code"].ToString()) == 0)
                {
                    return obj["data"]["access_token"].ToString();
                }
                return null;
               
            }
            catch (CustomException exception)
            {
                SDKLogger.Log(TAG, exception + "重试");
                await Task.Delay(1000);
                return await Execute(AppID, SecretKey);
            }
        }
    }
}


  • 写回答

4条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-03-17 11:23
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    这段代码中,出现了一个Debug.Assert语句,表示当token等于null时,会产生一个错误assertion。根据代码的逻辑,应该是获取token的过程中,出现了问题。可以尝试检查以下几个方面:
    1. 确认AppID和SecretKey是否正确,在代码中使用的是序列化字段,可能需要在Inspector中进行设置。
    2. 检查应用的状态,确认应用是否有权限进行该操作。
    3. 检查区域设置,确认Host的值是否正确。 以下是可供参考的修改建议:
    4. 在Start方法中,将Debug.Assert语句改为一个普通的if语句,将错误信息输出到控制台,例如: if(token == null) { Debug.LogError("Failed to get token, the application may not exist or the credentials are invalid."); }
    5. 在Execute方法中,可以加入Debug.Log语句,输出获取到的文本信息,例如: Debug.Log("Response: " + text); 这样可以更好地定位问题所在。
    6. 确认Utilities.GetTimeStampSecond()方法返回的时间戳是否正确,尝试在Execute中加入一个Debug.Log语句输出时间戳信息,例如: Debug.Log("Timestamp: " + timestamp);
    7. 确认Web请求是否正常发出,可以在Execute方法中加入一个Debug.Log语句,输出URL信息,例如: Debug.Log("URL: " + Host+Config.GetTokenUrl+stringBuilder);
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月14日
  • 创建了问题 3月17日