using Assets.MyGame.Scripts.Net; using Assets.MyGame.Scripts.Net.APIRequest.Objs; using Assets.MyGame.Scripts.Net.WebResponses; using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class UIHome_BY_ServicePanel : SubUIBase { public Toggle toggle_DefaultQuestion; public Toggle toggle_YouChangeIChange; public Toggle toggle_OnlineService; [Header("客服")] public TMP_InputField tMPInput_Text; public GameObject dialogGreenPrefab; public GameObject dialogOrangePrefab; public Transform dialogPrefabParent; public Button btn_SendText; public string conversationId; public void OnMessageReceived(CustomerMessageReceived customerMessageReceived) { if (customerMessageReceived.senderType==1) { if (dic_dialogItems.ContainsKey(customerMessageReceived.guid)) { dic_dialogItems[customerMessageReceived.guid].waitMessage.SetActive(false); //失败没有写 } } else { Instantiate(dialogOrangePrefab, dialogPrefabParent).GetComponent().SetMessage(customerMessageReceived.content); ScrollToBottom(); } } public void OnConversationStarted(CustomerConversationStartedResult customerConversationStartedResult) { conversationId = customerConversationStartedResult.conversation.id; } public void OnDestroy() { WebSocketSendList.Customer_LeaveConversation(conversationId); WebSocketManager.Instance.Close(SocketType.customer); } public override void Awake() { base.Awake(); btn_Close.onClick.RemoveAllListeners(); btn_Close.onClick.AddListener(() => { uIManager.Close(UIPanel.ServicePanel); }); WebSocketManager.Instance.Connect(SocketType.customer, () => { // 发起会话(服务端会回调 OnConversationStarted,自动 JoinConversation) WebSocketSendList.Customer_StartConversation( GameManager.Instance.user.id, GameManager.Instance.user.BaseInfo.userName ); // 连接后加入接单队列,服务端有新会话会回调 OnNewConversation(自动 JoinConversation) WebSocketSendList.Customer_JoinCustomerQueue(conversationId); }); toggle_DefaultQuestion.onValueChanged.AddListener((a) => { if (a) { //APIRequestList.GetOnlineCustomers } }); btn_SendText.onClick.AddListener(() => { var var = Instantiate(dialogGreenPrefab, dialogPrefabParent).GetComponent(); var.SetMessage(tMPInput_Text.text); ScrollToBottom(); string guid = Guid.NewGuid().ToString(); WebSocketSendList.Customer_SendMessage(conversationId, 1, GameManager.Instance.user.id, GameManager.Instance.user.BaseInfo.userName, null, 1, tMPInput_Text.text, guid); dic_dialogItems.Add(guid, var); }); } Dictionary dic_dialogItems = new Dictionary(); public void ScrollToBottom() { StartCoroutine(ScrollBottomCoroutine()); } IEnumerator ScrollBottomCoroutine() { // 等待所有布局刷新 yield return null; yield return null; // 两帧最稳 yield return null; // 两帧最稳 dialogPrefabParent.parent.parent.GetComponent().verticalNormalizedPosition = 0; // 再强制刷新一次,防止被覆盖 Canvas.ForceUpdateCanvases(); dialogPrefabParent.parent.parent.GetComponent().verticalNormalizedPosition = 0; } // Update is called once per frame void Update() { } }