using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChatAPITest : MonoBehaviour { //chat public Text chat_ansewr; [SerializeField] Text chat_message; [SerializeField] Transform trans; public InputField ask; public Dropdown uptextNum; public Button sendBtn; // Start is called before the first frame update void Start() { sendBtn.onClick.AddListener(() => { Chat(); }); } private void Update() { if (Input.GetKeyDown(KeyCode.Return)) { Chat(); } } List histryChat = new List(); private void Chat() { if (ask.text != "") { Text talk = Instantiate(chat_message, trans); talk.text = ask.text; talk.alignment = TextAnchor.MiddleRight; talk.GetComponent().enabled = false; chat_ansewr = Instantiate(chat_message, trans); chat_ansewr.text = "AI:思考中"; string askmsg = ask.text; ask.text = ""; if (uptextNum.value == 0) { IChatManager.instance.Chat(askmsg, result => { chat_ansewr.text = "AI:" + result; //ask.text = ""; histryChat.Add(new ChatInfo { ask = askmsg, answer = result }); });//95EC69 FFFFFF } else { //获取倒数第n个 List tmps = new List(); for (int i = histryChat.Count - 1; i >= 0 && tmps.Count <= uptextNum.value; i--) { tmps.Add(histryChat[i]); } IChatManager.instance.Chat(tmps, askmsg, result => { chat_ansewr.text = "AI:" + result; //ask.text = ""; histryChat.Add(new ChatInfo { ask = askmsg, answer = result }); }); } } } }