【问题标题】:How To Dynamically Add a 3D Model to a Ground Plane如何将 3D 模型动态添加到地平面
【发布时间】:2018-04-26 05:04:16
【问题描述】:

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

但是当我将它部署到安卓设备上时它不显示...

我使用的是支持 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);
    }
}

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

关于我做错了什么有什么建议吗?谢谢。

【问题讨论】:

  • 您是否在Editor 中测试过您的assetbundle 是否已正确下载,并且您从中获取模型?
  • 在编辑器中,当我按下播放时,资产包被下载,并作为地平面阶段的子节点附加到 [link] (imgur.com/a/FDuAuCS)
  • 如果您有网络摄像头,我建议您也可以在Editor 中测试地平面。由于您的手机可能一切正常,唯一的问题是您的模型可能会偏移或未正确缩放。 library.vuforia.com/articles/Solution/ground-plane-guide.html - 本文第 11 点
  • 尝试设置 santaasset.transform.transform.SetParent(child.transform, false);首先,然后进行缩放和旋转操作。如果枢轴不在中心,则在添加为子元素之前进行向量和四元数放置可能会使它们放置在其他位置。如果您的飞机在现实世界中改变了位置,它不会更新游戏中的位置,因为您的相关设置会在您稍后附加时丢失。您可以尝试让我们知道这是否有效。
  • @Hristo 好的,我会用网络摄像头试试,让你知道。我使用 adb 检查 debug.log 打印的 logcat 消息,这是我得到的输出.. [link] (imgur.com/a/xPRGJ6N) Hit Test is invalid or anchor stage not set

标签: c# unity3d vuforia assetbundle


【解决方案1】:

我注意到在您的AssetbundleDownloadingScript 中您正在创建GameObject santaasset,但是您从未将对象分配给new 或现有的游戏对象,甚至Instantiating 它。相反,您正在分配从bundle 加载的Asset。但是,该资产也从未分配过,因为它只是加载到内存中,以便 Unity 可以识别它。这就是您正在经历的,这就是为什么您在游戏中看到该对象,但它没有激活,甚至没有禁用它。

要解决此问题,您必须像这样分配您的游戏对象或实例化它:
GameObject santaasset = Instantiate(bundle.LoadAsset("animation_keyframes_increase_v1", typeof(GameObject)) as GameObject);

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2016-09-11
    • 2020-01-26
    • 2013-09-25
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多