/// <summary> /// Imported json data into an existing TTSPreloadSettings asset /// </summary> public static bool ImportData(TTSPreloadSettings preloadSettings, string textFilePath) { // Check for file if (!File.Exists(textFilePath)) { Debug.LogError($"TTS Preload Utility - Preload file does not exist\nPath: {textFilePath}"); return(false); } // Load file string textFileContents = File.ReadAllText(textFilePath); if (string.IsNullOrEmpty(textFileContents)) { Debug.LogError($"TTS Preload Utility - Preload file load failed\nPath: {textFilePath}"); return(false); } // Parse file WitResponseNode node = WitResponseNode.Parse(textFileContents); if (node == null) { Debug.LogError($"TTS Preload Utility - Preload file parse failed\nPath: {textFilePath}"); return(false); } // Iterate children for texts WitResponseClass data = node.AsObject; Dictionary <string, List <string> > textsByVoice = new Dictionary <string, List <string> >(); foreach (var voiceName in data.ChildNodeNames) { // Get texts list List <string> texts; if (textsByVoice.ContainsKey(voiceName)) { texts = textsByVoice[voiceName]; } else { texts = new List <string>(); } // Add text phrases string[] voicePhrases = data[voiceName].AsStringArray; if (voicePhrases != null) { foreach (var phrase in voicePhrases) { if (!string.IsNullOrEmpty(phrase) && !texts.Contains(phrase)) { texts.Add(phrase); } } } // Apply textsByVoice[voiceName] = texts; } // Import return(ImportData(preloadSettings, textsByVoice)); }
/// <summary> /// Create a new preload settings asset at specified location /// </summary> public static TTSPreloadSettings CreatePreloadSettings(string savePath) { // Ignore if empty if (string.IsNullOrEmpty(savePath)) { return(null); } // Get asset path string assetPath = savePath.Replace("\\", "/"); if (!assetPath.StartsWith(Application.dataPath)) { Debug.LogError( $"TTS Preload Utility - Cannot Create Setting Outside of Assets Directory\nPath: {assetPath}"); return(null); } assetPath = assetPath.Replace(Application.dataPath, "Assets"); // Generate & save TTSPreloadSettings settings = ScriptableObject.CreateInstance <TTSPreloadSettings>(); AssetDatabase.CreateAsset(settings, assetPath); AssetDatabase.SaveAssets(); // Reload & return return(AssetDatabase.LoadAssetAtPath <TTSPreloadSettings>(assetPath)); }
/// <summary> /// Prompt user for a json file to be imported into an existing TTSPreloadSettings asset /// </summary> public static bool ImportData(TTSPreloadSettings preloadSettings) { // Select a file string textFilePath = EditorUtility.OpenFilePanel("Select TTS Preload Json", Application.dataPath, "json"); if (string.IsNullOrEmpty(textFilePath)) { return(false); } // Import with selected file path return(ImportData(preloadSettings, textFilePath)); }
/// <summary> /// Find all preload settings currently in the Assets directory /// </summary> public static TTSPreloadSettings[] GetPreloadSettings() { List <TTSPreloadSettings> results = new List <TTSPreloadSettings>(); string[] guids = AssetDatabase.FindAssets("t:TTSPreloadSettings"); foreach (var guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); TTSPreloadSettings settings = AssetDatabase.LoadAssetAtPath <TTSPreloadSettings>(path); results.Add(settings); } return(results.ToArray()); }
/// <summary> /// Imported dictionary data into an existing TTSPreloadSettings asset /// </summary> public static bool ImportData(TTSPreloadSettings preloadSettings, Dictionary <string, List <string> > textsByVoice) { // Import if (preloadSettings == null) { Debug.LogError("TTS Preload Utility - Import Failed - Null Preload Settings"); return(false); } // Whether or not changed bool changed = false; // Generate if needed if (preloadSettings.data == null) { preloadSettings.data = new TTSPreloadData(); changed = true; } // Begin voice list List <TTSPreloadVoiceData> voices = new List <TTSPreloadVoiceData>(); if (preloadSettings.data.voices != null) { voices.AddRange(preloadSettings.data.voices); } // Iterate voice names foreach (var voiceName in textsByVoice.Keys) { // Get voice index if possible int voiceIndex = voices.FindIndex((v) => string.Equals(v.presetVoiceID, voiceName)); // Generate voice TTSPreloadVoiceData voice; if (voiceIndex == -1) { voice = new TTSPreloadVoiceData(); voice.presetVoiceID = voiceName; voiceIndex = voices.Count; voices.Add(voice); } // Use existing else { voice = voices[voiceIndex]; } // Get texts & phrases for current voice List <string> texts = new List <string>(); List <TTSPreloadPhraseData> phrases = new List <TTSPreloadPhraseData>(); if (voice.phrases != null) { foreach (var phrase in voice.phrases) { if (!string.IsNullOrEmpty(phrase.textToSpeak) && !texts.Contains(phrase.textToSpeak)) { texts.Add(phrase.textToSpeak); phrases.Add(phrase); } } } // Get data List <string> newTexts = textsByVoice[voiceName]; if (newTexts != null) { foreach (var newText in newTexts) { if (!string.IsNullOrEmpty(newText) && !texts.Contains(newText)) { changed = true; texts.Add(newText); phrases.Add(new TTSPreloadPhraseData() { textToSpeak = newText }); } } } // Apply voice voice.phrases = phrases.ToArray(); voices[voiceIndex] = voice; } // Apply data if (changed) { preloadSettings.data.voices = voices.ToArray(); EditorUtility.SetDirty(preloadSettings); } // Return changed return(changed); }