-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(example): wait for the assets to be loaded
- Loading branch information
Showing
13 changed files
with
177 additions
and
89 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Assets/MediaPipe/Examples/Scripts/AsyncOperationAwaiter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using UnityEngine; | ||
|
||
public class AsyncOperationAwaiter<T> : INotifyCompletion where T : AsyncOperation { | ||
T _asyncOperation; | ||
|
||
public AsyncOperationAwaiter(T asyncOperation) { | ||
_asyncOperation = asyncOperation; | ||
} | ||
|
||
public bool IsCompleted { | ||
get { return _asyncOperation.isDone; } | ||
} | ||
|
||
public T GetResult() { | ||
return _asyncOperation; | ||
} | ||
|
||
public void OnCompleted(Action continuation) { | ||
_asyncOperation.completed += _ => { continuation(); }; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...Examples/Scripts/DemoAssetManager.cs.meta → ...les/Scripts/AsyncOperationAwaiter.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using UnityEngine; | ||
|
||
public static class AsyncOperationExtension { | ||
public static AsyncOperationAwaiter<T> GetAwaiter<T>(this T operation) where T : AsyncOperation { | ||
return new AsyncOperationAwaiter<T>(operation); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ipe/SDK/Scripts/Util/AssetManager.cs.meta → ...s/Scripts/AsyncOperationExtension.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
Assets/MediaPipe/Examples/Scripts/ResourceManager/AssetBundleManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Mediapipe; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
|
||
public sealed class AssetBundleManager : ResourceManager { | ||
private static readonly Lazy<AssetBundleManager> lazy = new Lazy<AssetBundleManager>(() => new AssetBundleManager()); | ||
public static AssetBundleManager 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 AssetBundleManager() : base() { | ||
if (!Directory.Exists(ModelCacheRootPath)) { | ||
Directory.CreateDirectory(ModelCacheRootPath); | ||
} | ||
} | ||
|
||
public async Task LoadAllAssetsAsync() { | ||
Debug.Log("Starting to load assets"); | ||
|
||
var bundleLoadReq = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, "AssetBundles", "mediapipe", "models")); | ||
|
||
if (bundleLoadReq.assetBundle == null) { | ||
Debug.LogError("Failed to load the AssetBundle"); | ||
return; | ||
} | ||
|
||
var assetLoadReq = await bundleLoadReq.assetBundle.LoadAllAssetsAsync<TextAsset>(); | ||
if (assetLoadReq.allAssets == null) { | ||
Debug.LogError("Failed to load assets"); | ||
return; | ||
} | ||
|
||
var loadTasks = assetLoadReq.allAssets.Select((asset) => WriteCacheFileAsync((TextAsset)asset)); | ||
await Task.WhenAll(loadTasks); | ||
Debug.Log("Loaded all assets"); | ||
} | ||
|
||
protected override string CacheFileFromAsset(string assetPath) { | ||
var assetName = Path.GetFileNameWithoutExtension(assetPath); | ||
var cachePath = CachePathFor(assetName); | ||
|
||
if (File.Exists(cachePath)) { | ||
return cachePath; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected 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); | ||
var bytes = asset.bytes; | ||
Debug.Log($"Saving {asset.name} to {path} (length={bytes.Length})"); | ||
|
||
FileStream sourceStream = null; | ||
|
||
try { | ||
sourceStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true); | ||
await sourceStream.WriteAsync(bytes, 0, bytes.Length); | ||
} finally { | ||
if (sourceStream != null) { | ||
sourceStream.Close(); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/MediaPipe/Examples/Scripts/ResourceManager/AssetBundleManager.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters