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面板

2).添加后的样子

3.代码调用 RtVioceLocalTTS.cs

cs 复制代码
using Crosstales.RTVoice;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// creat by yxx 
/// 
/// https://blog.csdn.net/qq_43246529/article/details/109899257
/// </summary>
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();
         }
     }*/
}
相关推荐
神码编程1 天前
【Unity】HTModuleManager(三)Markdown语法的Unity编辑器方言
unity·markdown·模块管理器
井队Tell1 天前
打造高清3D虚拟世界|零基础学习Unity HDRP高清渲染管线(第十二天)
学习·3d·unity
PerfumerKarma2 天前
【渲染引擎基础】圣杯架构——固定逻辑时长+插值渲染
架构·游戏引擎
地狱为王2 天前
Unity使用AnimeGANv3实现动漫风格化效果(二)
unity·游戏引擎
fcm192 天前
unity之线框模式
unity·游戏引擎
unity工具人2 天前
unity DoTween DoPath设置物体按照指定轨迹运动
unity·游戏引擎
程序猿阿伟2 天前
《风格锚点+动态适配:Unity跨设备渲染的核心逻辑》
unity·游戏引擎
倚剑仙3 天前
Unity-WebGL开发——用IIS(Internet Information Services)部署webGL工程
unity·游戏引擎·webgl
baldr_43963 天前
关于光照探针的实验和疑问
unity
作孽就得先起床3 天前
c#调Lua返回个字符串
unity·c#·lua·xlua