ブロガーは、Socket 通信を使用して Unity のログインおよび登録モジュールを実行しているときにこの問題に遭遇しました。バックエンドによって返される Json データは次のとおりです。
C# のコア コードは次のとおりです。
IEnumerator LoginRequest(string url)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Debug.Log("GetSuccess");
yield return webRequest.SendWebRequest();
if (!string.IsNullOrEmpty(webRequest.error))
{
Debug.LogError(webRequest.error);
}
else
{
reminderText.text = webRequest.downloadHandler.text;
string resultStr = Regex.Unescape(reminderText.text).Trim('"');
Debug.Log("C#原生字符串"+"loginSuccess");
Debug.Log("获取到的字符串" + reminderText.text);
Debug.Log("处理后的字符串"+resultStr);
if (string.Compare(resultStr, "loginSuccess") == 0)
{
Debug.Log("1");
hallSetUI.SetActive(true);
loginUI.SetActive(false);
SceneManager.LoadScene(0);
}
else if(string.Compare(resultStr, "passwordError") == 0)
{
}
}
}
}
Unity コンソールの対応する出力は次のとおりです。
問題は、取得した文字列に独自に定義した文字列よりも多くのダブルクォーテーションが含まれていることと、それに対応する処理メソッドもコード内にあることです。便宜上、次のようにキーコードを別途公開します。
string resultStr = Regex.Unescape(reminderText.text).Trim('"');
このうち、resultStr は結果を格納するために定義した文字列、reminderText.text は処理対象の文字列、Trim の一重引用符内の文字は処理対象の文字列内で削除される文字です。