1、https://developer.vuforia.com注册账号。
2、分别添加License Manager(连接钥匙),Target Manager(图片后台下载)。
3、下载vuforia-unity-6-2-6.unitypackage (46.20 MB),下载Target Manager里面上传的图片。
4、打开Unity 加载第三部的两个包。
5、如下图unity组件添加
6、钥匙添加后此时这里就可以选择所添加的图片了 如图标注出,下拉选择。背景图片变成选择的图片;然后就可以吧想要展示出来的模型添加成ImageTarget的子物体
7、此时我们就可以实现AR功能。这里我加了两个按钮 实现动画的切换跟返回(附带Button跟模型同时出现隐藏)
8、需要添加的代码如下
using UnityEngine; using System.Collections; using UnityEngine.UI; using Vuforia; using System; public class ChontrolScripts : MonoBehaviour { private Animator ani; private AnimatorStateInfo current; private AnimatorStateInfo last; public GameObject canvas; public DefaultTrackableEventHandler DTEH; // Use this for initialization void Start () { ani = GetComponent<Animator>(); current = ani.GetCurrentAnimatorStateInfo(0); last = current; DTEH.found += canvansShow; DTEH.last += cannasHide; } private void cannasHide() { if (canvas.activeSelf) { canvas.SetActive(false); } } private void canvansShow() { if (!canvas.activeSelf) { canvas.SetActive(true); } } // Update is called once per frame void Update () { if (ani.GetBool("Next")) { current = ani.GetCurrentAnimatorStateInfo(0); if (current.shortNameHash!=last.shortNameHash) { ani.SetBool("Next", false); current = last; } } if (ani.GetBool("Back")) { current = ani.GetCurrentAnimatorStateInfo(0); if (current.shortNameHash != last.shortNameHash) { ani.SetBool("Back", false); current = last; } } } public void OnButtonClickNext() { ani.SetBool("Next",true); } public void OnButtonClickBack() { ani.SetBool("Back", true); } }
/*============================================================================== Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved. Confidential and Proprietary - Protected under copyright and other laws. ==============================================================================*/ using UnityEngine; namespace Vuforia { /// <summary> /// A custom handler that implements the ITrackableEventHandler interface. /// </summary> public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler { #region PRIVATE_MEMBER_VARIABLES private TrackableBehaviour mTrackableBehaviour; #endregion // PRIVATE_MEMBER_VARIABLES public delegate void FoundOrLastEvent(); public FoundOrLastEvent found; public FoundOrLastEvent last; #region UNTIY_MONOBEHAVIOUR_METHODS void Start() { mTrackableBehaviour = GetComponent<TrackableBehaviour>(); if (mTrackableBehaviour) { mTrackableBehaviour.RegisterTrackableEventHandler(this); } } #endregion // UNTIY_MONOBEHAVIOUR_METHODS #region PUBLIC_METHODS /// <summary> /// Implementation of the ITrackableEventHandler function called when the /// tracking state changes. /// </summary> public void OnTrackableStateChanged( TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) { if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) { OnTrackingFound(); } else { OnTrackingLost(); } } #endregion // PUBLIC_METHODS #region PRIVATE_METHODS private void OnTrackingFound() { Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true); Collider[] colliderComponents = GetComponentsInChildren<Collider>(true); // Enable rendering: foreach (Renderer component in rendererComponents) { component.enabled = true; } // Enable colliders: foreach (Collider component in colliderComponents) { component.enabled = true; } if (found != null) { found(); } Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found"); } private void OnTrackingLost() { Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true); Collider[] colliderComponents = GetComponentsInChildren<Collider>(true); // Disable rendering: foreach (Renderer component in rendererComponents) { component.enabled = false; } // Disable colliders: foreach (Collider component in colliderComponents) { component.enabled = false; } if (last != null) { last(); } Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost"); } #endregion // PRIVATE_METHODS } }