-1

您好,我想解析从 websocket 服务器获得的数据,但它显示“无法从 'string' 转换为 'int'


当我这样做时,它给了我一个错误的数字示例:

  • 实际价值:-11.6666342423411
  • 它说的值:-1.166663424234E + 16
    void Update()
    {
        ws.OnMessage += (sender, e) =>
        {
            JSONNode data = JSON.Parse(e.Data);
            Debug.Log(data);
            // position.x = int.Parse(e.Data["position"]["x"]);
            // position.y = int.Parse(e.Data["position"]["y"]);
            // Debug.Log(position);
            // gameObject.transform.position = position;

            // float rotation = data["rotation"];
            // rb.rotation = rotation;
        };
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WebSocketSharp;
using SimpleJSON;

public class Enemy : MonoBehaviour
{
    public Rigidbody2D rb;

    Vector2 position;

    WebSocket ws;
    void Start()
    {
        ws = new WebSocket("ws://localhost:8080");
        ws.Connect();
    }

    void Update()
    {
        ws.OnMessage += (sender, e) =>
        {
            // JSONNode data = JSON.Parse(e.Data);
            Debug.Log(e.Data["position"]);
            // position.x = int.Parse(e.Data["position"]["x"]);
            // position.y = int.Parse(e.Data["position"]["y"]);
            // Debug.Log(position);
            // gameObject.transform.position = position;

            // float rotation = data["rotation"];
            // rb.rotation = rotation;
        };
    }
}

4

1 回答 1

0

你得到解析的是

-116666342423411

因为int.Parse默认情况下不使用小数点...因为int. 然后它返回一个int并且 Unity 只是以科学符号显示它。


您宁愿使用的是float.Parse因为您的值应该是浮点值。

或者实际上现在我看到你正在使用SimpleJson,你也可以简单地使用JsonNode.AsFloat,因为他们已经有了一个实现。

或者,如果您还要使用,SimpleJSONUnity.cs (Extension file)那么您实际上可以使用JsonNode.ReadVector2!


然后您还想访问这些值,e.Data而不是在您解析的 json 节点中

position.x = float.Parse(data["position"]["x"].Value);
position.y = float.Parse(data["position"]["y"].Value);

或如所说

position.x = data["position"]["x"].AsFloat();
position.y = data["position"]["y"].AsFloat();

或(使用扩展)

position = data["position"].ReadVector2();

然后总的来说,让我立即告诉您,它不会按照您的方式工作!

您想将侦听器附加一次

而且大多数 Unity API 只能在 Unity 主线程中使用,并且OnMessage很可能被称为异步。

你的代码应该是例如

public class Enemy : MonoBehaviour
{
    public Rigidbody2D rb;

    private readonly ConcurrentQueue<Action> _actions = new ConcurrentQueue<Action>();

    WebSocket ws;
    void Start()
    {
        ws = new WebSocket("ws://localhost:8080");

        ws.OnMessage += (sender, e) =>
        {
            // dispatch this into the main thread so it is executed in the next Update call
            _actions.Enqueue(() =>
            {
                JSONNode data = JSON.Parse(e.Data);
            
                var x = float.Parse(data["position"]["x"].Value);     
                //var x = data["position"]["x"].AsFloat();
                var y = float.Parse(data["position"]["y"].Value);     
                //var y = data["position"]["y"].AsFloat();

                var position = new Vector2(x, y);
                //var position = data["position"].ReadVector2();

                Debug.Log($"Received position {position.ToString("G8")}");
                // NOTE: you should not set anything via Transform
                // otherwise you break the physics and collision detection
                rb.MovePosition(position);

                var rotation = float.Parse(data["rotation"].Value); 
                //var rotation = data["rotation"].AsFloat();
                rb.MoveRotation(rotation);
            });
        };

        ws.Connect();
    }

    void FixedUpdate()
    {
        while(_actions.Count > 0 && _actions.TryDequeue(out var action))
        {
            action?.Invoke();
        }
    }
}
于 2021-09-01T13:51:28.797 回答