Это моя первая игра любого рода, так что будьте осторожны :) Я изучаю Unity + C # по ходу игры. Unity Editor 2019.2.5f1
Ситуация:
В конце игры, когда игрок умирает, игра проверяет, не является ли строка значения Coil чем-то отличным от «0». Если оно равно 0, оно переходит на сцену без подписчика. Если не 0, то «Game Over» Сцена и попытка сделать платеж на адрес xrptipbot. Устранение неполадок привело к тому, что я сделал статические записи для «до» и «суммы», чтобы сузить проблему. Это будет возвращено к динамическому анализу после tshooting httpWebRequest. Ошибка WebGL появляется после -> yield return new WaitForSeconds (3). Я также временно удалил раздел SendXRP (), и ошибка не возникает. Так что это специфично для httpWebRequest, который терпит неудачу, но это вне моего понимания, почему. Я проверил это в редакторе Unity, и он производит оплату.
Ожидаемый результат
Я ожидаю, что httpWebRequest совершит вызов на сервер xrptipbot и осуществит платеж в фоновом режиме.
Скрипт монитора смерти игрока
void Update()
{
if (Player == null)
{
StartCoroutine(GameOver());
}
}
IEnumerator GameOver()
{
coil = PlayerPrefs.GetString("lastcoilscore");
yield return new WaitForSeconds(2);
if (coil == "0")
{
SceneManager.LoadScene("NonCoilSubscriber");
}
else
{
SceneManager.LoadScene("Game Over");
}
}
Ошибка WebGL
An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was:
uncaught exception: abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/kripken/emscripten/wiki/Linking") at jsStackTrace (Moon Run WebGL nonDev.wasm.framework.unityweb:8:22313)
stackTrace (Moon Run WebGL nonDev.wasm.framework.unityweb:8:22484)
onAbort@https://gamedomain.com/moonrun/Build/UnityLoader.js:4:11047
abort (Moon Run WebGL nonDev.wasm.framework.unityweb:8:494060)
_dlopen (Moon Run WebGL nonDev.wasm.framework.unityweb:8:178189)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[48391]:0xff6df3 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[49657]:0x102e7f1)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28207]:0xbb9025 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28206]:0xbb8f9f)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[51225]:0x1082a7a
UnityModule [UnityModule/Module.dynCall_iii] (Moon Run WebGL nonDev.wasm.framework.unityweb:8:467180)
invoke_iii (Moon Run WebGL nonDev.wasm.framework.unityweb:8:341965)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28205]:0xbb8d52 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[51244]:0x1082d17)
UnityModule [UnityModule/Module.dynCall_vi] (Moon Run WebGL nonDev.wasm.framework.unityweb:8:478850)
invoke_vi (Moon Run WebGL nonDev.wasm.framework.unityweb:8:364096)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28203]:0xbb8bc8 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28202]:0xbb8b08)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[36732]:0xd7a911 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[51221]:0x1082a1e)
UnityModule [UnityModule/Module.dynCall_ii] (Moon Run WebGL nonDev.wasm.framework.unityweb:8:465791)
invoke_ii (Moon Run WebGL nonDev.wasm.framework.unityweb:8:339445)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[36734]:0xd7aa54 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[51221]:0x1082a1e)
UnityModule [UnityModule/Module.dynCall_ii] (Moon Run WebGL nonDev.wasm.framework.unityweb:8:465791)
invoke_ii (Moon Run WebGL nonDev.wasm.framework.unityweb:8:339445)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28234]:0xbb9fca (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[34834]:0xd169a2)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[34833]:0xd1685e (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[25972]:0xb31812)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28209]:0xbb922b (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[28213]:0xbb9982)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[50641]:0x1057b6e (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[26014]:0xb352ea)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[44595]:0xf060b1 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[24038]:0xaf3c76)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[51228]:0x1082ac5
UnityModule [UnityModule/Module.dynCall_iiiii] (Moon Run WebGL nonDev.wasm.framework.unityweb:8:468879)
invoke_iiiii (Moon Run WebGL nonDev.wasm.framework.unityweb:8:345111)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[48985]:0x1013783 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[48314]:0xff3d96)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[4194]:0x1a0270 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[4193]:0x1a019e)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[7685]:0x2c8e75 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[7683]:0x2c8b85)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[7687]:0x2c9079 (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[8073]:0x2f65ae)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[10434]:0x3f761a (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[10151]:0x3d5338)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[10151]:0x3d534d (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[10143]:0x3d4530)
@blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[10137]:0x3d2b6c (blob:https://gamedomain.com/dc1d1d91-dbf5-4a8d-b814-4efd62da78f5 line 8 > WebAssembly.instantiate:wasm-function[51242]:0x1082cf7)
UnityModule [UnityModule/Module.dynCall_v] (Moon Run WebGL nonDev.wasm.framework.unityweb:8:478193)
browserIterationFunc (Moon Run WebGL nonDev.wasm.framework.unityweb:8:182416)
runIter (Moon Run WebGL nonDev.wasm.framework.unityweb:8:185489)
Browser_mainLoop_runner (Moon Run WebGL nonDev.wasm.framework.unityweb:8:183951)
ilppayment.cs Полный код
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
[Serializable]
public class Ilppayment : MonoBehaviour
{
public InputField inputTextArea;
string coil;
public class Values
{
public string to;
public string amount;
public string token;
public bool existingDestination;
}
public void Start()
{
GrabPaymentInfo();
}
public void GrabPaymentInfo()
{
if (PlayerPrefs.GetString("ilpaddress") != null)
{
inputTextArea.text = PlayerPrefs.GetString("ilpaddress");
}
else
{
Debug.Log("Must enter a payment address");
return;
}
// Separation of ILP Address for Payment.
char[] spearator = { '.', ' ', '$', '/' };
Int32 count = 5;
// Using the Method
String[] strlist = inputTextArea.text.Split(spearator,
count, StringSplitOptions.None);
foreach (String s in strlist)
{
Console.WriteLine(s);
}
string service = strlist[1];
//address = strlist[4];
//string to = "xrptipbot://"+ service + "/" + address;
//EXAMPLE : string to = "$coil.xrptipbot.com/643d5f5f-3e5b-4f87-a9b5-8c735d5ea30e "; <-- SEE SPACE
//string amount = PlayerPrefs.GetString("lastxrpscore");
// END - Separation of ILP Address for Payment. - END
string to = "xrptipbot://coil/643d5f5f-3e5b-4f87-a9b5-8c735d5ea30e";
string amount = "0.000001";
Debug.Log("to : " + to);
Debug.Log("amount : " + amount);
Debug.Log("Calling Payment to XRPTipBot");
StartCoroutine(SendXRP(to: to, amount: amount));
}
IEnumerator SendXRP(string to, string amount)
{
yield return new WaitForSeconds(3);
Debug.Log(amount);
Debug.Log("Type Amount : " + amount.GetType());
var data = new Values();
data.to = to;
data.amount = amount;
data.token = "customtoken for payments";
data.existingDestination = true;
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.xrptipbot.com/app/api/action:tip/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = JsonUtility.ToJson(data
, true);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
Debug.Log(httpWebRequest);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Debug.Log(result);
}
}
}
Решение
Используйте собственный UnityWebRequest для отправки запроса POST. Это было трудно понять для меня. Кредит, предоставленный этому сообщению Отправка http-запросов в C # с Unity
public class Values
{
public string to;
public string amount;
public string token;
public bool existingDestination;
}
public void GrabPaymentInfo()
...
...
var data = new Values();
data.to = to;
data.amount = amount;
data.token = "custom token here";
data.existingDestination = true;
string json = JsonUtility.ToJson(data
, true);
//Debug.Log(json);
StartCoroutine(PostRequest(url: postURL, json: json));
IEnumerator PostRequest(string url, string json)
{
var uwr = new UnityWebRequest(url, "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
uwr.SetRequestHeader("Content-Type", "application/json");
//Send the request then wait here until it returns
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
}