// Every frame check for callbacks & perform any found private System.Collections.IEnumerator PerformMainThreadCallbacks() { // While checking, continue while (HasMainThreadCallbacks()) { // Wait for frame if (Application.isPlaying && !Application.isBatchMode) { yield return(new WaitForEndOfFrame()); } // Wait for a tick else { yield return(null); } // Perform if possible while (_mainThreadCallbacks.Count > 0 && _mainThreadCallbacks.TryDequeue(out var result)) { result(); } } // Done performing _performer = null; }
// Iterate phrases private static void IteratePhrases(TTSService service, TTSPreloadData preloadData, TTSPreloadIterateDelegate onIterate, Action <float> onProgress, Action <string> onComplete) { // No service if (service == null) { onComplete?.Invoke("\nNo TTSService found in current scene"); return; } // No preload data if (preloadData == null) { onComplete?.Invoke("\nTTS Preload Data Not Found"); return; } // Ignore if running if (Application.isPlaying) { onComplete?.Invoke("Cannot preload while running"); return; } // Unload previous coroutine performer if (_performer != null) { MonoBehaviour.DestroyImmediate(_performer.gameObject); _performer = null; } // Run new coroutine _performer = CoroutineUtility.StartCoroutine(PerformIteratePhrases(service, preloadData, onIterate, onProgress, onComplete)); }
// While active, perform any sent callbacks private void WatchMainThreadCallbacks() { // Ifnore if already performing if (_performer != null) { return; } // Check callbacks every frame (editor or runtime) _performer = CoroutineUtility.StartCoroutine(PerformMainThreadCallbacks()); }
// Request setup public virtual void Setup(UnityWebRequest newRequest, Action <float> newProgress, Action <UnityWebRequest> newComplete) { // Already setup if (_request != null) { return; } // Setup _request = newRequest; _onProgress = newProgress; _onComplete = newComplete; _transmitting = false; _progress = 0f; // Use default timeout if (newRequest.timeout <= 0) { newRequest.timeout = Timeout; } // Begin _coroutine = CoroutineUtility.StartCoroutine(PerformUpdate()); }