public static void LinkClipsAfterImport() { for (int i = lastImportStartingLineCount; i < EasyVoiceSettings.instance.data.LineCount(); i++) { string assetFileName, fullFileName; EasyVoiceClipCreator.GenerateFullFileName(i, out assetFileName, out fullFileName); AudioClip foundClip = (AudioClip)AssetDatabase.LoadAssetAtPath(assetFileName, typeof(AudioClip)); if (foundClip != null) { EasyVoiceSettings.instance.data.SetClip(i, foundClip); // don't mark changed though } } EasyVoiceIssueChecker.CheckAllLineIssues(); }
//public static EasyVoiceSettings.AudioImportSettings.SampleRate SampleRate(AudioSampleRateSetting sampleRate) //{ // switch (sampleRate) // { // case AudioSampleRateSetting.PreserveSampleRate: // return EasyVoiceSettings.AudioImportSettings.SampleRate.PreserveSampleRate; // case AudioSampleRateSetting.OptimizeSampleRate: // return EasyVoiceSettings.AudioImportSettings.SampleRate.OptimizeSampleRate; // case AudioSampleRateSetting.OverrideSampleRate: // return EasyVoiceSettings.AudioImportSettings.SampleRate.OverrideSampleRate; // default: // return EasyVoiceSettings.AudioImportSettings.SampleRate.PreserveSampleRate; // Unity has a new value? // } //} public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { // This will make sure that if any changed assets match our lines, then these lines will have their clip/file name re-checked for issues (or no more issues) if (EasyVoiceSettings.instance != null && EasyVoiceSettings.instance.data != null) // we are not yet initialized? { EasyVoiceDataAsset data = EasyVoiceSettings.instance.data; for (int lineIndex = 0; lineIndex < data.LineCount(); lineIndex++) { //string assetFileName, fullFileName; //EasyVoiceClipCreator.GenerateFullFileName(lineIndex, out assetFileName, out fullFileName); //if ((deletedAssets != null && deletedAssets.Contains(assetFileName)) || // (movedAssets != null && movedAssets.Contains(assetFileName)) || // (importedAssets != null && importedAssets.Contains(assetFileName)) || // (movedFromAssetPaths != null && movedFromAssetPaths.Contains(assetFileName))) //{ -- WE CAN'T RELY ON THIS, IT DOESN'T TELL US WHAT THE FILE NAME *WAS* (and keeping this cached is an overkill imo) EasyVoiceIssueChecker.VerifyFileNameOrClip(lineIndex); // break; //} } } }
public static bool CreateFiles(EasyVoiceSettings settings) { if (CreateFilesPreCheck(settings) != CreateFilesCheckResult.ok) // this duplicates Editor Window UI, btu is quick enough { return(false); } List <int> created = new List <int>(); lastCreatedAssetFileNames.Clear(); // if this is not empty, something wen't wrong? well, we can't fix it anyway without knowing details, and if we know details, we probably already either addressed them or notified the user //List<List<LineIssue>> issues = EasyVoiceIssueChecker.GetLineIssues(settings); List <int> lineIndices = new List <int>(); for (int i = 0; i < settings.data.LineCount(); i++) { if (!settings.data.GetOutputStatus(i)) { continue; } EasyVoiceIssueChecker.CheckLineIssues(i); if (IssuePreventsFileMaking(settings.data.GetIssues(i))) // most overwriting is "okay" { continue; } lineIndices.Add(i); } for (int i = 0; i < lineIndices.Count; i++) { int lineIndex = lineIndices[i]; string assetFileName; // this is Unity internal asset path starting with "Assets\" and including extension string fullFileName; // this is the file name, including the system path and extension GenerateFullFileName(lineIndex, out assetFileName, out fullFileName); #if DEBUG_MESSAGES Debug.Log(assetFileName); #endif settings.querier.AskToMakeFile( settings.data.GetSpeechText(lineIndex), settings.data.GetSpeakerNameOrSelectDefault(lineIndex), fullFileName, settings.osxFileFormat); // Check if the file has the wrong extension if (Path.GetExtension(fullFileName) != settings.querier.FileExtension) { #if DEBUG_MESSAGES Debug.Log("Audio clip file has a different extension than expected (\"" + settings.querier.FileExtension + "\")"); #endif int extensionIndex = fullFileName.LastIndexOf('.'); if (extensionIndex != 0) { string newName = fullFileName.Substring(0, extensionIndex) + settings.querier.FileExtension; File.Move(fullFileName, newName); extensionIndex = assetFileName.LastIndexOf('.'); if (extensionIndex != 0) // just in case asset file name is messed up { assetFileName = assetFileName.Substring(0, extensionIndex) + settings.querier.FileExtension; } #if DEBUG_MESSAGES Debug.Log("Renamed audio clip asset to \"" + newName + "\" and internal asset path to \"" + assetFileName + "\""); #endif } } created.Add(lineIndex); lastCreatedAssetFileNames.Add(assetFileName); settings.data.SetOutputStatus(lineIndex, false); EditorUtility.DisplayProgressBar(EasyVoiceSettings.progressDialogTitle, EasyVoiceSettings.progressDialogText, (i + 1f) / (lineIndices.Count + 1)); } #if UNITY_EDITOR //AssetDatabase.Refresh(); -- using AssetDatabase.ImportAsset instead #endif #if DEBUG_MESSAGES foreach (string lastCreatedAssetFileName in lastCreatedAssetFileNames) { Debug.Log("Asset will be expected for linking: " + lastCreatedAssetFileName); } #endif if (settings.linkClips) { for (int i = 0; i < created.Count; i++) { // Force asset to import AssetDatabase.ImportAsset(lastCreatedAssetFileNames[i], ImportAssetOptions.ForceSynchronousImport); // Get the new asset AudioClip foundClip = (AudioClip)AssetDatabase.LoadAssetAtPath(lastCreatedAssetFileNames[i], typeof(AudioClip)); //if (foundClip != null) //{ // System.Threading.Thread.Sleep(500); // wait a little bit of OS to possibly refresh, then retry // AssetDatabase.ImportAsset(lastCreatedAssetFileNames[i], ImportAssetOptions.ForceSynchronousImport); // foundClip = (AudioClip)AssetDatabase.LoadAssetAtPath(lastCreatedAssetFileNames[i], typeof(AudioClip)); //} if (foundClip != null) { #if DEBUG_MESSAGES Debug.Log("Audio clip to link was found at: " + lastCreatedAssetFileNames[i]); #endif // if (!lastCreatedAssetFileNames[i].EndsWith(settings.querier.FileExtension)) // { //#if DEBUG_MESSAGES // Debug.Log("Audio clip has a different extension than expected (\"" + settings.querier.FileExtension + "\")"); //#endif // int extensionIndex = lastCreatedAssetFileNames[i].LastIndexOf('.'); // if (extensionIndex > 0 && extensionIndex > lastCreatedAssetFileNames[i].Length - 10) // some sanity // { // int slashIndex = lastCreatedAssetFileNames[i].LastIndexOf('/'); // if (slashIndex != -1 && slashIndex < extensionIndex) // some more sanity // { // string newName = lastCreatedAssetFileNames[i].Substring(slashIndex + 1, extensionIndex - slashIndex - 1) + settings.querier.FileExtension; //#if DEBUG_MESSAGES // Debug.Log("Renaming audio clip asset to \"" + newName + "\""); //#endif // Debug.Log(AssetDatabase.RenameAsset(lastCreatedAssetFileNames[i], newName)); -- CAN'T, extension isn't renamed // } // } // } settings.data.SetClip(created[i], foundClip, false); // TODO: VERIFY? } else { Debug.LogWarning("EasyVoice expected an AudioClip asset to be generated at \"" + lastCreatedAssetFileNames[i] + "\", but it wasn't found!"); } } } #if DEBUG_MESSAGES else { Debug.Log("Settings not set to link created clips, skipping."); } #endif lastCreatedAssetFileNames.Clear(); return(true); }
public static ImportResult ImportData(string fileName, EasyVoiceSettings settings, bool append) { try { if (!File.Exists(fileName)) { Debug.LogWarning("EasyVoice.ImportData tried to open the file returned by Unity's dialog, but such file doesn't appear to exist: \"" + fileName + "\"!"); return(ImportResult.fail); } FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream, settings.exportCSVFileEncodingUTF8 ? Encoding.Unicode : Encoding.ASCII); string header = streamReader.ReadLine(); List <string> headerSplit = SplitLine(header); if (headerSplit == null || headerSplit.Count != 6 || headerSplit[0] != "ID" || headerSplit[1] != "Group" || headerSplit[2] != "Status" || headerSplit[3] != "Speaker" || headerSplit[4] != "Text" || headerSplit[5] != "File name") { Debug.LogWarning("EasyVoice.ImportData opened the CSV the file, but the header syntax did not match the expected format!"); return(ImportResult.fail); } List <int> tempIds = new List <int>(); List <string> tempGroups = new List <string>(); List <byte> tempStatuses = new List <byte>(); List <string> tempSpeakerNames = new List <string>(); List <string> tempSpeechTexts = new List <string>(); List <string> tempFileNames = new List <string>(); List <LineIssue> tempIssues = new List <LineIssue>(); while (!streamReader.EndOfStream) { string line = streamReader.ReadLine(); List <string> splitLine = SplitLine(line); //Debug.Log(splitLine); if (splitLine != null && splitLine.Count == 6) { int id; if (!int.TryParse(splitLine[0], out id)) { Debug.LogWarning("EasyVoice.ImportData opened the CSV the file, but an encountered id field syntax did not match the expected format!"); return(ImportResult.fail); } tempIds.Add(id); tempGroups.Add(splitLine[3]); byte status; if (!byte.TryParse(splitLine[2], out status)) { Debug.LogWarning("EasyVoice.ImportData opened the CSV the file, but an encountered status field syntax did not match the expected format!"); return(ImportResult.fail); } tempStatuses.Add(status); tempSpeakerNames.Add(splitLine[3]); tempSpeechTexts.Add(splitLine[4]); tempFileNames.Add(splitLine[5]); tempIssues.Add(0); } else { Debug.LogWarning("EasyVoice.ImportData opened the CSV the file, but an encountered line syntax did not match the expected format!"); return(ImportResult.fail); } } streamReader.Close(); fileStream.Close(); if (!append) { settings.data.DeleteAllLines(); } lastImportStartingLineCount = settings.data.LineCount(); bool defaultFolderValid = settings.IsDefaultFolderValid(); // so we don't check this too many times bool clipFound = false; for (int i = 0; i < tempSpeakerNames.Count; i++) { settings.data.AddNewLine(tempIds[i], tempGroups[i], tempStatuses[i], tempSpeakerNames[i], tempSpeechTexts[i], tempFileNames[i], tempIssues[i]); // If we need to find a clip (haven't found one yet) and also settings are valid to do this check if (!clipFound && defaultFolderValid) { string assetFileName, fullFileName; EasyVoiceClipCreator.GenerateFullFileName(i, out assetFileName, out fullFileName); AudioClip foundClip = (AudioClip)AssetDatabase.LoadAssetAtPath(assetFileName, typeof(AudioClip)); if (foundClip != null) { clipFound = true; } } } EasyVoiceIssueChecker.CheckAllLineIssues(); // this will get called again after linking clips, but we don't know if user will decide to do this yet return(clipFound ? ImportResult.matchingClipsFound : ImportResult.okay); } catch (Exception e) { Debug.LogError("EasyVoice.ImportData encountered an error: " + e); return(ImportResult.fail); } }