【Vuforia官方文档】在Unity中动态改变模型的显示

2 篇文章 0 订阅

关于ImageTarget的使用不再赘述,现在假设有个按钮,按下之后,imagetarget下的球体便变为正方体。

添加如下脚本,挂在ARCamera上。

using UnityEngine;
using Vuforia;
using System.Collections;
public class ModelSwapper : MonoBehaviour {
    public TrackableBehaviour theTrackable;
    private bool mSwapModel = false;
    // Use this for initialization
    void Start () {
        if (theTrackable == null)
        {
            Debug.Log ("Warning: Trackable not set !!");
        }
    }
    // Update is called once per frame
    void Update () {
        if (mSwapModel && theTrackable != null) {
            SwapModel();
            mSwapModel = false;
        }
    }
    void OnGUI() {
        if (GUI.Button (new Rect(50,50,120,40), "Swap Model")) {
            mSwapModel = true;
        }
    }
    private void SwapModel() {
        GameObject trackableGameObject = theTrackable.gameObject;
        //把原来显示的模型隐藏掉。
        for (int i = 0; i < trackableGameObject.transform.GetChildCount(); i++)
        {
            Transform child = trackableGameObject.transform.GetChild(i);
            child.gameObject.active = false;
        }
        // 创建新物体cube
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        // 把cube的parent设置为trackable gameObject
        cube.transform.parent = theTrackable.transform;
        // 调整它的大小,使它能更好地适应target
        cube.transform.localPosition = new Vector3(0,0.2f,0);
        cube.transform.localRotation = Quaternion.identity;
        cube.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        // 保证它是激活的
        cube.active = true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值