Instantiate 3D models from an external folder - unity3d

I have small project that I am creating to learn a bit more about Unity.
In this project I have a lot of 3D models, and I need to use only some of them. I am using external text file that the program reads when executed, in order to tell it which models to load.
For example, in the text file I can write:
Hunter, CandyHouse
and the program would instantiate those 2 models only.
My problem is that I don't want to save all the models in the Resources folder and load onluly those I need, because that would increase my program size drastically. So what I want to do is to save the models I need next to the text file and instantiate them. But I don't know how to access them from Unity.
I have found the WWW class in some answer, but they used it to load textures, when I tried to load Maya file with it, Unity just stopped responding.
Is there anyway to do it¿

Use model make a prefab.(This can decrease draw call when you need many model in scene.)
Use this prefab make a assetbundle.
put this assetbundle in your url(this could be a remote server url or a file url.Note that iOS/Android may have problem on permission of read/write).
And use the code follows to load.
WWW www = new WWW("url");
yield return www;
GameObject obj = Object.Instantiate(www.assetBundle.Load("ModelName")) as GameObject;
We could also use LoadFromCacheOrDownload,the asset will be save speficy location.So no need everytime load from web.

Related

What happens if you modify a image when it's open?

I was doing a geometry dash private server with a friend then I noticed that we can modify images in ftp while someone is running the game so I want to ask what will happen if we rename the image when someone is playing?
It depends on what image you're talking about. Gamesheets get loaded on starting the game so renaming them will do nothing while running the game but detail images like the vault faces will only be loaded when needed. Since the names of these images are hardcoded you need to change those before renaming them, otherwise it will just crash due to missing files.

How could I load a Static Mesh from external folder?

I'm learning UE4 C++ Development and I have a idea but I don't know if that is possible to make. I have a folder on my WAMP SERVER for exemplo ( C:\wamp\www\staticmesh\My3DModel.3DS ). My idea is create a button on my UE4 widget and when It's pressed it import my Static Mesh from my folder to my currently scene. It's possible ?
OBS: because my game will be too heavy, so I thought I would import the static meshs and put in the scene in real time, without building next to the game. If you know another solution or any idea it will be welcome. **
Would Somebody be willing to helping me step by step How could I implementing that code below ? It's very very important to me, because I'm learning UE4 C++.
// Load Static Mesh From Path
static FORCEINLINE UStaticMesh* LoadMeshFromPath(const FName& Path)
{
if(Path == NAME_None) return NULL;
//~
return LoadObjFromPath<UStaticMesh>(Path);
}
Thank you so much for any answer.
You are asking a bit much if all you did yet is draw that sketch.
If you did more already please share the information.
I recently answered what I could identify as core part of your question tho - how to get the mesh from local drive into ue4 -
https://stackoverflow.com/a/41753091/5196012
This also features the link to the wiki where your code sample seems to come from.
You are missing the template the person has on the wiki
//TEMPLATE Load Obj From Path
template <typename ObjClass>
static FORCEINLINE ObjClass* LoadObjFromPath(const FName& Path)
{
if(Path == NAME_None) return NULL;
//~
return Cast<ObjClass>(StaticLoadObject( ObjClass::StaticClass(), NULL,*Path.ToString()));
}
Since no one knows what selectionstrategy you have I guess it will be some standard file browser dialog returning a file handle...
All you would need to do then is to call your function in the widget button "on pressed" event.
OBS: because my game will be too heavy, so I thought I would import the static meshs and put in the scene in real time, without building next to the game.
That is not possible. The moment you import a static mesh it will be saved as a .uasset file in your content folder.
How big are your meshes and how small is your disk that you cannot import them?
If you absolutely do not want to import any assets you can generate your meshes at runtime with this plugin: https://www.unrealengine.com/marketplace/runtime-mesh-component
Note however that you lose a lot of extra goodies and optimizations with that approach.

Force Unity to save prefabs to disk

Using Unity 5.4.2.
I've created an editor script that implements an exporter for some prefab data when you press an export button. It recursively finds all the prefab files matching a certain type under its own directory and loads those prefabs so they can be exported in a custom manner.
The problem I'm having is that the prefabs on disk, that are loaded with
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(file);
do not match the current state in the editor. That is if you have modified some of the prefab data in the editor, it is not actually saved to disk. Note that this does not require any objects to be added to a scene, I am just exporting data directly out of components attached to prefabs.
I have tried adding AssetDatabase.SaveAssets(); to the top of my export function, which is supposed to save all the assets, but when I edit a value and then log the values loaded from the prefabs, they are not up to date. There are two (non-programatic) methods I have found so far that cause my exporter to be able to see the modified data:
If I exit Unity and restart it, the prefabs appear to get saved at that point and the exporter sees the updated data.
If I play and stop any scene, then export, the exporter can see the updated data, but oddly (according to git status) they have not been written to disk, so I suspect LoadAssetAtPath actually loads some cached version which is updated on 'play'.
Is there a way to force Unity to save everything to disk so that my exporter can operate on the most up to date values?
The following should do the trick
// Your changes go here
AssetDatabase.Save();
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); // perhaps even ForceSynchronousImport?
// You loading should go here
For more information about ImportAssetOptions see this documentation https://docs.unity3d.com/ScriptReference/ImportAssetOptions.html
Note: This does not answer the original question, but identifying and working around the cause may help anyone with the same/similar problem.
My problem was when exporting computed-and-cached properties in components attached to the prefabs. My assumption that LoadAssetAtPath would load a fresh instance from disk was wrong, it returns the version already in editor memory, which means cached values aren't invalidated like they would be with a fresh load.
The solution I used was to never cache when running in the editor, using #ifdef UNITY_EDITOR.

Marker not working properly in ARtoolkit in Unity

I am working on a simple AR project using Unity and ARToolkit. For testing purposes, I have created a test project to track the image and create a simple 3d sphere and it is working perfectly when I play it from inside the Unity editor. The problem is that when I create the build .exe of the project, the application does not augment the 3D model of the marker when I put the marker in front of Camera (it should work as it is working when playing from inside the editor). It is also showing two errors as I build the project. Note that I have included the .dll files in the folder where .exe file is located.
Also I see that after building the project, the UID of the marker disappears as well as it is showing perfectly when played from inside the Unity Editor.
Kindly guide me in this matter as I have to submit this project to the university final year project. ThankYou
several things on this:
the disappearing of the marker ID during the build of the APP is normal and does not affect your result
Please check if you have selected the correct scene when building your APP:
Build view
select the "Add open scenes" button and tick the scene(s) you would like to include in your APP.
(It happens quite often that the wrong scene is picked)
Also ensure that you are copying the correct versions of the dlls (32bit vs. 64bit) but if you copied the ones from the [appname]_data/Plugins directory you should be fine.
Let me know if that works for you.

how do I use resources.load to call a 2D sprite from the asset folder to the camera?

like it is from the title,
how can I use the script resources.load to call on items which exist in the assets folder to be called runtime into the camera ?
if not resource.load is there any other way to load items on runtime ?
Resources.Load only can retrieve assets stored in the Resources folder under Assets. That is because the path is used by the method. You can have sub folder and provide the path in the url of your asset but you cannot store something under Assets/MyFolder/SomeTexture.png and use Resources.Load to find it. In this case, you'd have to create a reference and drag the asset into it in inspector.
The difference between Resources folder assets and others is that whatever is inside Resources is packed and shipped with the project. The reason is that Unity is no able to know if the app will use it, so to avoid a null reference, it goes with.
Assets contained within the Assets folder are only shipped if Unity finds a reference in code in the Inspector via a drag. This makes the build smaller as it is also compressed with better algorithm.