Example #1
0
        // Start is called before the first frame update
        void Start()
        {
            _gameClient      = ClientLab.GetGameClient();
            _gameClient.User = new ClientUser()
            {
                UserName = GetLocalIpAddress().ToString(), UserEndPoint = _localEndPoint
            };
            _gameClient.RemoteEndPoint           = _remoteEndPoint;
            _gameClient.OnGameUserListReceive    = (userList) => { };
            _gameClient.OnGameUserMessageReceive = (userName, otherName, message) =>
            {
                // 如果信息是自己发的,此时userName为自己,otherName 为朋友名字
                if (userName == _gameClient.User.UserName)
                {
                    Friend friend = friends.Find(f => f.UserName == otherName);
                    // 如果列表中没有此人记录
                    if (friend == null)
                    {
                        friend = new Friend {
                            UserName = otherName
                        };
                        friend.MessageList.Add(userName, message);
                        friends.Add(friend);
                    }
                    else
                    {
                        friend.MessageList.Add(userName, message);
                    }
                }
                // userName 为此消息的发送人,otherName为此消息的接收人
                // 当接受到的消息不是本人发送的时,记录发送者
                // 此时userName为朋友名字,otherName为自己
                if (userName != _gameClient.User.UserName)
                {
                    Friend friend = friends.Find(f => f.UserName == userName);
                    // 如果列表中没有此人记录
                    if (friend == null)
                    {
                        friend = new Friend {
                            UserName = userName
                        };
                        friend.MessageList.Add(userName, message);
                        friends.Add(friend);
                    }
                    else
                    {
                        friend.MessageList.Add(userName, message);
                    }
                }

                hasReceiveMessage = true;
            };
            _gameClient.OnGameUserListStrReceive = (userListStr) => { };
        }
        // Use this for initialization
        void Start()
        {
            _gameClient = ClientLab.GetGameClient();

            chatPanel.SetActive(false);

            friendBtn.onClick.AddListener(() =>
            {
                // 此处显示好友列表, 并将当前接收的消息传入android界面
                CallAndroidMethod.StartFriendListDialog();
                // chatPanel.SetActive(true);
            });
            squareBtn.onClick.AddListener(() =>
            {
                print(ArUtils.GetObjectSizeByCollider(role));
                Vector3 chatBubblePosition =
                    Camera.main
                    .WorldToScreenPoint(
                        role.transform.position +
                        new Vector3(
                            0,
                            ArUtils.GetObjectSizeByCollider(role).y / 2,
                            0)) +
                    new Vector3(0, 100, 0);
                var chatBubble = Instantiate(chatBubblePrefab, chatBubblePosition, Quaternion.Euler(new Vector3())) as GameObject;
                chatBubble.transform.SetParent(GameObject.Find("Canvas").transform);
                BubbleController bubbleController = chatBubble.GetComponent <BubbleController>();
                bubbleController.SetMessage("hello\nhello\bhello");
                bubbleController.SetTimeDelay(1);
            });
            quietImage.GetComponent <PointerClickEventTrigger>()
            .onPointerClick
            .AddListener(() =>
            {
                chatPanel.SetActive(false);
            });

            submitButton.onClick.AddListener(() =>
            {
                if (chatInput.text != "")
                {
                    _gameClient.ChatToUser(otherName, chatInput.text);
                }
            });
        }