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();
         }
     }*/
}
相关推荐
程序猿多布6 小时前
预定义委托(C# and Unity)
unity·c#
虾球xz9 小时前
游戏引擎学习第108天
学习·游戏引擎
虾球xz10 小时前
游戏引擎学习第112天
java·学习·游戏引擎
Edision_li16 小时前
DeepSeek教unity------Dotween
unity·游戏引擎
zfoo-framework17 小时前
Unity中NavMesh的使用 及其 导出给java服务端进行寻路
unity
程序猿多布17 小时前
数学函数(C#、Lua 、Unity)
unity·c#·lua
虾球xz19 小时前
游戏引擎学习第110天
数码相机·学习·游戏引擎
爱写代码的山山1 天前
虚幻蓝图解决抗锯齿方案
游戏·ue5·游戏引擎·虚幻·抗锯齿化
github_czy1 天前
(9/100)每日小游戏平台系列
javascript·python·游戏引擎·游戏程序
头发掉光的程序员1 天前
正顺基碱基
c++·算法·游戏引擎·图形渲染