【问题标题】:Loading 3d models in unity editor and in deployed app在统一编辑器和部署的应用程序中加载 3d 模型
【发布时间】:2017-06-29 17:37:03
【问题描述】:

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

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

【问题讨论】:

    标签: c# unity3d import model assets


    【解决方案1】:

    始终检查您使用的 API 不是UnityEditor 命名空间中。如果它只是一个编辑器插件,你可以将它包裹在 UNITY_EDITOR 周围。

    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    

    但它不是编辑器插件,所以伤口不起作用。

    那么,我应该采用哪种策略来加载我的模型 运行时,在编辑器之外?

    在Unity中加载文件确实有两种方式

    1.AssetBundles(推荐)

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

    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 文件夹

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 2022-11-26
      • 2017-11-07
      • 1970-01-01
      相关资源
      最近更新 更多