public void OnCreateRoomClick()
    {
        string roomName = inputField.GetComponent <InputField>().text;

        //增强鲁棒性   前端进行判断 真省心
        if (roomName.Equals(""))
        {                               //没有输入
            UnityEditor.EditorUtility.DisplayDialog("警告", "房间名不能为空", "确认", "取消");
            return;
        }
        else if (RoomObject.DetectRoomName(roomName, roomList))
        {   //有重复的名字
            UnityEditor.EditorUtility.DisplayDialog("警告", "该房间名已存在", "确认", "取消");
            return;
        }
        //上方判断通过了后 就可以加入 roomList 啦
        RoomObject thisRoom = new RoomObject(roomName);

        thisRoom.PlayerList.Add(new Player(Root.this_player.Name)); //加入房主

        currentRoom = thisRoom;                                     //这个房间就是创建者所在的房间啦
        //roomList.Add(thisRoom);//在所有的房间列表中 加入这个房间//在广播回调中加入
        //通过协议将 新建房间  信息传给服务器
        ProtocolBytes protocol = new ProtocolBytes();

        protocol.AddString("CreateRoom");
        protocol.AddString(roomName);
        Debug.Log("发送 " + protocol.GetDesc());
        NetMgr.srvConn.Send(protocol);//广播 不需要注册回调函数,但是服务端发过来的数据会分发到那个回调函数上
        //在广播的回调中进行 列表的重新渲染
    }
예제 #2
0
    //挂到 Button 上的方法
    public void CreateRoom()
    {
        string roomName = inputField.GetComponent <InputField>().text;

        //增强鲁棒性
        if (roomName.Equals(""))
        {                               //没有输入
            UnityEditor.EditorUtility.DisplayDialog("警告", "房间名不能为空", "确认", "取消");
            return;
        }
        else if (RoomObject.DetectRoomName(roomName, roomList))
        {   //有重复的名字
            UnityEditor.EditorUtility.DisplayDialog("警告", "该房间名已存在", "确认", "取消");
            return;
        }
        //上方判断通过了后 就可以加入 roomList 啦
        RoomObject thisRoom = new RoomObject(roomName);

        thisRoom.PlayerList.Add(new Player("hahaha"));
        currentRoom = thisRoom;         //这个房间就是创建者所在的房间啦
        roomList.Add(thisRoom);         //在所有的房间列表中 加入这个房间
        // TODO: 通过协议将 新建房间  信息传给服务器


        // 解释一下 (items.Count+1)/2
        // 因为我的一行有两个text 所以 这个是求行数最简单的办法
        GameObject a1 = Instantiate(itemGObject1) as GameObject;
        GameObject a2 = Instantiate(itemGObject2) as GameObject;

        a1.transform.SetParent(content1GObject.transform);
        a2.transform.SetParent(content1GObject.transform);
        a1.transform.localPosition    = new Vector3(itemLocalPos1.x, itemLocalPos1.y - (items.Count + 1) / 2 * itemHeight1, 0);
        a2.transform.localPosition    = new Vector3(itemLocalPos1.x, itemLocalPos1.y - (items.Count + 1) / 2 * itemHeight1, 0);
        a1.GetComponent <Text>().text = roomName;
        a2.GetComponent <Text>().text = thisRoom.Fraction;
        items.Add(a1);
        items.Add(a2);
        if (content1Size.y <= ((items.Count + 1) / 2) * itemHeight1)
        {
            content1GObject.GetComponent <RectTransform>().sizeDelta = new Vector2(content1Size.x, ((items.Count + 1) / 2) * itemHeight1);
        }

        //来给 新加入的两个 Text 添加监听吧 同时将它们自己传入
        a1.GetComponent <Button>().onClick.AddListener(delegate()
        {
            OnTextA1CLick(this.gameObject);
        });
        a2.GetComponent <Button>().onClick.AddListener(delegate()
        {
            OnTextA2CLick(this.gameObject);
        });

        //因为这个方法里 有对于新房间的创建 所以最后需要自动 在房间内列表 content 里面显示所有的玩家
        OnTextA1CLick(a1);
    }