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();
         }
     }*/
}
相关推荐
虾球xz9 小时前
游戏引擎学习第124天
java·学习·游戏引擎
Thomas_YXQ1 天前
Unity3D 战斗系统架构与设计详解
开发语言·unity·架构·系统架构·unity3d
Nicole Potter1 天前
内存泄漏指什么?常见的内存泄漏有哪些?
开发语言·游戏·unity·面试·c#
Jditinpc1 天前
Unity FBXExport导出的FBX无法在Blender打开
unity·游戏引擎·blender
虾球xz2 天前
游戏引擎学习第122天
学习·游戏引擎
Thinbug2 天前
UE(虚幻)学习(五)初学创建NPC移动和遇到的问题
学习·游戏引擎·虚幻
虾球xz2 天前
游戏引擎学习第123天
学习·游戏引擎
虾球xz2 天前
游戏引擎学习第121天
学习·游戏引擎
太妃糖耶2 天前
Unity地形系统
unity·游戏引擎
别偷喝船长的朗姆酒2 天前
【Unity】鱼群效果模拟
unity·游戏引擎