Unity VR项目集成IBM Watson TTS遇多类错误,寻求解决方案
背景说明
- 需求:搭建带输入框的小型Unity VR场景,输入文本后按回车,通过IBM Watson TTS输出语音
- 已完成工作:参照Watson Unity SDK示例完成场景搭建(截图:
)及代码编写,代码如下:
using IBM.Watson.TextToSpeech.V1; using IBM.Watson.TextToSpeech.V1.Model; using IBM.Cloud.SDK.Utilities; using IBM.Cloud.SDK.Authentication; using IBM.Cloud.SDK.Authentication.Iam; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using IBM.Cloud.SDK; namespace IBM.Watson.Examples { public class ExampleTextToSpeechV1 : MonoBehaviour { #region PLEASE SET THESE VARIABLES IN THE INSPECTOR [Space(10)] [Tooltip("The IAM apikey.")] [SerializeField] private string iamApikey = "APIKEY"; [Tooltip("The service URL (optional). This defaults to \"https://api.us-south.text-to-speech.watson.cloud.ibm.com\"")] [SerializeField] private string serviceUrl = "https://api.eu-de.text-to-speech.watson.cloud.ibm.com/instances/SERVICEINSTANCE"; private TextToSpeechService service; private string allisionVoice = "en-US_AllisonV3Voice"; private string synthesizeText = "Hello, my Name is Niclas."; private string placeholderText = "Please type text here and press enter."; private string waitingText = "Watson Text to Speech service is synthesizing the audio!"; private string synthesizeMimeType = "audio/wav"; public InputField textInput; private bool _textEntered = false; private AudioClip _recording = null; private byte[] audioStream = null; #endregion private void Start() { LogSystem.InstallDefaultReactors(); Runnable.Run(CreateService()); } void Update() { if (Input.GetKeyDown(KeyCode.Return)) { Runnable.Run(ExampleSynthesize(textInput.text)); } } private IEnumerator CreateService() { if (string.IsNullOrEmpty(iamApikey)) { throw new IBMException("Please add IAM ApiKey to the Iam Apikey field in the inspector."); } IamAuthenticator authenticator = new IamAuthenticator(apikey: iamApikey); while (!authenticator.CanAuthenticate()) { yield return null; } service = new TextToSpeechService(authenticator); if (!string.IsNullOrEmpty(serviceUrl)) { service.SetServiceUrl(serviceUrl); } } #region Synthesize Example private IEnumerator ExampleSynthesize(string text) { if (string.IsNullOrEmpty(text)) { text = synthesizeText; Log.Debug("ExampleTextToSpeechV1", "Using default text, please enter your own text in dialog box!"); } byte[] synthesizeResponse = null; AudioClip clip = null; service.Synthesize( callback: (DetailedResponse<byte[]> response, IBMError error) => { synthesizeResponse = response.Result; Log.Debug("ExampleTextToSpeechV1", "Synthesize done!"); clip = WaveFile.ParseWAV("myClip", synthesizeResponse); PlayClip(clip); }, text: text, voice: allisionVoice, accept: synthesizeMimeType ); while (synthesizeResponse == null) yield return null; yield return new WaitForSeconds(clip.length); } #endregion #region PlayClip private void PlayClip(AudioClip clip) { if (Application.isPlaying && clip != null) { GameObject audioObject = new GameObject("AudioObject"); AudioSource source = audioObject.AddComponent<AudioSource>(); source.spatialBlend = 0.0f; source.loop = false; source.clip = clip; source.Play(); GameObject.Destroy(audioObject, clip.length); } } #endregion } }
运行后出现的错误列表
Saving has no effect. Your class 'UnityEditor.XR.Simulation.XREnvironmentViewManager' is missing the FilePathAttribute. Use this attribute to specify where to save your ScriptableSingleton. Only call Save() and use this attribute if you want your state to survive between sessions of Unity.UnityEditor.XR.Simulation.XREnvironmentViewManager:OnDisable () (at ./Library/PackageCache/com.unity.xr.arfoundation@5.1.0/Editor/Simulation/XREnvironmentViewManager.cs:169)
Multiple scripted importers are targeting the extension 'glb' and have all been rejected: Microsoft.MixedReality.Toolkit.Utilities.Gltf.Serialization.Editor.GlbAssetImporter (assembly: D:\INDISUnity\INDIS\Library\ScriptAssemblies\Microsoft.MixedReality.Toolkit.Gltf.Importers.dll), GLTFast.Editor.GltfImporter (assembly: D:\INDISUnity\INDIS\Library\ScriptAssemblies\glTFast.Editor.dll)UnityEditor.AssetImporters.ScriptedImporter:RegisterScriptedImporters ()
Multiple scripted importers are targeting the extension 'gltf' and have all been rejected: Microsoft.MixedReality.Toolkit.Utilities.Gltf.Serialization.Editor.GltfAssetImporter (assembly: D:\INDISUnity\INDIS\Library\ScriptAssemblies\Microsoft.MixedReality.Toolkit.Gltf.Importers.dll), GLTFast.Editor.GltfImporter (assembly: D:\INDISUnity\INDIS\Library\ScriptAssemblies\glTFast.Editor.dll)UnityEditor.AssetImporters.ScriptedImporter:RegisterScriptedImporters ()
The referenced script (Unknown) on this Behaviour is missing!No GetDeviceLayoutName() override detected in HP Reverb G2 Controller Profile. Binding path validator for this interaction profile is not as effective. To fix, add GetDeviceLayoutName and GetInteractionProfileType override in this profile.UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()
错误解决方案
1. XREnvironmentViewManager 保存无效错误
这是AR Foundation包的编辑器脚本内部警告,不影响场景运行。若要修复:
- 更新AR Foundation到最新稳定版本,官方大概率已修复该问题;
- 若无需AR功能,直接在Package Manager中移除AR Foundation包;
- 不建议修改官方包的Editor脚本,避免后续版本冲突。
2. & 3. GLB/GLTF导入器冲突错误
Microsoft MRTK与GLTFast两个插件同时注册了GLB/GLTF格式的导入器,导致Unity无法识别有效导入器。解决方法:
- 二选一移除插件:若不需要MRTK的GLTF导入能力,在Package Manager中删除
Microsoft.MixedReality.Toolkit.Gltf.Importers包;若不需要GLTFast,直接删除GLTFast插件文件; - 修改导入器优先级(不推荐):找到其中一个导入器脚本,修改
[ScriptedImporter]属性的优先级参数(如[ScriptedImporter(0, "glb")],数字越小优先级越高),但需修改插件源码,可能影响后续更新。
4. 引用脚本丢失错误
场景中存在GameObject的脚本组件指向已被删除、移动或重命名的脚本文件。解决步骤:
- 点击Console中的该错误,Unity会自动定位到对应的GameObject;
- 在Inspector面板中找到显示
Missing Script的组件,要么删除该组件,要么点击组件的Select按钮重新关联正确的脚本文件。
5. HP Reverb G2控制器配置文件警告
这是XR交互配置的警告,不影响基础功能,但会降低控制器绑定的有效性检查精度。解决方法:
- 更新XR交互工具包:将XR Interaction Toolkit或MRTK更新到支持HP Reverb G2的最新版本,官方可能已完善该配置;
- 手动添加重写方法:找到HP Reverb G2的Controller Profile脚本,添加以下两个方法的重写:
public override string GetDeviceLayoutName() { return "HPMotionController"; // 确认HP Reverb G2的设备布局名称,可在XR输入设置中查看 } public override InteractionProfileType GetInteractionProfileType() { return InteractionProfileType.GenericXRController; }
内容的提问来源于stack exchange,提问作者SHOUTER

