72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
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<ChatInfo> histryChat = new List<ChatInfo>();
|
||
private void Chat()
|
||
{
|
||
if (ask.text != "")
|
||
{
|
||
Text talk = Instantiate(chat_message, trans);
|
||
talk.text = ask.text;
|
||
talk.alignment = TextAnchor.MiddleRight;
|
||
talk.GetComponent<ChatText>().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<ChatInfo> tmps = new List<ChatInfo>();
|
||
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 });
|
||
});
|
||
|
||
}
|
||
}
|
||
}
|
||
} |