我有一個嵌入統一場景作為視圖的 iOS 專案。此問題發生在設定本地化該數字具有逗號,作為十進制分隔符而不是點的設備上.
我從本機代碼(swift)發送了一個引數字串到 unity。Unity 然后決議來自 iOS 的字串,創建一個新引數以嵌入另一個 http 請求。
這是 xcode 中的登錄:
param from iOS: 615d48538edef900126a784e***616957b6f59835001137ae7b***98.42519685039369***100.06561679790026
Request Object:
- distanceFromCeiling: 9,84251968503937E 15
- distanceFromSideWall: 1,000656167979E 16
- Obj: : UpdateRequest
- Obj.distanceFromCeiling: : 9,84251968503937E 15
- Obj.distanceFromSideWall: : 1,000656167979E 16
DATA request {"distanceFromCeiling":9842519685039368.0,"distanceFromSideWall":10006561679790026.0}
我希望我發送98.42519685039369, 100.06561679790026,但由于某種原因,它變成9842519685039368.0, 10006561679790026.0了資料請求 json 字串。
如何解決這個問題?我有另一個來自不同設備的日志,使用 localize en-US,似乎這里沒有出現問題。
Unity中的代碼(搜索is_the_problem_here直接跳轉到有問題的行:
using System;
using System.Collections;
using System.Collections.Generic;
using Lean.Touch;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.UI;
using BestHTTP;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using UnityEngine.UIElements;
public class GameManager : MonoBehaviour
{
// Other functions
// ====================================
void callApiFromIOS(string param)
{
Debug.Log("param from iOS: " param);
string[] str = param.Split(new string[] { "***" }, StringSplitOptions.None);
// is_the_problem_here
System.Nullable <double> distanceFromCeiling = null;
System.Nullable<double> distanceFromSideWall = null;
if (str != null && str.Length >= 4)
{
string ceiling = str[2];
if (!ceiling.Equals("null"))
{
// is_the_problem_here
distanceFromCeiling = Convert.ToDouble(ceiling);
}
string sideWall = str[3];
if (!sideWall.Equals("null"))
{
// is_the_problem_here
distanceFromSideWall = Convert.ToDouble(sideWall);
}
}
Debug.Log("Request Object: ");
Debug.Log("- distanceFromCeiling: " distanceFromCeiling);
Debug.Log("- distanceFromSideWall: " distanceFromSideWall);
UpdateRequest updateRequest = new UpdateRequest(distanceFromCeiling, distanceFromSideWall);
Debug.Log(" - Obj: : " updateRequest);
Debug.Log(" - Obj.distanceFromCeiling: : " updateRequest.distanceFromCeiling);
Debug.Log(" - Obj.distanceFromSideWall: : " updateRequest.distanceFromSideWall);
// is_the_problem_here
string data = JsonConvert.SerializeObject(updateRequest);
Debug.Log("DATA request " data);
// the rest of the code is set the json data as request body and send http.
}
另一個日志:
param from iOS: 615d48538edef900126a784e***616957b6f59835001137ae7b***98.42519685039369***100.06561679790026
Request Object:
- distanceFromCeiling: 98.4251968503937
- distanceFromSideWall: 100.0656167979
- Obj: : UpdateRequest
- Obj.distanceFromCeiling: : 98.4251968503937
- Obj.distanceFromSideWall: : 100.0656167979
DATA request {"distanceFromCeiling":98.425196850393689,"distanceFromSideWall":100.06561679790026}
** 更新 **
I've tried following this link but it's still not working as expect (same output as the first)
var converter = new FormattedDecimalConverter(CultureInfo.GetCultureInfo("en-US"));
string data = JsonConvert.SerializeObject(updateRequest, converter);
The converter class:
using System;
using System.Globalization;
using Newtonsoft.Json;
internal class FormattedDecimalConverter : JsonConverter
{
private CultureInfo culture;
public FormattedDecimalConverter(CultureInfo culture)
{
this.culture = culture;
}
public override bool CanConvert(Type objectType)
{
return true;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(Convert.ToString(value, culture));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Sorry for being clueless. What am I doing wrong here?
** UPDATE **
so changing Convert.toDouble() to double.Parse(ceiling, CultureInfo.InvariantCulture) fix the problem. What weird issue.
uj5u.com熱心網友回復:
默認情況下,Convert.ToDouble(string)使用運行代碼的設備的語言/區域設定。
在某些文化中,該.字符不被視為十進制分隔符,而是十進制組字符。
=> 例如1.000不是一而是一千!
如果string在多個設備上處理s,您應該始終確保使用培養的多載Convert.ToDouble(string, IFormatProvider)并傳入CultureInfo.InvariantCulture基于en-us.
string ceiling = str[2];
if (!ceiling.Equals("null"))
{
// is_the_problem_here
distanceFromCeiling = Convert.ToDouble(ceiling, CultureInfo.InvariantCulture);
}
string sideWall = str[3];
if (!sideWall.Equals("null"))
{
// is_the_problem_here
distanceFromSideWall = Convert.ToDouble(sideWall, CultureInfo.InvariantCulture);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/347449.html
標籤:c# unity3d encoding localization json.net
上一篇:檢查播放器不在場景中(不作業)
