Unity文字转语音(使用RT-Voice PRO [2023.1.0])

参考文章Unity插件——文字转朗读语音RtVioce插件功能/用法/下载_rtvoice-CSDN博客

一、使用步骤

1.导入进Unity(插件形式为 .unitypackage)

https://download.csdn.net/download/luckydog1120446388/88717512

2.添加所需Prefab

1).右键可以直接添加到 Hierarchy面板
Unity文字转语音(使用RT-Voice PRO [2023.1.0])_第1张图片
2).添加后的样子
Unity文字转语音(使用RT-Voice PRO [2023.1.0])_第2张图片

3.代码调用 RtVioceLocalTTS.cs

using Crosstales.RTVoice;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// 
/// creat by yxx 
/// 
/// https://blog.csdn.net/qq_43246529/article/details/109899257
/// 
public class RtVioceLocalTTS : MonoBehaviour
{
    /*
 插件 - RtVioce
支持的Unity版本:5.3.1 及以上版本
功能:语音转文字
*不需要为自己的声音行事
*多个音色变换
*多个扬声器 同时对讲(说话)
*NPC聊天转换等。。。。   
?生成的音频可以存储到文件中。*/
    public InputField mTxtSpeakContent;
    public Button mBtnClick;
    private string mID;
    // Start is called before the first frame update
    void Start()
    {
        mBtnClick.onClick.AddListener(() =>
        {
            //Speak(mTxtSpeakContent.text);
            mID = Speaker.Instance.Speak(mTxtSpeakContent.text, null, Speaker.Instance.Voices[0]);
        });
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            mID = Speaker.Instance.Speak("测试语音功能是否正常!Test whether the voice function is normal", null, Speaker.Instance.Voices[0]);
            //Speaker.Instance.SpeakNative("测试语音功能是否正常!Test whether the voice function is normal", Speaker.Instance.Voices[0], 1, 1, 1);
            Debug.Log("开始ID:" + mID);
            Speaker.Instance.OnSpeakStart += SpeakStart;
            Speaker.Instance.OnSpeakComplete += SpeakComplete;
            //Speaker.Instance.Speak("why");//测试语音功能是否正常!
        }
        if (Input.GetKeyDown(KeyCode.O))
        {
            Speaker.Instance.Silence(mID);
            mID = Speaker.Instance.Speak("我的目的是来测试id的", null, Speaker.Instance.Voices[0]);
            Debug.Log("开始ID:" + mID);
            Speaker.Instance.OnSpeakComplete += SpeakComplete;
        }
        if (Input.GetKeyDown(KeyCode.M))
        {
            Speaker.Instance.PauseOrUnPause();
        }
        if (Input.GetKeyDown(KeyCode.N))
        {
            Speaker.Instance.PauseOrUnPause();//
        }

        if (Input.GetKeyDown(KeyCode.Y))
        {
            Speaker.Instance.Silence(mID);//静默(介绍播放)
        }
    }
    private void SpeakStart(Crosstales.RTVoice.Model.Wrapper wrapper)
    {
        Debug.Log("开始播放ID:" + wrapper.Uid);
    }
    private void SpeakComplete(Crosstales.RTVoice.Model.Wrapper wrapper)
    {
        Debug.Log("完成ID:" + wrapper.Uid);
        if (wrapper.Uid.Equals(mID))
        {

        }
    }

    public void Speak(string _conetnt)
    {
        mID = Speaker.Instance.Speak(_conetnt, null, Speaker.Instance.Voices[0]);
    }
    /* public SpeechText SpeechText;
     // Start is called before the first frame update
     void Start()
     {
        *//* Speaker.Speak("哇塞,我可以说话了!", null, null, true, GUISpeech.Rate, GUISpeech.Volume, "", GUISpeech.Pitch);
         Speaker.OnSpeakComplete += speakEndMethod;
         Speaker.OnSpeakStart += speakStartMethod;*//*
         //取消调用
         //Speaker.OnSpeakStart-= speakStartMethod;
         //Speaker.OnSpeakComplete += speakEndMethod;

         //Speaker.Silence();//停止说话
     }
     private void speakStartMethod()//SpeakEventArgs e)
     {
         print("开始说话");
     }
     private void speakEndMethod()//SpeakEventArgs e)
     {
         print("完成说话");
     }
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.A))
         {
             SpeechText.Speak();
         }
     }*/
}

你可能感兴趣的:(数字人,unity,游戏引擎)