For a class project, I’m developing an Android racing game that can support user-created content, and I’ve run across a bit of a problem.
In the Resources folder in the Editor, I have separate “cars” and “tracks” folders for the user to put their content, each consisting of a folder with an .fbx mesh and image textures. The script uses the Directory.GetDirectories() function to create the list of tracks and cars the user has in the game, and the game uses these array entries in place of literal strings in Resources.Load().
However, this all seems to go out of the window, as once I build the .apk for Android, the Resources folder must be condensed into one instance, because GetDirectories() doesn’t work at this point. Of course, this means the cars and tracks don’t load. However, if I type out the entire path in a script, the objects will load. Also, I can’t find where the .apk installs on my device after I open it, so I can’t even see what’s going on between files and folders.
I’ve started working with the WWW class, but I don’t think that can instantiate GameObjects. Any help would be greatly appreciated. Thank you!
The resources folder is only a folder inside the editor. On build Unity will put the data that is inside a asset container called resources.assets that contain all the runtime data of the files that where in the resources folder.
You can not move in or out any files to or from the “resources” since its no longer a folder.
You may be able to load images from the streaming asset path of the app via the WWW class but an fbx file would require you to have code in place to convert the fbx to a usable mesh in runtime. (Units tools to convert fbx to a mesh is not available at runtime)
There seems to be a asset in the assetstore for importing FBX at runtime, maybe worth a look for you:
Awesome. I’ve managed to combine native cars and user-added cars into one array, which the user will scroll through on the select screen. Once the player scrolls to an added car, it will load for the first instance. Each added car will load once when the player scrolls to it. But when the player returns to that car, the mesh will not load. Native cars will still load in their expected position, regardless. How do I set up the following code so that the mesh loads each time this method is called?
// Loads car from external SD card
void LoadCarFromSD(string path)
{
Destroy(GameObject.Find("car"));
WWW w = new WWW("file://" + path);
GameObject car = Instantiate((GameObject)w.assetBundle.LoadAsset("car.fbx"));
car.name = "car";
string[] name;
foreach (Transform child in car.transform)
{
// Code to apply textures and materials
}
SetUpCar(car); // External method to finalize components on car
}