最终由 Unity 解决(从大约 2018.2 版本开始)
在任何到达服务器的[Command] 内,
发送[Command]的客户端IP是
connectionToClient.address
呼。
您实际上将拥有自己的NetworkBehaviour 派生类,您必须这样做。 (通常称为“通讯”):
public partial class Comms : NetworkBehaviour {
该类必须覆盖服务器/客户端启动,以便每个 Comms “知道它是什么”。
public override void OnStartServer() {
.. this "Comms" does know it IS the server ..
}
public override void OnStartLocalPlayer() {
.. this "Comms" does know it IS one of the clients ..
}
在任何实际项目中,由于 Unity 不这样做,因此客户端必须向服务器表明自己的身份。您必须从头开始在 Unity 网络中完全处理这些问题。
(请注意,该过程在此处的长博客中进行了说明
https://forum.unity.com/threads/networkmanager-error-server-client-disconnect-error-1.439245/#post-3754939)
所以这是你(必须)在OnStartLocalPlayer做的第一件事
public override void OnStartLocalPlayer() {
Grid.commsLocalPlayer = this;
StandardCodeYouMustHave_IdentifySelfToServer();
// hundreds of other things to do..
// hundreds of other things to do..
}
通讯中的“命令对”如下所示:
您必须在所有网络项目中执行此操作(否则您将不知道哪个客户端是哪个客户端):
public void StandardCodeYouMustHave_IdentifySelfToServer() {
string identityString = "quarterback" "linebacker" "johnny smith"
.. or whatever this device is ..
CmdIdentifySelfToServer( identityString );
// hundreds more lines of your code
// hundreds more lines of your code
}
[Command]
void CmdIdentifySelfToServer(string connectingClientIdString) {
.. you now know this connection is a "connectingClientIdString"
.. and Unity gives you the connection: in the property: connectionToClient
.. you must record this in a hash, database, or whatever you like
MakeANote( connectingClientIdString , connectionToClient )
// hundreds more lines of your code
// hundreds more lines of your code
}
好消息是Unity添加了connectionToClient属性,也就是说在服务器上,在[Command]里面。
然后,您确实可以在其上使用新的.address 属性。
你终于可以得到那个客户的IP了!
public void StandardCodeYouMustHave_IdentifySelfToServer() {
CmdIdentifySelfToServer( identityString );
}
[Command]
void CmdIdentifySelfToServer(string connectingClientIdString) {
MakeANote( connectingClientIdString , connectionToClient )
string THEDAMNEDIP = connectionToClient.address;
}
就是那个“简单”,刚刚连接的客户端的ip就是“THEDAMNEDIP”。
呼。
只用了 Unity 5?年。