From e0e87951d9e4d304f60797280d96c58de3462f6d Mon Sep 17 00:00:00 2001 From: homuler Date: Sun, 11 Oct 2020 18:10:52 +0900 Subject: [PATCH] feat(example): read local files in UnityEditor env --- .../ResourceManager/LocalAssetManager.cs | 51 +++++++++++++++++++ .../ResourceManager/LocalAssetManager.cs.meta | 11 ++++ .../Examples/Scripts/SceneDirector.cs | 10 +++- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs create mode 100644 Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs.meta diff --git a/Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs b/Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs new file mode 100644 index 000000000..c6b9fcb34 --- /dev/null +++ b/Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs @@ -0,0 +1,51 @@ +using Mediapipe; +using System; +using System.IO; +using System.Threading.Tasks; +using UnityEngine; + +/// +/// Sample implementation of ResourceManager, that reads files from local filesystem. +/// This class is used in UnityEditor environment. +/// +public sealed class LocalAssetManager : ResourceManager { + private static readonly Lazy lazy = new Lazy(() => 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() {} + + /// dummy method + 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); + } +} diff --git a/Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs.meta b/Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs.meta new file mode 100644 index 000000000..78072ba5d --- /dev/null +++ b/Assets/MediaPipe/Examples/Scripts/ResourceManager/LocalAssetManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6250a2a740c7ba7e6924545fe5913d7e +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 81108756e..cd2189d28 100644 --- a/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs +++ b/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs @@ -45,10 +45,16 @@ async void Start() { 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);