- Home /
Instantiating Prefabs on Android Mobile Not Working
EDIT : I have made a detailed approach to solve this problem to any lost soul on the internet trying to make it work down the page.
Hello chaps! I am instantiating prefabs in my simple game like this.
GameObject prefab = (GameObject) Resources.Load("Prefabs/" + _type + "/" + _prefab, typeof(GameObject));
GameObject instance = GameObject.Instantiate(prefab);
instance.transform.position = anchor_object.transform.position;
instance.transform.rotation = anchor_object.transform.rotation;
It works fine in my editor. But when I do it in my mobile android build, nothing appears, ever. This is not an event problem I instantiate on Start. I've been searching for a couple hours, but the only answer were obsolete from 2013 and around. Can't seem to make this work.
Anyone knows how to save myself from becoming mad? Thank you.
Answer by Guy_Yome · Feb 10, 2019 at 07:17 PM
Here I finally manage to force my way trough this problem! For the folks out there searching a solution for this problem you might wanna read this carefully.
First of all, you got to start making asset bundles.
To do so you must create a script that will help you make asset bundles in the first place. You need to make it in the Assets/Editor directory. Name it however you want but mine is called ExportAssetBundle.cs
using UnityEditor;
using System.IO;
public class CreateAssetBundles {
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles() {
string assetBundleDirectory = "Assets/StreamingAssets";
if(!Directory.Exists(assetBundleDirectory)) {
Directory.CreateDirectory(assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.Android);
}
}
Put this code inside. This will make possible to create bundles automatically.
Now, it's written all over the internet to name objects to asset bundles so they get implemented in the build of the application when ready. Create a folder named StreamingAssets in Assets folder.
You now need to have the Resources folder at the root of Assets with your textures, prefabs, etc. inside. You can now select them from the project tab and assign them to AssetBundles inside the inspector right at the bottom.
Now opening the selection field shown at the bottom right of the above picture, you have the option to assign the prefab to an existing asset bundle or create a new one. Reminder : The name is all in lowercase whatever you type it seems. I cannot show it because I cant upload more pictures but it's super simple and well described.
One important thing to note is that you can put multiple things in every asset bundle. Like in the picture I decided to put the Monkey and the Hammer in the same asset bundle. Every tagged item in this fashion will go in the asset bundle of the same name.
You now then need to build your asset bundles everytime you want to test them as they are in the next build. It's not mandatory but if you really need to see some changes in the app, you must build.
On the subject of using these resources in-app.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using UnityEngine.Networking;
public class Your_Class : MonoBehaviour {
// Create AssetBundle variable to store the data of the bundles
AssetBundle weapons_bundle;
bool asset_bundles_loaded = false;
void Start () {
// Start the coroutine that will load all needed asset bundles
StartCoroutine(load_asset_bundles());
}
public void Update () {
// Wait for all asset bundles to be loaded
if (asset_bundles_loaded) {
// Once loaded use the assets in asset bundles
// To get the data in a bundle, you must do your_bundle.LoadAsset<type_of_your_asset>("name_of_the_asset_case_sensitive");
GameObject asset_loaded_prefab = (GameObject) weapons_bundle.LoadAsset<GameObject>("Hammer");
GameObject instance = (GameObject) GameObject.Instantiate(asset_loaded_prefab);
}
}
IEnumerator load_asset_bundles () {
// Reuse this line to load different asset bundles of different names
StartCoroutine(load_sub_asset_bundle("weapons"));
// The delay here has been added to make sure it will load.
// Im looking into a solution to make it as quick as possible
// but this is another problem and is not required here.
yield return new WaitForSeconds(2F);
asset_bundles_loaded = true;
yield return true;
}
IEnumerator load_sub_asset_bundle (string bundle_name) {
// This coroutine loads a single asset bundle at a time
string uri;
string path_to_use;
#if UNITY_ANDROID && !UNITY_EDITOR
// This is the path to require an asset bundle in Assets/StreamingAssets on Android
path_to_use = Path.Combine("jar:file://" + Application.dataPath + "!assets/", bundle_name);
uri = path_to_use;
#else
// This is the same path but for your computer to recognize
path_to_use = Application.path_to_use;
uri = path_to_use + "/" + bundle_name;
#endif
// Ask for the bundle
UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequestAssetBundle.GetAssetBundle(uri, 0);
yield return request.Send();
switch (bundle_name) {
case "weapons":
// Get the bundle data and store it in the AssetBundle variable created at the begining.
weapons_bundle = DownloadHandlerAssetBundle.GetContent(request);
break;
default:
break;
}
// Delay for now is just to make sure it loads properly before its use.
yield return new WaitForSeconds(1F);
}
}
It is important to note that the Assets, when loaded from an AssetBundle via script, require a type. Some types stay unchanged, like GameObject for example. But to load a text file you would instead need to write TextAsset as its type. I also think texture is the type for images. I didn't dig more on AssetBundle types, but this might be something to be careful about when doing this.
And now pals, as far as I know, at 2019-02-10 with Unity 2018.1, is how I finally completed this problem.
I hope this is the clearest I can do.
Answer by xxmariofer · Feb 09, 2019 at 09:52 PM
hello, check this answer
Okay, I searched and made modifications, but this allows me to know the files in assets after build, but I can load them as prefabs to use as gameobjects in the game.
hmmmm, thats true you can only read sprites audios etc... maybe using https://docs.unity3d.com/$$anonymous$$anual/AssetBundlesIntro.html
i have never used it but i know you can instantiate objects from there.
Answer by Bakrino · May 01, 2019 at 12:13 AM
Hi, After all create a folder in your assets and name it "Resources" into Resources create an other folder and name it what you want and then put your prefab that you want to instantiate..., the path would be like this Ex: Resources/myPrefabs/Brush.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paintable : MonoBehaviour {
public GameObject Brush;
void OnCollisionEnter(Collision collision){
if (collision.collider.CompareTag ("Player"))
{
Brush = GameObject.Instantiate(Resources.Load<GameObject>("myPrefabs/Brush") as GameObject);
}
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Q about Building my game for so many devices 2 Answers
Unity 2018.2 LWRP blackscreen on build(Android VR) 2 Answers
Unity IAP not working on android build 1 Answer
instantiates in the same place 2 Answers