string - 关于 UWP 中字符串的 UDP 套接字
问题描述
我在 UWP 中编写代码时发现了一个奇怪的问题。
我使用 UDP 套接字向 hololens 发送坐标。
坐标如“1.0_1.0_1.0”,以字符串形式发送,按照“_”进行切割,然后用坐标控制球体的运动。
首先,它在统一编辑器中运行良好。
但是在hololens中,例如,我只收到“1.0_1.0_1.0”,但不能将其更改为vector3:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity;
#if !UNITY_EDITOR
using System;
using Windows.Foundation;
#endif
public class pre : MonoBehaviour
{
public TextMesh tm = null;
public TextMesh tmmm = null;
public GameObject sphere;
string test;
public void ResponseToUDPPacket(string incomingIP, string incomingPort, string data)
{
string[] centre = data.Split('_');
float[] num= new float[3];
if (tm != null)
tm.text = data;
num[0] = float.Parse(centre[0]);
num[1] = float.Parse(centre[1]);
num[2] = float.Parse(centre[2]);
Debug.Log(num[1]);
if (tmmm != null)
tmmm.text = num[1].ToString();
sphere.transform.position = new Vector3(num[0], num[1], num[2]);
//var headPosition = Camera.main.transform.position;
//headPosition.z = headPosition.z+10;
//sphere1.transform.position = headPosition;
}
}
我用两个testmesh显示结果,“data”是我收到的字符串,第一个“tm”没有问题。例如,它会显示“1.0_1.0_1.0”。
但是根据 num[1] 得到的“tmm”是行不通的。
我一直以为是UWP的问题。
但!!!!!!!!!!!!!!!!!!!!!!!我通过 c++ 程序发送 UDP 字符串,但是当我使用软件(UDPsender 或类似软件)手动发送字符串时,hololens 中的问题消失了!
但我认为我的 C++ 程序没有问题,因为至少它在统一编辑器中运行良好。
有没有人知道这个问题?
解决方案
也许你的模拟器和你的设备有不同的文化,一个理解 1.0 而另一个在 float.Parse 中期望 1,0。
如果是这种情况,您可以将您的文化(或 InvariantCulture)传递给 float.Parse。
Debug.Log(num[1]) 是否在所有情况下都打印正确的数字?如果不是,这可以证实上述理论。
推荐阅读
- r - 如何在 Shiny 模块生成的 DiagrammeR 输出中找到单击的元素
- sqlite - SQLite - 如何使用一个命令行读取脚本文件
- postgresql - 本地 Postgres 数据库到 Google Cloud PostgreSQL Github
- python - 为 ML 分类处理列中的无限/大值
- php - 使用 json_encode 数据设置输入字段默认值
- python - 如何根据多个变量定位数据帧的子组并仅替换原始数据帧中该子组的值?
- c - 使用 void 指针连接字符串
- triggers - 是否可以从 Power App 触发 ADF 管道?
- node.js - Httprequest 未收到来自快速路由器的响应
- ruby-on-rails - 使用 exq 和 Sidekiq 在 rails 和 phoenix 之间进行通信