如何将3D模型动态添加到地平面

我正在使用Unity的地平面功能放置3D动画模型。但是,我现在正在使用AssetBundle功能下载此3d模型,并需要使用脚本将其放置在Ground Plane Stage上。


但是,当我将其部署到android设备上时,它不会显示...


我正在使用支持GroundPlane检测的Xiaomi Redmi 3s。


我已添加脚本以将资产捆绑包下载到“平面查找器”中


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

    }

}

这是我的场景的屏幕截图:

http://img.mukewang.com/60a0caa80001fb2d13650526.jpg

关于我在做什么错的任何建议吗?谢谢。

慕娘9325324
浏览 128回答 1
1回答

慕森卡

我在您发现AssetbundleDownloadingScript您正在创建中时注意到GameObject santaasset,但是您绝不会将对象分配给new或现有的GameObject甚至是Instantiating它。您要分配的Asset是您正在从中加载的bundle。但是,该资产也从未分配过,因为它只是被加载到内存中,因此Unity可以识别它。这就是您所经历的,这就是为什么您在游戏中看到该对象,但该对象未激活,甚至很难禁用的原因。若要解决此问题,您必须像这样分配您的GameObject或实例化它:GameObject santaasset = Instantiate(bundle.LoadAsset("animation_keyframes_increase_v1", typeof(GameObject)) as GameObject);
打开App,查看更多内容
随时随地看视频慕课网APP