101 lines
3.7 KiB
C#
101 lines
3.7 KiB
C#
using DG.Tweening;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using ZXKFramework;
|
|
namespace YiLiao.XinFeiTingZhen
|
|
{
|
|
public class AIPanel : UIBase
|
|
{
|
|
public override string GroupName => "AIPanel";
|
|
public override string Name => "AIPanel";
|
|
public Action CloseAction;
|
|
public Action AIFinishAction;
|
|
Text showText;
|
|
public Transform p1; // ÆðʼµãTransform
|
|
public Transform p2; // ½áÊøµãTransform
|
|
public RectTransform container;
|
|
public AudioSource audioSource;
|
|
bool playSound;
|
|
public GameObject objText;
|
|
public override void Init(IUIManager uictrl)
|
|
{
|
|
base.Init(uictrl);
|
|
transform.FindFirst<Button>("closeBtn").onClick.AddListener(Close);
|
|
showText = transform.FindFirst<Text>("AIText");
|
|
}
|
|
public void SendStateToAI(string str, Action AIFinishAction,Action closeAction, bool showCloseBtn = true, bool playSound = true)
|
|
{
|
|
SetActive(true);
|
|
objText.gameObject.SetActive(true);
|
|
ChatAI.Instance.SendData(str, SuccessBack);
|
|
this.CloseAction = closeAction;
|
|
this.AIFinishAction = AIFinishAction;
|
|
transform.FindFirst<Button>("closeBtn").gameObject.SetActive(showCloseBtn);
|
|
this.playSound = playSound;
|
|
}
|
|
Coroutine coroutine;
|
|
public void SuccessBack(string str)
|
|
{
|
|
string s = Regex.Replace(str, @"^\s*$\n|\r", "", RegexOptions.Multiline).Replace("[", "").Replace("]", "").Replace("#", "").Replace("*", "").Trim().Replace(" ", "");
|
|
if (playSound)
|
|
{
|
|
//Debug.Log(s);
|
|
ChatAI.Instance.PlayVoice(s, c => {
|
|
objText.gameObject.SetActive(false);
|
|
container.DOMove(p1.position, 0.5f);
|
|
audioSource.clip = c;
|
|
audioSource.Play();
|
|
coroutine = Game.Instance.IEnumeratorManager.Run(TypeText(c.length, s, str => {
|
|
showText.text = str;
|
|
}));
|
|
});
|
|
}
|
|
else
|
|
{
|
|
objText.gameObject.SetActive(false);
|
|
showText.text = s;
|
|
container.DOMove(p1.position, 0.5f);
|
|
ChatAI.Instance.PlayVoice(s, c =>{
|
|
audioSource.clip = c;
|
|
audioSource.Play();
|
|
});
|
|
}
|
|
AIFinishAction?.Invoke();
|
|
}
|
|
private void Close()
|
|
{
|
|
Game.Instance.IEnumeratorManager.Stop(coroutine);
|
|
container.transform.position = p2.position;
|
|
SetActive(false);
|
|
showText.text = "";
|
|
CloseAction?.Invoke();
|
|
ChatAI.Instance.StopVoice();
|
|
ChatAI.Instance.StopSend();
|
|
audioSource.Stop();
|
|
audioSource.clip= null;
|
|
}
|
|
IEnumerator TypeText(float totalTime, string fullText, Action<string> _callBack)
|
|
{
|
|
float timePerCharacter = totalTime / fullText.Length;
|
|
for (int i = 0; i <= fullText.Length; i++)
|
|
{
|
|
_callBack?.Invoke(fullText.Substring(0, i));
|
|
yield return new WaitForSeconds(timePerCharacter);
|
|
}
|
|
}
|
|
public override void Hide()
|
|
{
|
|
base.Hide();
|
|
Game.Instance.IEnumeratorManager.Stop(coroutine);
|
|
container.transform.position = p2.position;
|
|
showText.text = "";
|
|
ChatAI.Instance.StopVoice();
|
|
ChatAI.Instance.StopSend();
|
|
audioSource.Stop();
|
|
audioSource.clip = null;
|
|
}
|
|
}
|
|
} |