/// <summary> /// Adds new user message to channel ui /// </summary> private void AddMessage(string messageId, string userId, string username, string content, string date, bool historical) { //Declaring temp variables GameObject messagePrefab; bool couldBeEdited; //Checking if user who send message is local user if (userId == NakamaSessionManager.Instance.Account.User.Id) { messagePrefab = _thisUserMessagePrefab; couldBeEdited = true; } else { messagePrefab = _otherUserMessagePrefab; couldBeEdited = false; } //Instantiating message object as a child of _content GameObject messageGO = Instantiate(messagePrefab, _content) as GameObject; //Getting ChatMessageUI component from already instantiated message ChatMessageUI message = messageGO.GetComponent <ChatMessageUI>(); if (message) { //If the message is from the same user as newest we should hide his username on this message bool hideUsername = (username == _lastMessageUsername) && !historical; //Initializing message with given data message.InitMessage(messageId, username, content, date, couldBeEdited, hideUsername); //Register edit and remove methods to message eventss message.OnEditMessageClicked += OnEditMessageClicked; message.OnRemoveMessageClicked += OnRemoveMessageClicked; //Adding message to messages dict _messages.Add(messageId, message); //Setting last _lastMessageUsername = username; //If message is historical change order in hierarchy, the latest historical message is the oldest if (historical) { message.transform.SetSiblingIndex(1); } } else { Debug.LogError("Invalid _thisUserMessagePrefab or _otherUserMessagePrefab! It should contains ChatMessageUI script."); Destroy(messageGO); return; } }
/// <summary> /// Shows edit view and starts editing message /// </summary> /// <param name="messageId"></param> private void OnEditMessageClicked(string messageId) { //If other message was edited cancel this edit if (_editedMessage) { _editedMessage.Deselect(); } //Set new edited message _editedMessage = _messages[messageId]; //Select message in UI _editedMessage.SetSelectedState(); //Fill input field with actual message content _chatInputField.text = _messages[messageId].ContentTextValue; //Show edit and cancel buttons, hide send button _sendMessageButton.gameObject.SetActive(false); _editMessageButton.gameObject.SetActive(true); _cancelEditMessageButton.gameObject.SetActive(true); }