//请求房间列表 public static void MsgGetRoomList(ClientState c, MsgBase msgBase) { MsgGetRoomList msg = (MsgGetRoomList)msgBase; Player player = c.player; if (player == null) { return; } player.Send(RoomManager.ToMsg()); }
static void Main(string[] args) { NetUtils.Start("127.0.0.1", 8888); Thread.Sleep(1000); NetUtils.Enter("test", "test"); while (true) { MsgGetRoomList msgGetRoomList = new MsgGetRoomList(); NetUtils.Send(msgGetRoomList); NetUtils.Update(); Thread.Sleep(20); } }
//显示 public override void OnShow(params object[] args) { //寻找组件 idText = skin.transform.Find("InfoPanel/IdText").GetComponent <Text>(); scoreText = skin.transform.Find("InfoPanel/ScoreText").GetComponent <Text>(); majorText = skin.transform.Find("InfoPanel/CampText").GetComponent <Text>(); coinText = skin.transform.Find("InfoPanel/GoldText").GetComponent <Text>(); createButton = skin.transform.Find("CtrlPanel/CreateButton").GetComponent <Button>(); reflashButton = skin.transform.Find("CtrlPanel/RefreshButton").GetComponent <Button>(); chooseButton = skin.transform.Find("CtrlPanel/ChooseBtn").GetComponent <Button>(); pressMeButton = skin.transform.Find("CtrlPanel/PressMeBtn").GetComponent <Button>(); content = skin.transform.Find("ListPanel/Scroll View/Viewport/Content"); roomObj = skin.transform.Find("Room").gameObject; //不激活房间 roomObj.SetActive(false); //显示id idText.text = GameMain.id; //按钮事件(chooseButton实际上是reflashButton) createButton.onClick.AddListener(OnCreateClick); reflashButton.onClick.AddListener(OnReflashClick); chooseButton.onClick.AddListener(OnChooseClick); pressMeButton.onClick.AddListener(OnPressMeClick); createButton.onClick.AddListener(Audio.ButtonClick); reflashButton.onClick.AddListener(Audio.ButtonClick); chooseButton.onClick.AddListener(Audio.ButtonClick); pressMeButton.onClick.AddListener(Audio.ButtonClick); //协议监听 NetManager.AddMsgListener("MsgGetAchieve", OnMsgGetAchieve); NetManager.AddMsgListener("MsgGetRoomList", OnMsgGetRoomList); NetManager.AddMsgListener("MsgCreateRoom", OnMsgCreateRoom); NetManager.AddMsgListener("MsgEnterRoom", OnMsgEnterRoom); NetManager.AddMsgListener("MsgVisitShop", OnMsgVisitShop); //发送查询 MsgGetAchieve msgGetAchieve = new MsgGetAchieve(); NetManager.Send(msgGetAchieve); MsgGetRoomList msgGetRoomList = new MsgGetRoomList(); NetManager.Send(msgGetRoomList); Audio.PlayLoop(Audio.bgRoomListPanel); }
//收到房间列表协议 public void OnMsgGetRoomList(MsgBase msgBase) { MsgGetRoomList msg = (MsgGetRoomList)msgBase; //清除房间列表 for (int i = content.childCount - 1; i >= 0; i--) { GameObject o = content.GetChild(i).gameObject; Destroy(o); } //重新生成列表 if (msg.rooms == null) { return; } for (int i = 0; i < msg.rooms.Length; i++) { GenerateRoom(msg.rooms[i]); } }
//生成MsgGetRoomList协议 public static MsgBase ToMsg() { MsgGetRoomList msg = new MsgGetRoomList(); int count = rooms.Count; msg.rooms = new RoomInfo[count]; //rooms int i = 0; foreach (Room room in rooms.Values) { RoomInfo roomInfo = new RoomInfo(); //赋值 roomInfo.id = room.id; roomInfo.count = room.playerIds.Count; roomInfo.status = (int)room.status; msg.rooms[i] = roomInfo; i++; } return(msg); }
//generate MsgGetRoomList protocol public static MsgBase ToMsg() { MsgGetRoomList msg = new MsgGetRoomList(); int count = roomDic.Count; msg.rooms = new RoomInfo[count]; //rooms int i = 0; foreach (Room room in roomDic.Values) { RoomInfo roomInfo = new RoomInfo { id = room.id, count = room.playerDic.Count, status = (int)room.status }; msg.rooms[i] = roomInfo; i++; } return(msg); }
//显示 public override void OnShow(params object[] args) { //寻找组件 idText = skin.transform.Find("InfoPanel/IdText").GetComponent <Text>(); scoreText = skin.transform.Find("InfoPanel/ScoreText").GetComponent <Text>(); createButton = skin.transform.Find("CtrlPanel/CreateButton").GetComponent <Button>(); reflashButton = skin.transform.Find("CtrlPanel/ReflashButton").GetComponent <Button>(); content = skin.transform.Find("ListPanel/Scroll View/Viewport/Content"); roomObj = skin.transform.Find("Room").gameObject; tankCamera = skin.transform.Find("InfoPanel/TankCamera").gameObject; //不激活房间 roomObj.SetActive(false); //显示id idText.text = GameMain.id; //按钮事件 createButton.onClick.AddListener(OnCreateClick); reflashButton.onClick.AddListener(OnReflashClick); //协议监听 NetManager.AddMsgListener("MsgGetAchieve", OnMsgGetAchieve); NetManager.AddMsgListener("MsgGetRoomList", OnMsgGetRoomList); NetManager.AddMsgListener("MsgCreateRoom", OnMsgCreateRoom); NetManager.AddMsgListener("MsgEnterRoom", OnMsgEnterRoom); //发送查询 MsgGetAchieve msgGetAchieve = new MsgGetAchieve(); NetManager.Send(msgGetAchieve); MsgGetRoomList msgGetRoomList = new MsgGetRoomList(); NetManager.Send(msgGetRoomList); //创建坦克模型 GameObject tankSkin = ResManager.LoadPrefab("tankPrefab"); tankObj = (GameObject)Instantiate(tankSkin, tankCamera.transform); tankObj.transform.localPosition = new Vector3(0, -2, 25); tankObj.transform.Rotate(0, 90, -30); }
//点击刷新按钮 public void OnReflashClick() { MsgGetRoomList msg = new MsgGetRoomList(); NetManager.Send(msg); }