using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using static IFlytekManager; using static WindowManager; using TMPro; using WebSocketSharp; public class SparkModelPanel : PanelBasic { private TMP_InputField question_input_field; private Button send_question_button; private RectTransform chat_item_content; private ChatItem chat_item_prefab; /// /// 对象池 /// private List chat_item_pool = new List(); /// /// 使用中的对象 /// private List using_chat_item_list = new List(); public Sprite answer_avatar_sprite; public Sprite question_avatar_sprite; /// /// 是否联系上下文 /// public bool _contact_context; protected override void Awake() { base.Awake(); } // Start is called before the first frame update void Start() { send_question_button.onClick.AddListener(SendChat); allow_ask = true; } bool allow_enter; bool allow_ask; void FixedUpdate() { if (allow_enter && allow_ask && (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter))) { if (!string.IsNullOrEmpty(question_input_field.text.Replace("\n", ""))) { SendChat(); allow_enter = false; } } else { allow_enter = question_input_field.isFocused; } } List chat_history = new List(); void SendChat() { var __question = question_input_field.text; __question = __question.Trim('\n'); if (!string.IsNullOrEmpty(__question)) { //添加问题Item展示 using_chat_item_list.Add(Get(__question, ChatItem.ChatType.QUESTION)); //刷新 LayoutRebuilder.ForceRebuildLayoutImmediate(chat_item_content); question_input_field.text = ""; if (_contact_context) { } else { allow_ask = false; instance.Chat(__question, (_answer) => { allow_ask = true; _answer = _answer.TrimStart('\n'); using_chat_item_list.Add(Get(_answer, ChatItem.ChatType.ANSWER)); }); } } } ChatItem Get(string _message, ChatItem.ChatType _chat_type) { ChatItem _chat_item; if (chat_item_pool.Count > 0) { _chat_item = chat_item_pool[chat_item_pool.Count - 1]; chat_item_pool.Remove(_chat_item); _chat_item.gameObject.SetActive(true); } else { if (chat_item_prefab == null) chat_item_prefab = Resources.Load("Prefabs/UIItem/ChatItem"); _chat_item = Instantiate(chat_item_prefab); } _chat_item.Init(_message, _chat_type,!max_screen); _chat_item.transform.SetParent(chat_item_content); return _chat_item; } void Recycle(ChatItem _chat_item) { _chat_item.transform.SetParent(transform); chat_item_pool.Add(_chat_item); _chat_item.gameObject.SetActive(false); } }