我想将数据从Lua发送到C#

时间:2016-06-05 09:22:15

标签: c# lua udp

这是Lua代码。我想发送XXPos,YYPos,ZZPos。但在unity3d中,它只接收XXPos。

  socket = require("socket")
  print(socket._VERSION)

  function dataListener:post( t )

    local XPos = ship.rb:getPosition():x()
    local YPos = ship.rb:getPosition():y()
    local ZPos = ship.rb:getPosition():z()
    local XXPos = math.floor( XPos * 1000 + 0.5 ) / 1000
    local YYPos = math.floor( YPos * 1000 + 0.5 ) / 1000
    local ZZPos = math.floor( ZPos * 1000 + 0.5 ) / 1000

    udp=socket.udp();
    udp:setpeername("127.0.0.1",8051)
    udp:send(XXPos, " ", YYPos, " ", ZZPos);
  end

当我改变这样的Lua代码时,

--udp:send(XXPos, " ", YYPos, " ", ZZPos)
  udp:send(string.format("%d; %d; %d",XXPos,YYPos,ZZPos))

正确接收数据。但这个结果有1位数字,如3; 5; 2。

如何更改此Lua代码?

1 个答案:

答案 0 :(得分:1)

udp:send(string.format("%d; %d; %d",XXPos,YYPos,ZZPos))

应为udp:send(string.format("%f; %f; %f",XXPos,YYPos,ZZPos))

注意%f。这意味着浮动。