Search code examples
c#unity-game-enginevuforiaassetbundle

How To Dynamically Add a 3D Model to a Ground Plane


I was using Unity's Ground Plane feature to place a 3d animated model. However I am now downloading this 3d model using AssetBundle feature and need to place it under on the Ground Plane Stage using a script.

However it doesn't display, when I deploy it onto an android device...

I am using a Xiaomi Redmi 3s which has support for GroundPlane detection.

I have added the script to download the asset bundle onto the Plane Finder

AssetbundleDownloadingScript:

public class AssetLoader : MonoBehaviour
{

    public static AssetLoader Instance;

    public string url = "myurl";
    public int version = 1;
    public string AssetName;

    //public Text infoText;
    public string infoText = "";

    AssetBundle bundle;


    void Awake()
    {
        Instance = this;

        DownloadAsset();
    }

    void OnDisable()
    {
        //AssetBundleManager.Unload(url, version);
    }


    public void DownloadAsset()
    {
        // bundle = AssetBundleManager.getAssetBundle (url, version);
        //   Debug.Log(bundle);

        if (!bundle)
            StartCoroutine(DownloadAssetBundle());
    }

    IEnumerator DownloadAssetBundle()
    {
        yield return StartCoroutine(AssetBundleManager.downloadAssetBundle(url, version));

        bundle = AssetBundleManager.getAssetBundle(url, version);

        if (bundle != null)
            infoText = "Download Success.....";
        else
            infoText = "Download error please retry";

        GameObject santaasset = bundle.LoadAsset("animation_keyframes_increase_v1", typeof(GameObject)) as GameObject;

        //here script attached to plane finder,get 1st child of planefinder
        var child = gameObject.transform.GetChild(0);

        if (santaasset != null)
        {
            santaasset.transform.transform.Rotate(new Vector3(0, 180, 0));
            santaasset.transform.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
            santaasset.transform.transform.SetParent(child.transform, false);
        }
        bundle.Unload(false);
    }


    public void SetInfoText(string text)
    {
        //infoText.text = text;
    }

    void OnGUI()
    {
        GUILayout.Label("Dummy Label:" + infoText);
    }
}

Here's a screenshot of my scene: enter image description here

Any suggestions on what I am doing wrong ? Thanks.


Solution

  • I noticed in your AssetbundleDownloadingScript you are creating a GameObject santaasset, however you are never assigning the object to a new or an existing GameObject, or even Instantiating it. You are instead assigning the Asset that you are loading from your bundle. However that asset is also never assigned, since it was just loaded into memory, so that Unity could recognize it. This is what you are experiencing, and this is why you see the object in game, but its not active, even tough its not disabled.

    To fix this issue, you must assign your GameObject or Instantiate it like so:
    GameObject santaasset = Instantiate(bundle.LoadAsset("animation_keyframes_increase_v1", typeof(GameObject)) as GameObject);