局域网射击游戏之Network局域网组件




 public int connections = 10;            //客户端控制
    public int listenPort = 8899;           //监听端口
    public bool UseNat = false;             // 地址转换
    public string ip = "127.0.0.1";

    public GameObject PlayerPrefab;
    public void OnGUI()
    {
        if (Network.peerType == NetworkPeerType.Disconnected)
        {
            if (GUILayout.Button("创建服务器"))
            {
                //进行创建服务器的操作
                NetworkConnectionError error = Network.InitializeServer(connections, listenPort, UseNat);
                print("aha");
            }
            if (GUILayout.Button("连接服务器"))
            {
                //在这里进行连接服务器的操作
                NetworkConnectionError error = Network.Connect(ip, listenPort);
            }
        }
        else
        if(Network.peerType == NetworkPeerType.Server)
        {
            GUILayout.Label("服务器创建完成");
        }
        else
        if(Network.peerType == NetworkPeerType.Client)
        {
            GUILayout.Label("客户端已经接入");
        }
    }

    //两个方法都是在服务器端调用
     void OnServerInitialized()
    {
        print("server完成初始化");
        //NetWork.Player可以访问到当前客户端
        int Group = int.Parse(Network.player + "");
        Network.Instantiate(PlayerPrefab, new Vector3(0,10,0),Quaternion.identity,Group);
    }

     void OnPlayerConnected(NetworkPlayer player)
    {
        print("一个客户端连接进来 Index Number:" + player);
    }

    //只在客户端被调用的函数
     void OnConnectedToServer()
    {
        print("我成功的连接了服务器!");
    }

今天就这么多了~~~  this is 大神(ruoji)



 
  

你可能感兴趣的:(局域网,网络,游戏,Unity)