简体   繁体   中英

Loading 3d models in unity editor and in deployed app

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?

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?

There are really two ways to load files in Unity

1 .AssetBundles(Recommended)

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 folder

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM