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

时间:2018-04-26 05:04:17

标签: c# unity3d vuforia assetbundle

我使用Unity的地平面功能来放置一个3D动画模型。但是我现在正在使用AssetBundle功能下载这个3D模型,需要使用脚本将它放在地平面舞台上。

然而,当我将它部署到Android设备上时它不会显示...

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

我已添加脚本以将资产包下载到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);
    }
}

这是我的场景的截图: enter image description here

有关我做错的任何建议吗?感谢。

1 个答案:

答案 0 :(得分:1)

我注意到你AssetbundleDownloadingScript正在创建一个GameObject santaasset,但是你永远不会将对象分配给new或现有的GameObject,甚至Instantiating。您只需从Asset分配要加载的bundle即可。但是,该资产也从未被分配,因为它只是被加载到内存中,因此Unity可以识别它。这就是你所经历的,这就是你在游戏中看到对象的原因,但它不活跃,甚至很难被禁用。

要解决此问题,您必须分配您的GameObject或将其实例化为:
GameObject santaasset = Instantiate(bundle.LoadAsset("animation_keyframes_increase_v1", typeof(GameObject)) as GameObject);