Skip to content

Commit

Permalink
feat(example): use AssetBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Oct 10, 2020
1 parent 3a782c6 commit 25ce035
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
77 changes: 77 additions & 0 deletions Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs
Original file line number Diff line number Diff line change
@@ -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<DemoAssetManager> lazy = new Lazy<DemoAssetManager>(() => 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<TextAsset>().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);
};
}
}
11 changes: 11 additions & 0 deletions Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions Assets/MediaPipe/Examples/Scripts/SceneDirector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -46,6 +40,10 @@ public class SceneDirector : MonoBehaviour {
gpuHelper = new GlCalculatorHelper();
gpuHelper.InitializeForTest(gpuResources);
}
#if UNITY_EDITOR || UNITY_STANDALONE
DemoAssetManager.Instance.LoadAllAssetsAsync();
ResourceUtil.InitializeAssetManager(DemoAssetManager.Instance);
#endif
}

void OnDisable() {
Expand Down

0 comments on commit 25ce035

Please sign in to comment.