-
-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): read local files in UnityEditor env
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.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,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); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.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