我正在開發一個多人游戲。我有一個朋友在 Swagger.io 上開發 API。所以這是我的問題:
我想使用我可以使用的 Task 函式來代替 Unity 自己的 UnityWebRequest 函式。我看了之后,做了一些研究,寫了下面的代碼。
private async Task<T> Send<T>(string service, object obj)
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest(service)
{
RequestFormat = DataFormat.Json,
Method = Method.POST,
};
#if UNITY_ANDROID
uniqueID = SystemInfo.deviceUniqueIdentifier;
platform = "1";
#endif
#if UNITY_IOS
uniqueID = UIDevice.identifierForVendor
platform = "0";
#endif
request.AddHeader("DeviceID", uniqueID);
request.AddHeader("BundleID", Application.identifier);
request.AddHeader("BuildID", "0");
request.AddHeader("Platform", platform);
request.AddHeader("Version", "0");
request.AddJsonBody(obj);
var response = await client.ExecuteAsync(request);
if (response.Content != null && response.StatusCode == HttpStatusCode.OK)
{
var jsonSerializerSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore };
var result = JsonConvert.DeserializeObject<T>(response.Content, jsonSerializerSetting);
return result;
}
else
{
return default;
}
因為它是一個異步函式,所以我用 await 呼叫它沒有任何問題。像這樣 :
我有一些課程可以回應其他課程:
[Serializable]
public class WelcomeRequest : Response
{
public string Location;
public string Language;
}
[Serializable]
public class WelcomeRequest : Response
{
public string Location;
public string Language;
}[Serializable]
public class WelcomeResponse : Response
{
public WelcomePayload Payload;
}
[Serializable]
public class WelcomePayload : Response
{
public Player Player;
public Settings Settings;
public Season Play;
public Season Purchase;
public Game GameOfSeason;
public Game[] Games;
public double PrizePool;
}
(I know it's a big project and there are a lot of subclasses. I don't want you to write code for me, I just want you to explain the logic of the code I wrote and an example that I can reference. Not to mention that I'm trying to do it alone. I don't have any friends who can help you but you.) I am able to receive files in json format from the server. But since this function only returns the object, I cannot assign sub-objects of the class. Do I need to convert it to json format again and deserialize it? Since I was working with the Firebase infrastructure before, I was able to communicate without any problems. I looked for Restsharp Unity tutorials but couldn't find any. Can you help me? At least if you guide me I will try to solve it myself. I'm sorry if I didn't express myself properly as this is my first post on this site and I don't know the rules very well. Endless thanks to all of you.
uj5u.com熱心網友回復:
這并不能真正回答您的問題,但您可能需要考慮使用生成的客戶端。
如果 API 是用 swagger 制作的,您應該能夠使用openapi-generator和 Swagger Schema制作 C# API 客戶端 。然后只需構建生成的專案并將 DLL 放到 unity projects Assets 檔案夾中。
您可能還需要包括RestSharp和Newtonsoft.Json。只需下載這些 NuGet 包并解壓它們(nupkg 檔案只是 zip),dll 應該在那里。通過將 DLL 放到 Assets 檔案夾中來包含它。
uj5u.com熱心網友回復:
public class Error: Response
{
public string Field; //The name of the area where the problem was detected
public string Error_; //Contains the code for the error message
public string HighLight; //It's used for the data to be highlighted in the error
field
}
這是我的反應課。我想通過在請求的回應回應物件中分配“Payload”變數來發送它。我不知道該怎么做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/383545.html
上一篇:如何在資料加載時顯示電子郵件警報并在加載資料時自動關閉-xamarin
下一篇:使處理程式通用
