diff --git a/Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs b/Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs new file mode 100644 index 000000000..efb3a76f6 --- /dev/null +++ b/Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs @@ -0,0 +1,77 @@ +using Mediapipe; +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using UnityEngine; + +public sealed class DemoAssetManager : AssetManager { + private static readonly Lazy lazy = new Lazy(() => new DemoAssetManager()); + public static DemoAssetManager Instance { get { return lazy.Value; } } + private readonly static string CacheRootPath = Path.Combine(Application.persistentDataPath, "Cache"); + private readonly static string ModelCacheRootPath = Path.Combine(CacheRootPath, "MediaPipe", "Models"); + + private AssetBundle _mediaPipeModels; + private AssetBundle MediaPipeModels { + get { + if (_mediaPipeModels != null) { + return _mediaPipeModels; + } + + return _mediaPipeModels = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "AssetBundles", "mediapipe", "models")); + } + } + + private DemoAssetManager() : base() { + if (!Directory.Exists(ModelCacheRootPath)) { + Directory.CreateDirectory(ModelCacheRootPath); + } + } + + public async void LoadAllAssetsAsync() { + var bundle = MediaPipeModels; + + if (bundle == null) { + Debug.LogError("Failed to load assets"); + return; + } + + await Task.WhenAll(bundle.LoadAllAssets().Select((asset) => WriteCacheFileAsync(asset)).ToArray()); + } + + public override string CacheFileFromAsset(string assetPath) { + var assetName = Path.GetFileNameWithoutExtension(assetPath); + var cachePath = CachePathFor(assetName); + + if (File.Exists(cachePath)) { + return cachePath; + } + + return null; + } + + public override bool ReadFile(string path, IntPtr dst) { + var cachePath = CacheFileFromAsset(path); + + if (!File.Exists(cachePath)) { + return false; + } + + var asset = File.ReadAllBytes(cachePath); + ResourceUtil.CopyBytes(dst, asset); + return true; + } + + private string CachePathFor(string assetName) { + return Path.Combine(ModelCacheRootPath, assetName); + } + + private async Task WriteCacheFileAsync(TextAsset asset) { + var path = CachePathFor(asset.name); + + using (FileStream sourceStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)) { + var bytes = asset.bytes; + await sourceStream.WriteAsync(bytes, 0, bytes.Length); + }; + } +} diff --git a/Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs.meta b/Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs.meta new file mode 100644 index 000000000..7bb1124a3 --- /dev/null +++ b/Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 59b6eca57891f074aae0dc89ce1a14ef +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs b/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs index f77c072b8..c21ad6fc8 100644 --- a/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs +++ b/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs @@ -22,19 +22,13 @@ public class SceneDirector : MonoBehaviour { void OnEnable() { var nameForGlog = Path.Combine(Application.dataPath, "MediaPipePlugin"); - - #if UNITY_EDITOR || UNITY_STANDALONE - var logDir = Path.Combine(Application.dataPath.Replace("/Assets", ""), "Logs", "MediaPipe"); - #else - var logDir = Path.Combine(Application.persistentDataPath, "Logs", "MediaPipe"); - #endif + var logDir = Path.Combine(Application.persistentDataPath, "Logs", "MediaPipe"); if (!Directory.Exists(logDir)) { Directory.CreateDirectory(logDir); } UnsafeNativeMethods.InitGoogleLogging(nameForGlog, logDir); - ResourceUtil.SetResourceRootPath(Application.streamingAssetsPath); } void Start() { @@ -46,6 +40,10 @@ void Start() { gpuHelper = new GlCalculatorHelper(); gpuHelper.InitializeForTest(gpuResources); } + #if UNITY_EDITOR || UNITY_STANDALONE + DemoAssetManager.Instance.LoadAllAssetsAsync(); + ResourceUtil.InitializeAssetManager(DemoAssetManager.Instance); + #endif } void OnDisable() {