166 lines
4.9 KiB
C#
166 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
|
|
public class ChatUI : MonoBehaviour
|
|
{
|
|
public TextMeshProUGUI[] texts; // 0 이름
|
|
public ScrollRect m_ScrollRect;
|
|
public Transform tf_parent;
|
|
public GameObject go_chatGirlCard, go_chatMyCard, go_dayCard, go_pictureCard;
|
|
public TMP_InputField if_msg;
|
|
|
|
List<GameObject> list_chat = new List<GameObject>();
|
|
girltabledata m_Data;
|
|
|
|
public void Set(girltabledata data)
|
|
{
|
|
gameObject.SetActive(true);
|
|
|
|
m_Data = data;
|
|
texts[0].text = m_Data.s_Name;
|
|
ADInfo.Ins.Set_Banner(false);
|
|
|
|
for (int i = 0; i < list_chat.Count; i++)
|
|
Destroy(list_chat[i]);
|
|
list_chat.Clear();
|
|
|
|
var chatdata = SaveMgr.Ins.Get_ChatSaveData_orNull(m_Data.n_GirlID);
|
|
if (chatdata != null)
|
|
for (int i = 0; i < chatdata.histories.Count; i++)
|
|
Set_Chat(chatdata.histories[i]);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
ADInfo.Ins.Set_Banner(true);
|
|
}
|
|
|
|
void Set_Chat(ChatHistory chat)
|
|
{
|
|
if (chat.role.Equals("girl"))
|
|
{
|
|
var chatscript = DSUtil.Get_Clone<ChatGirlCard>(go_chatGirlCard, tf_parent);
|
|
chatscript.Set(m_Data.n_GirlID, chat.content);
|
|
list_chat.Add(chatscript.gameObject);
|
|
}
|
|
else if (chat.role.Equals("me"))
|
|
{
|
|
var chatscript = DSUtil.Get_Clone<ChatMyCard>(go_chatMyCard, tf_parent);
|
|
chatscript.Set(chat.content);
|
|
list_chat.Add(chatscript.gameObject);
|
|
}
|
|
|
|
Invoke("Set_ScrollEnd", Time.deltaTime);
|
|
}
|
|
|
|
void Set_ScrollEnd()
|
|
{
|
|
Canvas.ForceUpdateCanvases();
|
|
m_ScrollRect.verticalNormalizedPosition = 0;
|
|
}
|
|
|
|
public void OnClick_Send()
|
|
{
|
|
StartCoroutine(Send_Chat());
|
|
}
|
|
|
|
IEnumerator Send_Chat()
|
|
{
|
|
if (string.IsNullOrEmpty(if_msg.text)) yield break;
|
|
|
|
var myhistory = new ChatHistory
|
|
{
|
|
role = "me",
|
|
content = if_msg.text,
|
|
timestamp = InternetTime.Ins.Time.ToString()
|
|
};
|
|
// 내 히스토리 추가
|
|
SaveMgr.Ins.Add_ChatData(m_Data.n_GirlID, "", myhistory);
|
|
Set_Chat(myhistory);
|
|
|
|
string chaturl = $"https://navistalk-yrtti2co7a-dt.a.run.app/personas/{m_Data.s_PersonaId}/chat";
|
|
var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnYW1lSWQiOiJzaGVnb3R3ZXQiLCJwYXJ0bmVySWQiOiJ0b25ld29ya2VyIiwiZ2FtZVRva2VuIjoiZ2FtZVRva2VuMjIyMyIsImlhdCI6MTc1ODc4OTE5N30.BU52nWyA6oNtiPNoiO6z9J6B9xYxnTOr_bD-z_Xrw10";
|
|
|
|
var chatsaveData = SaveMgr.Ins.Get_ChatSaveData_orNull(m_Data.n_GirlID);
|
|
|
|
// ChatData 객체 생성
|
|
ChatData chatData = new ChatData
|
|
{
|
|
message = if_msg.text,
|
|
conversationId = chatsaveData == null ? "" : chatsaveData.conversationId,
|
|
userLanguage = "ko",
|
|
histories = chatsaveData == null ? new List<ChatHistory>() : chatsaveData.histories
|
|
};
|
|
|
|
// JSON 변환
|
|
string jsonData = JsonUtility.ToJson(chatData);
|
|
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
|
|
|
|
using (UnityWebRequest www = new UnityWebRequest(chaturl, "POST"))
|
|
{
|
|
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
// 헤더 설정
|
|
www.SetRequestHeader("Content-Type", "application/json");
|
|
www.SetRequestHeader("Accept", "application/json");
|
|
www.SetRequestHeader("Authorization", "Bearer " + token);
|
|
|
|
if_msg.text = "";
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.result == UnityWebRequest.Result.Success)
|
|
{
|
|
ChatResponeData data = JsonUtility.FromJson<ChatResponeData>(www.downloadHandler.text);
|
|
|
|
var girlchathistory = new ChatHistory
|
|
{
|
|
role = "girl",
|
|
content = data.response,
|
|
timestamp = InternetTime.Ins.Time.ToString()
|
|
};
|
|
|
|
// 걸 히스토리 추가
|
|
SaveMgr.Ins.Add_ChatData(m_Data.n_GirlID, data.conversationId, girlchathistory);
|
|
Set_Chat(girlchathistory);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Error: " + www.error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ChatHistory
|
|
{ // 최대 20개
|
|
public string role;
|
|
public string content;
|
|
public string timestamp;
|
|
}
|
|
|
|
[Serializable]
|
|
public class ChatData
|
|
{
|
|
public string message;
|
|
public string conversationId;
|
|
//public string userId;
|
|
public string userLanguage = "ko";
|
|
public List<ChatHistory> histories;
|
|
}
|
|
|
|
[Serializable]
|
|
public class ChatResponeData
|
|
{
|
|
public string ok;
|
|
public string response;
|
|
public string conversationId;
|
|
} |