2

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.

Nathaniel Nunes
  • 301
  • 1
  • 15
  • Have you tested in `Editor` to see if your `assetbundle` is downloaded properly, and you are getting the model from it? – Hristo Apr 26 '18 at 05:38
  • In the Editor, when I pressed play the asset bundle got downloaded, and got attached as a child of ground plane stage [link] (https://imgur.com/a/FDuAuCS) – Nathaniel Nunes Apr 26 '18 at 05:42
  • If you have a webcam, I suggest testing the ground plane in `Editor` as well. Since it might be that your phone is doing everything correct, only problem is your model could be offset or not scaled correctly. https://library.vuforia.com/articles/Solution/ground-plane-guide.html - Point 11 from this article – Hristo Apr 26 '18 at 05:52
  • try and set santaasset.transform.transform.SetParent(child.transform, false); first and then do the scale and rotation manipulation. Doing Vector and quaternion placement before adding as child might make them get placed at some other position if pivot is not at center. If your plane changes position in real world it will not update the position in game as your relative settings are lost as you attached it later. You can try and let us know if this worked. – killer_mech Apr 26 '18 at 05:55
  • @Hristo okay I will try it with webcam and let u know. I used adb to check the logcat messages printed by debug.log, this was the output I was getting.. [link] (https://imgur.com/a/xPRGJ6N) Hit Test is invalid or anchor stage not set – Nathaniel Nunes Apr 26 '18 at 06:18
  • @killer_mech This did not work ,I'll try with Webcam also and check – Nathaniel Nunes Apr 26 '18 at 06:19
  • @killer_mech I set the parent initially as suggested and also increased the scale santaasset.transform.transform.SetParent(child.transform, false); santaasset.transform.transform.Rotate(new Vector3(0, 180, 0)); santaasset.transform.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f); – Nathaniel Nunes Apr 26 '18 at 06:46
  • @Hristo I tried with a webcam but it didnt show the 3d model. [link] (https://i.imgur.com/ihmatTW.png) . In my script which downloads AssetBundle I also checked whether 3d image is child of ground plane stage //here script attached to ar camera,so gameobject is ar camera var child = gameObject.transform.GetChild(0); if (santaasset.transform.transform.IsChildOf(child.transform)) Debug.Log("Its a child of parent"); – Nathaniel Nunes Apr 26 '18 at 07:30
  • In the picture you provided, the `GroundPlaneStage` is not active, meaning that your webcam does not see the image, or Its somehow disabled from Vuforia's settings. When the `GroundPlaneStage` is active in the Hierarchy, that's a sign that you are simulating `Markerless` like on a mobile. – Hristo Apr 26 '18 at 11:08
  • @Hristo I was able to set the GroundPlaneStage to Active, but the children of GroundPlaneStage are still Inactive [link] (https://i.imgur.com/lyl4Fni.png). I used a foreach loop to get all children gameobjects of groundplanestage and set them to Active [link] (https://i.imgur.com/WzoWlHn.png) – Nathaniel Nunes Apr 26 '18 at 13:03
  • They are Inactive since you hard-coded your `GroundPlaneStage` to be enabled when the bundle is loaded. What should happen, is the camera should recognize your ground plane photo and immediately enable the `GroundPlaneStage` with all of its children. – Hristo Apr 26 '18 at 13:06
  • Oh and by the way, your children are inactive, since you are using `GetComponentInChildren<>()` - well that actually gets only active components. you should instead have used `GetComponentInChildren<>(true)`- includes inactive. – Hristo Apr 26 '18 at 13:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169930/discussion-between-nathan-n-and-hristo). – Nathaniel Nunes Apr 27 '18 at 09:09
  • @NathanN See discussion – Hristo Apr 27 '18 at 13:23

1 Answers1

1

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);

Hristo
  • 1,805
  • 12
  • 21