Som hľadal pre SDK, ktorý môže pristupovať k vnútorný priečinok (3D model zložky) z HoloLens a načítať ho do spustenej aplikácie a snažili sme sa veľa odkazov na bezvýsledne. Môže mi niekto pomôcť nám tento problém vyriešiť?
Vaša otázka je veľmi široký, ale aby som bol úprimný, UWP je chúlostivé témy. Píšem to na môj telefón, ale napriek tomu dúfam, že to pomôže vám začíname.
Po prvé: Na Hololens používa UWP
.
Za to file IO v UWP
aplikácie budete musieť použiť špeciálne c# API, ktorá funguje len vtedy, asynchrónne! Aby ste sa oboznámili s touto koncepciou a kľúčové slová async
, await
a Task
skôr ako začnete!
Ďalej upozorňujeme, že väčšina z Jednoty je API môžu byť použité len na Jednote hlavnú niť! Preto budete potrebovať špecializované triedy, ktoré umožňuje prijímať asynchrónna Action
s a odoslať ich do ďalšej Jednoty hlavné vlákno Update
hovor cez ConcurrentQueue
napr.
using System;
using System.Collections.Concurrent;
using UnityEngine;
public class MainThreadDispatcher : MonoBehaviour
{
private static MainThreadDispatcher _instance;
public static MainThreadDispatcher Instance
{
get
{
if (_instance) return _instance;
_instance = FindObjectOfType<MainThreadDispatcher>();
if (_instance) return _instance;
_instance = new GameObject(nameof(MainThreadDispatcher)).AddComponent<MainThreadDispatcher>();
return _instance;
}
}
private readonly ConcurrentQueue<Action> _actions = new ConcurrentQueue<Action>();
private void Awake()
{
if (_instance && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
}
public void DoInMainThread(Action action)
{
_actions.Enqueue(action);
}
private void Update()
{
while (_actions.TryDequeue(out var action))
{
action?.Invoke();
}
}
}
Teraz to povedal, vy ste pravdepodobne hľadá Windows.Storage.KnownFolders.Objects3D
čo je Windows.Storage.StorageFolder
.
Tu budete používať GetFileAsync
v snahe získať Windows.Storage.StorageFile
.
Potom môžete využiť Windows.Storage.FileIO.ReadBufferAsync
v záujme čítať obsah tohto súboru do IBuffer
.
A konečne, môžete použiť ToArray
v snahe získať suroviny byte[]
z nej.
Po tom všetkom máte na odoslanie výsledok späť do Jednoty hlavné vlákno, aby boli schopní používať ju tam (alebo pokračujte proces importu v inom async spôsobom).
Si môžete vyskúšať a používať niečo ako
using System;
using System.IO;
using System.Threading.Tasks;
#if WINDOWS_UWP // We only have these namespaces if on an UWP device
using Windows.Storage;
using System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions;
#endif
public static class Example
{
// Instead of directly returning byte[] you will need to use a callback
public static void GetFileContent(string filePath, Action<byte[]> onSuccess)
{
Task.Run(async () => await FileLoadingTask(filePath, onSuccess));
}
private static async Task FileLoadingTask(string filePath, Action<byte[]> onSuccess)
{
#if WINDOWS_UWP
// Get the 3D Objects folder
var folder = Windows.Storage.KnownFolders.Objects3D;
// get a file within it
var file = await folder.GetFileAsync(filePath);
// read the content into a buffer
var buffer = await Windows.Storage.FileIO.ReadBufferAsync(file);
// get a raw byte[]
var bytes = buffer.ToArray();
#else
// as a fallback and for testing in the Editor use he normal FileIO
var folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "3D Objects");
var fullFilePath = Path.Combine(folderPath, filePath);
var bytes = await File.ReadAllBytesAsync(fullFilePath);
#endif
// finally dispatch the callback action into the Unity main thread
MainThreadDispatcher.Instance.DoInMainThread(() => onSuccess?.Invoke(bytes));
}
}
Čo si potom počať s vrátené byte[]
je len na vás! Existuje mnoho loader implementácií a knižnice online pre rôzne typy súborov.
Ďalšie čítanie: