在统一编辑器和已部署的应用程序中加载3d模型 [英] Loading 3d models in unity editor and in deployed app

查看:240
本文介绍了在统一编辑器和已部署的应用程序中加载3d模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序,它使用我在 UnityEditor.AssetDatabase.LoadAssetAtPath 中加载的编辑器中的模型。如果我尝试部署应用程序,则表示无法使用Unity Editor。

I've created an app that make use of models that in editor I load using UnityEditor.AssetDatabase.LoadAssetAtPath. If I try to deploy the app, it says that Unity Editor can't be used.

那么在编辑器之外我应该在运行时加载我的模型的最佳策略是什么?

So which is the best strategy that I should apply to load my models at run time, outside the editor?

推荐答案

请务必在 UnityEditor 命名空间中检查您使用的API是不是。如果它只是一个编辑器插件,你可以将它包装在 UNITY_EDITOR

Always check that the API you are using is not in the UnityEditor namespace. If it's just an Editor plugin, you can wrap it around UNITY_EDITOR.

#if UNITY_EDITOR
using UnityEditor;
#endif

但它不是编辑器插件,因此无法正常工作。

But it's not an Editor plugin so that wound't work.


那么我应该在编辑器之外的
运行时加载我的模型的最佳策略是什么?

So which is the best strategy that I should apply to load my models at run time, outside the editor?

有两种方法可以在Unity中加载文件

There are really two ways to load files in Unity

1 .AssetBundles(推荐)

1.AssetBundles(Recommended)

最好的方法是使用AssetBundles。在Assets文件夹中创建一个文件夹,名称为StreamingAssets。创建AssetBundles然后将AssetBundles放在那里。您可以使用Unity的 WWW API和 Application.streamingAssetsPath 作为加载它的路径。

The best way is to use AssetBundles. Create a folder in the Assets folder and na e name is "StreamingAssets". Create AssetBundles then put the AssetBundles there. You can use Unity's WWW API with Application.streamingAssetsPath as the path to load it.

public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
public string result = "";
IEnumerator Example() {
    if (filePath.Contains("://")) 
    {
        WWW www = new WWW(filePath);
        yield return www;
        result = www.text;
    } else
        result = System.IO.File.ReadAllText(filePath);
}

2 .Resources文件夹

2.Resources folder

创建一个名为Resources的文件夹,然后将所有文件放在那里。然后,您可以使用Resources API加载它。 这篇帖子提供了有关此方法的更多信息。我认为你应该避免使用这种方法,但它确实值得一提。

Create a folder named "Resources" then put all your files there. You can then load it with the Resources API. This post has more information about this method. I do think you should avoid using this method but it's really worth to be mentioned.

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;

这篇关于在统一编辑器和已部署的应用程序中加载3d模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆