com.sky.sentis.audio2lipsync / Runtime /Audio2LipsyncModel.cs
Sky-Kim's picture
Initial commit
116878d
Raw History Blame Contribute Delete
3.03 kB
using System.IO;
using Unity.InferenceEngine;
using UnityEngine;
using UnityEngine.Networking;
namespace Audio2Lipsync
{
/// <summary>
/// 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.
/// </summary>
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
/// <summary>Model~/audio2lipsync.sentis inside the resolved package (editor only).</summary>
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
/// <summary>
/// Where <see cref="LoadAsync"/> reads the model from: a model placed in
/// StreamingAssets/Audio2Lipsync/ wins (override); otherwise, in the editor, the packaged one.
/// </summary>
public static string DefaultPath
{
get
{
#if UNITY_EDITOR
if (!File.Exists(StreamingAssetsPath) && PackagedPath != null) return PackagedPath;
#endif
return StreamingAssetsPath;
}
}
/// <summary>
/// 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.
/// </summary>
public static async Awaitable<Model> 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);
}
}
}