AR开发实战Vuforia项目之PokemonGo(一、有卡脱卡识别)

一、框架视图

AR开发实战Vuforia项目之PokemonGo(一、有卡脱卡识别)_第1张图片

AR开发实战Vuforia项目之PokemonGo(一、有卡脱卡识别)_第2张图片
AR开发实战Vuforia项目之PokemonGo(一、有卡脱卡识别)_第3张图片

二、关键代码

PageNotFound

using UnityEngine;
using Vuforia;
public class pageNotFound : MonoBehaviour, ITrackableEventHandler
{
    //引用
    private TrackableBehaviour mTrackableBehaviour;
    public Transform Target;//识别物
    Vector3 imgPos = new Vector3(0, 0.2f, -0.2f);//识别图上的位置
    Vector3 camPos = new Vector3(0, -2.5f, 10f);//脱卡后在屏幕中的位置
                                              //这俩值,具体多少得自己调,模型尺寸、重心不同

    bool isFirstTime = true; //第一查找

   

    void Start()
    {
        mTrackableBehaviour = GetComponent();
        if (mTrackableBehaviour)
        {
            //重置
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
        // Target.GetComponent().enabled = false;//起始时不显示
        Target.gameObject.SetActive(false);

    }


    //接口实现 公开的
    public void OnTrackableStateChanged(
            TrackableBehaviour.Status previousStatus,
            TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
                newStatus == TrackableBehaviour.Status.TRACKED ||
                newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            //视野内发现识别图时
            //Target.GetComponent().enabled = true;
            Target.gameObject.SetActive(true);
            Target.parent = this.transform;
            Target.localPosition = imgPos;
            // Target.localRotation = Quaternion.identity;
            Target.localRotation = Quaternion.Euler(90, 0, 0);
            isFirstTime = false;
        }
        else
        {
            //视野内没有识别图时,这里我是把位置和旋转都归零了,如果不做处理,可以
            if (!isFirstTime)
            {
               // Target.parent = Camera.main.transform;
                Target.parent = GameObject.FindGameObjectWithTag("MainCamera").transform;
                Target.localPosition = camPos;
                //  Target.localRotation = Quaternion.identity;
                Target.localRotation = Quaternion.Euler(-30, 0, 0);
                 Debug.Log("3333333333333");
            }
        }
    }
}

AccelormeterControl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AccelormeterControl : MonoBehaviour {

    //定义速度参数
    float speed = 200f;

    //屏幕是否触摸
    static bool isTouched;

    //游戏物体的引用
    //public GameObject fireDrogon;
    
    void Start () {
        isTouched = false;
        //开始隐藏
        //fireDrogon.SetActive(false);
       // this.gameObject.SetActive(false);
    }
    
    
    void Update () {
        
        //如果屏幕的触摸的次数大于等于1次 设置为true;
        //if (Input.touchCount>=1)
        //{
        //    //点击后显示
        //    //fireDrogon.SetActive(true);
        //    this.gameObject.SetActive(true);
            

        //}
         if (Input.touchCount >= 1)
        {
            isTouched = true;
        }

        if (isTouched)
        {
            //根据重力加速度的位移
            Vector3 mMovement = new Vector3(Input.acceleration.x * speed * Time.deltaTime,
                                            Input.acceleration.y * speed * Time.deltaTime);
            //游戏对象本身的移动方式;
            transform.Translate(mMovement);
        }
    

    }
}

Btn1_Click

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Btn1_Click : MonoBehaviour {
    
    //公开的持有引用
    public Button mButton;

    void Start () {
        
        //获取按钮一
        Button btn = mButton.GetComponent

Btn2_Click

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Btn2_Click : MonoBehaviour
{

    //公开的持有引用
    public Button mButton;

    void Start()
    {

        //获取按钮二
        Button btn = mButton.GetComponent

ButtonBack

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ButtonBack : MonoBehaviour {

    //公开的持有引用
    public Button mButton;

    void Start()
    {

        //获取回退按钮
        Button btn = mButton.GetComponent

三、运行效果

你可能感兴趣的:(AR开发实战Vuforia项目之PokemonGo(一、有卡脱卡识别))