Skip to content

Commit

Permalink
feat(example): read local files in UnityEditor env
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Oct 11, 2020
1 parent 4cd0d2e commit e0e8795
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Mediapipe;
using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// Sample implementation of ResourceManager, that reads files from local filesystem.
/// This class is used in UnityEditor environment.
/// </summary>
public sealed class LocalAssetManager : ResourceManager {
private static readonly Lazy<LocalAssetManager> lazy = new Lazy<LocalAssetManager>(() => new LocalAssetManager());
public static LocalAssetManager Instance { get { return lazy.Value; } }
private readonly static string ModelRootPath = Path.Combine(Application.dataPath, "MediaPipe", "SDK", "Models");

private LocalAssetManager() : base() {}

/// <summary>dummy method</summary>
public async Task LoadAllAssetsAsync() {
await Task.CompletedTask;
}

protected override string CacheFileFromAsset(string assetPath) {
var assetName = GetAssetName(assetPath);
var localPath = GetLocalFilePath(assetName);

if (File.Exists(localPath)) {
return localPath;
}

return null;
}

protected override bool ReadFile(string path, IntPtr dst) {
var localPath = CacheFileFromAsset(path);
var asset = File.ReadAllBytes(localPath);
ResourceUtil.CopyBytes(dst, asset);
return true;
}

private string GetAssetName(string assetPath) {
var assetName = Path.GetFileNameWithoutExtension(assetPath);
var extension = Path.GetExtension(assetPath);

return extension == ".tflite" ? $"{assetName}.bytes" : $"{assetName}.txt";
}

private string GetLocalFilePath(string assetName) {
return Path.Combine(ModelRootPath, assetName);
}
}

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

10 changes: 8 additions & 2 deletions Assets/MediaPipe/Examples/Scripts/SceneDirector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ public class SceneDirector : MonoBehaviour {
gpuHelper.InitializeForTest(gpuResources);
}

ResourceUtil.InitializeResourceManager(AssetBundleManager.Instance);
#if UNITY_EDITOR
var resourceManager = LocalAssetManager.Instance;
#else
var resourceManager = AssetBundleManager.Instance;
#endif

ResourceUtil.InitializeResourceManager(resourceManager);

try {
await AssetBundleManager.Instance.LoadAllAssetsAsync();
await resourceManager.LoadAllAssetsAsync();
IsAssetLoaded = true;
} catch (Exception e) {
Debug.LogError(e);
Expand Down

0 comments on commit e0e8795

Please sign in to comment.