AR/VR/MR

AR开发实战Vuforia项目之PokemonGo(口袋精灵)

2017-06-19  本文已影响195人  TonyWan_AR

一、框架视图


二、关键代码

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<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            //重置
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
        // Target.GetComponent<MeshRenderer>().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<MeshRenderer>().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<Button>();
        //给按钮添加监听器,点击 按钮的时候 执行 切换场景的方法
        btn.onClick.AddListener(TaskOnClick);

    }


    void TaskOnClick() {
        //加载场景一
        //SceneManager.LoadScene("Scene1");
        SceneManager.LoadScene(1);
    }
}

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<Button>();
        //给按钮添加监听器,点击 按钮的时候 执行 切换场景的方法
        btn.onClick.AddListener(TaskOnClick);

    }


    void TaskOnClick()
    {
        //加载场景二
        //SceneManager.LoadScene("Scene2");
        SceneManager.LoadScene(2);
    }
}

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<Button>();
        //给按钮添加监听器,点击 按钮的时候 执行 切换场景的方法
        btn.onClick.AddListener(TaskOnClick);

    }


    void TaskOnClick()
    {
        //加载开始场景
        //SceneManager.LoadScene("Menu");
        SceneManager.LoadScene(0);
    }
}

三、运行效果

上一篇 下一篇

猜你喜欢

热点阅读