using System.IO; using Unity.InferenceEngine; using UnityEngine; using UnityEngine.Networking; namespace Audio2Lipsync { /// /// Locates and loads the packaged model with no setup step: /// - Editor: straight from the package's Model~ folder (nothing is copied into Assets). /// - Player: from StreamingAssets/Audio2Lipsync/, where the package's build hook puts it for /// the duration of each build. /// public static class Audio2LipsyncModel { public const string FileName = "audio2lipsync.sentis"; public const string StreamingAssetsFolder = "Audio2Lipsync"; public static string StreamingAssetsPath => Path.Combine(Application.streamingAssetsPath, StreamingAssetsFolder, FileName); #if UNITY_EDITOR /// Model~/audio2lipsync.sentis inside the resolved package (editor only). public static string PackagedPath { get { var pkg = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(Audio2LipsyncModel).Assembly); return pkg == null ? null : Path.Combine(pkg.resolvedPath, "Model~", FileName); } } #endif /// /// Where reads the model from: a model placed in /// StreamingAssets/Audio2Lipsync/ wins (override); otherwise, in the editor, the packaged one. /// public static string DefaultPath { get { #if UNITY_EDITOR if (!File.Exists(StreamingAssetsPath) && PackagedPath != null) return PackagedPath; #endif return StreamingAssetsPath; } } /// /// Loads the default model. Android / WebGL keep StreamingAssets inside the package or on a /// server, so those are fetched with UnityWebRequest; everywhere else it is a plain file read. /// public static async Awaitable LoadAsync() { var path = DefaultPath; if (path.Contains("://")) { using var req = UnityWebRequest.Get(path); await req.SendWebRequest(); if (req.result != UnityWebRequest.Result.Success) throw new FileNotFoundException($"Audio2Lipsync model not loaded from {path}: {req.error}"); using var ms = new MemoryStream(req.downloadHandler.data); return ModelLoader.Load(ms); } if (!File.Exists(path)) throw new FileNotFoundException( $"Audio2Lipsync model not found: {path}. If the package came from git, install git-lfs and re-resolve it."); if (new FileInfo(path).Length < 1024 * 1024) throw new InvalidDataException($"{path} is a Git LFS pointer, not the model. Install git-lfs and re-resolve the package."); return ModelLoader.Load(path); } } }