我一直在寻找一个SDK,可以访问的内部文件夹(3D模型文件夹)的。并将其加载到正在运行的应用程序,我们已经尝试了很多的链接也没有用。 任何人都可以帮助我们解决这个问题?
0
你的问题是极其广泛,但说实话应是一个棘手的问题。 我输入我的话虽然但我希望它可以帮助你开始。
第一:使用。 UWP
.
这样做的文件IO在 UWP
应用程序需要使用一种特殊c#API这只是工作 异! 因此,获得熟悉这一概念和关键词 async
, await
和 Task
之前你开始!
进一步注意到,大多数的统一的API只能用上的统一主线! 因此,你会需要一个专门类,让收到异 Action
s和派遣他们进入下一个团结的主线 Update
呼吁通过一个 ConcurrentQueue
例如:
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();
}
}
}
现在这种说你是最有可能在寻找 Windows.Storage.KnownFolders.Objects3D
这是一个 Windows.Storage.StorageFolder
.
在这里,你会使用 GetFileAsync
为了得到一个 Windows.Storage.StorageFile
.
然后你使用 Windows.Storage.FileIO.ReadBufferAsync
为了读取内容的这种文件纳入一个 IBuffer
.
最后你可以用 ToArray
为了获得原 byte[]
出来的。
在这之后你们派遣的结果回到团结的主线程,以便能够利用它在那里(或继续该进程中的另一个异步的方式)。
你可以尝试使用的东西喜欢
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));
}
}
什么你那么做的返回 byte[]
是给你的! 有很多的加载程序实现和图书馆网上的不同类型的文件。
进一步阅读: