How to load a 3d model from URL in unity? - unity3d

What is the best way to load a 3d model from a URL inside of unity at runtime. I have tried this obj importer
http://wiki.unity3d.com/index.php?title=ObjImporter
But the mesh in not imported properly. What other options exist? Assetbundle needs a pro version and even that won't solve as I need to load a 3d model from url. Any suggestions?

The way to go would be asset bundles. They are Unity's (proprietary) way to load prepackaged, compressed assets from a file or an URL. All asset bundles must be prepared and built in Unity Editor.
If you need a more general approach which loads a raw file format like fbx, obj, 3ds, collada, et cetera, you should use the Assimp library. Assimp library is written in C++ but there is a wrapper for C#.NET here.
There are also wrappers for Unity like this Unity Assimp being free but quite outdated. The Asset Store though is packed with format specific importers including this free OBJ importer.
2021 update
Unity now also has Addressables which are an evolution of the aforementioned Asset Bundles. In a sentence, Addressables are memory managed, asynchronously loadable resources that can be loaded from local or remote end points.

There are a couple of libraries available to load 3d model from a URL in Unity.
These are the best two libraries to load the model from remote location at runtime.
Asynchronous Importer and run-time Loader for Unity
Runtime OBJ Importer
Asynchronous Importer and run-time Loader for Unity is quite a useful tool to import obj files from a remote location with texture and materials. where the Runtime OBJ Importer might miss the texture/material of the model.
Demos:
https://github.com/codemaker2015/unity3d-runtime-model-importer
https://github.com/codemaker2015/Runtime-OBJ-Importer-Demo

Related

Unity Asset Bundles Rebuilding with Old Data

I have a mobile project which displays various AR experiences to the user via a mobile application. This application is made in Unity.
Alongside this Unity project we have a second project that stores asset bundles exclusively. Each AR experience is a prefab marked with a unique asset bundle name. These bundles are stored online and downloaded into the main mobile application when relevant. We're having issues with having these bundles correctly update, when changing things on the prefab such as scale or rotation they aren't reflected in the rebuilt asset bundle.
Here's a look at the process we're using for rebuilding assets, It's only a simple script.
[MenuItem("Spiff/Build AssetBundles")]
static void BuildAllAssetBundles()
{
// BuildPlatformBundles(BuildTarget.iOS);
BuildPlatformBundles(BuildTarget.Android);
}
static void BuildPlatformBundles(BuildTarget platform)
{
// We convert the passed platform enum to a string.
string platformFolder = platform.ToString();
string assetBundleDirectory = "Assets/AssetBundles/" + platformFolder;
// Build our bundles out to the relevent platform specific folder.
if (!AssetDatabase.IsValidFolder(assetBundleDirectory))
{
AssetDatabase.CreateFolder("Assets/AssetBundles/", platformFolder);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, platform);
}
Currently we use 'BuildAssetBundleOptions.None' We've also tried the ForceRebuild flag but this has no effect. Am I correct in assuming that Unity has some sort of internal cache it keeps for assets bundles? If so can we clear this somehow so I can ensure 100% that the bundle I am building is going to be the most up to date based on the prefab tagged with it?
Loaded Assetbundles are indeed cached, and will remain so until AssetBundle.Unload() has been called to free up all memory accociated with the asset.
After the asset has been unloaded a newer version of the assetbundle can be downnloaded and instantiated with the new values (Assuming the file downloaded has all the updated information), else it will load the data from cache if available.
On a side note: It seems like you're still using the old assetbundle pipeline. Unity has a plugin tool that makes the assetbundle workflow considerably easier called the assetbundle browser found here. It comes with a way easier UI for building bundles and inspecting bundles, open source and a very customisable pipeline. It may be worth checking out.
Like remy_rm mentioned, do give the Unity Asset Bundle Browser tool a try. It gives more control over what assets are in the bundle.
Your problem can be solved if you ensure two things:
1- When you make changes to a prefab and create a new bundle, is it indeed created with the said changes? (again using the tool would make checking this easier)
2- The bundles you are downloading in the main application are the new ones and not the old cached ones. In Unity Webgl for instance, you need to clear the browser cache to be 100% sure that the application downloads the new asset bundles and doesn't use the old ones in browser cache.

Load an asset from outside a 'resources' folder

I'd like to load "dynamic" assets (in particular, FBX files) that are located outside of a "Resources" folder so that the user can add/remove/modify files with no need to re-build the project.
To make those files easily accessible for the user in different kinds of devices (target platforms are desktop/android/ios), it seems reasonable to use either Application.persistentDataPath or Application.streamingAssetsPath.
Now the problem is: How do I load them (as proper Unity objects) ?
This obviously can't be done via Resources.Load() as they are outside a "Resources" folder.
Using WWW, retrieving the files should be easy and it could actually work for some types of files (e.g. textures, see http://docs.unity3d.com/ScriptReference/WWW-texture.html) but I cannot see a way to load something else.
The idea is that I'd like to be able to use those loaded objects like so:
(GameObject) Instantiate(loadedObject, new Vector3(0, 0, 0), Quaternion.identity);
I would really appreciate your help (preferrably with a solution that does not require a "pro license").
I know two options that you can try to use in this case:
Build an asset bundle for each dynamic asset you want to load, but this seems not really what you desire, because the user still have to use Unity3d to generate the asset bundle.
Use an custom 3d model loader, like described in this post, seems the best fit for your case.

Is there any way to import solidworks files into unity as assets?

I'm attempting to program a simple chess game using the unity game engine. However, I can't work out how to import the assets that I've created in Solidworks into Unity.
Is it possible to do?
SolidWorks files tend to describe parametric geometry, such as NURBs, but most game engines prefer to work with raw polygons.
Unity has direct support for the following model formats: FBX, OBJ, DAE, 3DS.
SolidWorks can export to STL format, which other programs should be able to convert to OBJ, DAE, and/or FBX. Recommending a specific tool is beyond the scope of Stack Overflow, but I know that such tools exist. I recommend searching terms such as "stl to obj conversion".
These sorts of headaches are fairly common with 3D assets, since so many programs use customized file formats.
Also with 3DS studio max, you can directly open Solidwork files and then export as them as .fbx (you need to have Solidworks installed in the same machine as 3DS Max).
about SolidWorks (SLDPRT, SLDASM) Files:
http://help.autodesk.com/cloudhelp/2015/ENU/3DSMax/files/GUID-861F1B25-0F1B-4E79-A660-821F4CD2175B.htm
Other free tool for converting 3D files: (can save as .obj, which unity reads)
MeshLab
http://meshlab.sourceforge.net/
*Update for 2018:
Unity has partnered with this CAD importer company, and its available to purchase from: https://unity3d.com/pixyz
They support pretty much all the available CAD formats, including SolidWorks
full importer list at https://www.pixyz-software.com/plugin/
Just for reference, since Solidworks 2019 it is not necessary to use any AddIn/3rd Party Plugin. It is supported out of the box: https://help.solidworks.com/2019/english/whatsnew/t_export_using_extended_reality.htm
the "XR exporter" in SOLIDWORKS will allow you to export the model with some additional content (animation, appearances, metadata) to the GLTF / GLB fileformat and using the GLTF importer for unity (should be free) you're done.

Errors loading scripts on prefabs from asset bundles

I'm currently trying to set up my project to allow users to download mini games post release. Todo this I am trying to use asset bundles to facilitate this, one containing the new games scene and one containing the assets and DLLs for the scripts required. The problem I am having is that when I load the scene or a prefab from the bundle all the attached scripts are still there but a warning says "the associated script can not be loaded. please fix any compile errors and assign a valid script". Is there something obvious that I am missing or do asset bundles not work this way and I'm going to have to rebuild my scene on load?
You can't have the scripts in the asset bundles assigned as references from GameObjects like you normally would. You can only access scripts from asset bundles by loading them with reflection. Then you can programmatically attach them to GameObjects, etc. from there (for example, load the assembly, find your Type, then use AddComponent() on a GameObject with your new loaded type).
See http://docs.unity3d.com/Manual/scriptsinassetbundles.html.
But even if you were to overcome these hurdles, it won't work on Windows Phone and it's against the terms of service of Apple's store, so ...

Creating walkthrough dynamically in unity3d

I want to using the Unity3d framework for a project in which I have create a walkthrough. But the client want it to be dynamic.
We would be getting the final fbx file from 3DMax which when added to a particular folder a walkthrough should be created automatically. I went through the help files on Unity3D site but did not find any documentation as to how use the Unity3D framework programmatically.
Can anyone please let me know how can it be done? Even if link is provided for the resource it would help us.
Unity 3D has a built in way to load models at runtime, but this should be done using a Resources folder (Resources.Load()) or using a Asset Bundles (AssetBundle.Load()) as described here: http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html
These two methods are performed inside the Unity editor and your need is load any arbitrary 3D model file at runtime, for this case you will need a plugin. The only one that I know is ObjReader.