AR开发实战Vuforia项目之房地产(AR与VR混合开发)

一、框架视图

二、关键代码

CameraMode

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


/// <summary>
/// 自动对焦功能
/// </summary>
public class CameraMode : MonoBehaviour
{


    void Start()
    {
        //一开始自动对焦
        //Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
        VuforiaARController.Instance.RegisterOnPauseCallback(OnPaused);
    }

    void Update()
    {
        //触碰的时候对焦
        //if (Input.GetMouseButtonUp(0))
        //{
        //    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        //    {
        //        Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        //    }
        //}

    }
    private void OnVuforiaStarted()
    {
        CameraDevice.Instance.SetFocusMode(
        CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }

    private void OnPaused(bool paused)
    {
        if (!paused)
        { // resumed
            // Set again autofocus mode when app is resumed
            CameraDevice.Instance.SetFocusMode(
            CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        }
    }

}

IntoOrOutside_Button

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
//引用场景管理器命名空间

public class IntoOrOutside_Button : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    //进入房间
    public void In_Button(){
        SceneManager.LoadScene ("IntoHouse");
    }


    //退出房间
    public void Out_Button()
    {
        SceneManager.LoadScene("Main");
    }


    public void Vr_Button() {
        SceneManager.LoadScene("vr1");
    }

}

Load_Mark

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Load_Mark : MonoBehaviour {

    public Texture[] Load_Textures;
    //申请贴图类型的数组储存每一张进度条图片

    public float Load_Speed = 0.2f;
    //圆形进度条每张图片切换的时间 进度条的读取速度

    public float Load_Time;
    //记录进度条所使用的时间

    public int Load_Count;
    //记录当前进度条该播放哪张图片

    public Texture Alpha;

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {

        if(Load_Count>16){
        //当图片超过16张时 
            SceneManager.LoadScene ("vr2");
            //跳转到场景二
        }

        Load_Time += Time.deltaTime;
        //每一帧增加 帧所使用的时间  即进度条经过的时间

        Load_Count = (int)Mathf.Floor (Load_Time / Load_Speed);
        //计算出当前应该播放哪张图片 用所经过的时间除以每张图片切换的速度
        //Mathf.Floor() 向下取正
        if (Load_Count<16)
        {
            gameObject.GetComponent<Renderer>().material.mainTexture = Load_Textures[Load_Count];
            //把当前所对应的进度条图片显示出来
        }




    }

    // OnDisable函数 在该脚本被取消的时候会执行一次
    void OnDisable(){
        Load_Count = 0;
        Load_Time = 0;
        gameObject.GetComponent<Renderer> ().material.mainTexture = Alpha;  
    }


}

MobileGyro

using UnityEngine;
using System.Collections;
//摄像机  陀螺仪转动
public class MobileGyro : MonoBehaviour
{
    Gyroscope gyro;
    Quaternion quatMult;
    Quaternion quatMap;
    GameObject player;
    GameObject camParent;
    void Awake()
    {
        player = GameObject.Find("Player");
        // find the current parent of the camera's transform
        Transform currentParent = transform.parent;
        // instantiate a new transform
        camParent = new GameObject("camParent");
        // match the transform to the camera position
        camParent.transform.position = transform.position;
        // make the new transform the parent of the camera transform
        transform.parent = camParent.transform;
        // make the original parent the grandparent of the camera transform
        //camParent.transform.parent = currentParent;
        // instantiate a new transform
        GameObject camGrandparent = new GameObject("camGrandParent");
        // match the transform to the camera position
        camGrandparent.transform.position = transform.position;
        // make the new transform the parent of the camera transform
        camParent.transform.parent = camGrandparent.transform;
        // make the original parent the grandparent of the camera transform
        camGrandparent.transform.parent = currentParent;

        Input.gyro.enabled=true;
        gyro = Input.gyro;

        gyro.enabled = true;
        //camParent.transform.eulerAngles = new Vector3(90, 0, 0);
        camParent.transform.eulerAngles = new Vector3(90,-135, 0);
       //camParent.transform.eulerAngles = new Vector3(90, 0, 180);
        //quatMult = new Quaternion(0, 0, 1, 0);
        quatMult = new Quaternion(0, 0, 1, 0);
    }

    void Update()
    {

        quatMap = new Quaternion(gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
        Quaternion qt=quatMap * quatMult;

        transform.localRotation =qt;

    }

}

Rotate

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

public class Rotate : MonoBehaviour {

    //记录上一帧手指的位置;
    private Vector3 PreviousPosition;

    //记录手指的偏移值;
    private Vector3 Offset;

    void Start () {
        
    }
    

    void Update () {
        //判断是否触屏;
        if (Input.GetMouseButtonDown(0))
        {
            //记录手指的偏移值
            Offset = Input.mousePosition - PreviousPosition;
            //记录上一帧的手指的位置;
            PreviousPosition = Input.mousePosition;
            //通过手指的偏移值来控制物体的旋转;
            transform.Rotate(Vector3.Cross(Offset*0.01f,Vector3.up).normalized,Offset.magnitude,Space.Self);
        }
        
    }
}

SRay

using UnityEngine;
using System.Collections;

public class SRay : MonoBehaviour {

    public RaycastHit HitInfo;
    public Transform HitPoint;
    public GameObject LoadPlane;

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
        SendRay ();

        Debug.Log (HitInfo.collider.gameObject.name);
    }


    void SendRay() {
        
        if (Physics.Raycast (HitPoint.position, HitPoint.forward, out HitInfo)) {
            

            if (HitInfo.collider.gameObject.tag == "A") {
                Debug.Log("111");
                LoadPlane.GetComponent<Load_Mark> ().enabled = true;
                //如果射线撞击的位置标签是否为“face” 则

            }else{   
                Debug.Log("222");
                LoadPlane.GetComponent<Load_Mark> ().enabled = false;

            }



        }
    }
}

SX

using UnityEngine;
using System.Collections;

public class SX : MonoBehaviour {

    public GameObject Load_Obj;
    //储存附有进度条脚本的物体
    private RaycastHit HitIfo;
    //申请变量记录碰撞的信息


    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        SendRay ();
    }

    //发射射线的函数
    public void SendRay(){
        if (Physics.Raycast (transform.position, transform.forward, out HitIfo)) {
            //发射射线固定格式Physics.Raycast (发射射线的位置, 发射射线的方向, 射线的返回信息)

            if(HitIfo.collider.tag=="Door"){
            //如果碰到的碰撞器的标签是Door
                Load_Obj.GetComponent<Load_Mark>().enabled=true;
                //启用进度条脚本
            }else{
                Load_Obj.GetComponent<Load_Mark>().enabled=false;
                //禁用进度条脚本
            }
        }
    }
}

TransformChange

using UnityEngine;
using System.Collections;


public class TransformChange : MonoBehaviour {

    public Transform EndPosition;
    //储存第一人称控制器摄像机的位置

    public float CostTime=0.2f;
    //摄像机移动所经过的时间

    public GameObject FPSCtrl;
    //储存第一人称控制器

    public GameObject UICtrl;
    //储存虚拟摇杆


    //退出房间
    public GameObject Close_Btn;

    //切换VR场景
    public GameObject Vr_Btn;

    // Use this for initialization
    void Start () {
        StartCoroutine(CameraMove ());
        //摄像机移动
        StartCoroutine (Show());
        //延迟执行激活 第一人称控制器与虚拟摇杆的函数

      // Vector3  qua = EndPosition.gameObject.transform.eulerAngles; 

    }
    
    // Update is called once per frame
    void Update () {
    
    }

    IEnumerator CameraMove(){
        yield return new WaitForSeconds (0.5f);
        iTween.MoveTo (gameObject,EndPosition.position,CostTime); //iTween 调不出来的话吧编辑器关闭再打开
        //从摄像机起始位置 经过CostTime这段时间  移动到EndPosition.position的位置
        iTween.RotateTo(gameObject,EndPosition.rotation.eulerAngles,CostTime);
        //从摄像机起始位置 经过CostTime这段时间  移动到EndPosition.position的角度
       
        
    }

    IEnumerator Show(){
        yield return new WaitForSeconds (4.7f);
        //延迟函数
        FPSCtrl.SetActive(true);
        //激活第一人称控制器
        UICtrl.SetActive(true);
        //激活虚拟摇杆

        //激活关闭按钮
        Close_Btn.SetActive(true);

        //激活Vr场景按钮
        Vr_Btn.SetActive(true);
    }

    

}

UI_Manager

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

public class UI_Manager : MonoBehaviour {

    //储存信息面板
    public GameObject Info_Panel;
    //二级面板引用;
    public GameObject Inside_Panel;


    //贮存外部模型;
    public GameObject Building;

    //储存户型1;
    public GameObject Plan1;

    //储存户型2;
    public GameObject Plan2;

    //储存户型3;
    public GameObject Plan3;


    //记录二级菜单状态;
    // public int Inside_Ctrl = 0;
    private bool isShow = true;



    //记录面板的信息
    private bool Info_P_State = true;


    //贮存案例的面板
    public GameObject Example_Panel;

    //储存案例名称的Text组件
    public Text Example_Content_Text;

    //储存显示案例图片的Image组件
    public Image Example_Content_Image;

    //申请整型int变量来作为案例显示的顺序
    public int Example_Count = 0;

    //储存案例图片的数组;
    public Sprite[] SpriteChange;

    //申请字符串来储存案例的名称
    public string[] NameChange;

    //记录面板的状态
    public bool Example_P_State = false;


    void Start() {

    }


    void Update() {

    }

    /// <summary>
    /// 显示二级面板
    /// </summary>
    public void Inside_Button() {
        Example_Panel.SetActive(false);
        Example_P_State = false;

        if (isShow)
        {
            Inside_Panel.SetActive(true);

        }
        else
        {
            Inside_Panel.SetActive(false);

        }

        isShow = !isShow;

    }

    //方案1按钮
    public void Plan1_Button() {
        //隐藏二级面板
        Inside_Panel.SetActive(false);
        Plan1.SetActive(true);
        Plan2.SetActive(false);
        Plan3.SetActive(false);
        Building.SetActive(false);
        isShow = !isShow;
    }

    //方案2按钮
    public void Plan2_Button()
    {
        //隐藏二级面板
        Inside_Panel.SetActive(false);
        Plan1.SetActive(false);
        Plan2.SetActive(true);
        Plan3.SetActive(false);
        Building.SetActive(false);
        isShow = !isShow;
    }

    //方案3按钮
    public void Plan3_Button()
    {
        //隐藏二级面板
        Inside_Panel.SetActive(false);
        Plan1.SetActive(false);
        Plan2.SetActive(false);
        Plan3.SetActive(true);
        Building.SetActive(false);
        isShow = !isShow;
    }


    //外部效果按钮
    public void Outside_Button() {
        Plan1.SetActive(false);
        Plan2.SetActive(false);
        Plan3.SetActive(false);
        Building.SetActive(true);
    }


    //关于我们的按钮
    public void Info_Button() {

        //让成功案例面板处于隐藏
        Example_Panel.SetActive(false);
        //成功案例面板状态设置为false
        Example_P_State = false;

        if (Info_P_State)
        {
            Info_Panel.SetActive(true);

        }
        else
        {
            Info_Panel.SetActive(false);
        }
        //没点击一次改变一次值;
        Info_P_State = !Info_P_State;
    }

    //信息面板中的关闭按钮;
    public void Info_Close_Button() {
        Info_Panel.SetActive(false);
        Info_P_State = true;
    }

    //成功案例的按钮
    public void Example_Button() {
        //关闭信息面板
        Info_Close_Button();
        if (Example_P_State==false)
        {
            Example_Panel.SetActive(true);
            Example_P_State = true;
        }
        else
        {
            Example_Panel.SetActive(false);
            Example_P_State = false;
        }

    }



    //成功案例面板中的关闭按钮;
    public void Example_Close_Button() {
        Example_Panel.SetActive(false);
    }

    /*

    //成功案例面板中的向右按钮;
    public void Right_Button() {
        //第一种判断方法;
        if (Example_Count == 0)
        {
            Example_Content_Image.sprite = SpriteChange[1];
            Example_Content_Text.text = "金字塔";
            Example_Count = 1;
        }
        else if (Example_Count == 1)
        {
            Example_Content_Image.sprite = SpriteChange[2];
            Example_Content_Text.text = "吴哥窟";
            Example_Count = 2;
        }
        else if (Example_Count == 2)
        {
            Example_Content_Image.sprite = SpriteChange[0];
            Example_Content_Text.text = "长城";
            Example_Count = 0;
        }

        //第二种判断方法;
        //switch (Example_Count)
        //{
        //    case 0:
        //        Example_Content_Image.sprite = SpriteChange[++Example_Count];
        //        Example_Content_Text.text = "金字塔";
        //        break;
        //    case 1:
        //        Example_Content_Image.sprite = SpriteChange[++Example_Count];
        //        Example_Content_Text.text = "吴哥窟";
        //        break;
        //    case 2:
        //        Example_Content_Image.sprite = SpriteChange[Example_Count=0];
        //        Example_Content_Text.text = "长城";
        //        break;
        //    default:
        //        break;
        //}

    }

    //成功案例面板中的向左按钮;
    public void Left_Button()
    {
        //switch (Example_Count)
        //{
        //    case 0:
        //        Example_Content_Image.sprite = SpriteChange[SpriteChange.Length-(++Example_Count)];
        //        Example_Content_Text.text = "吴哥窟";
        //        break;
        //    case 1:
        //        Example_Content_Image.sprite = SpriteChange[SpriteChange.Length-(++Example_Count)];
        //        Example_Content_Text.text = "金字塔";
        //        break;
        //    case 2:
        //        Example_Content_Image.sprite = SpriteChange[Example_Count = 0];
        //        Example_Content_Text.text = "长城";
        //        break;
        //    default:
        //        break;
        //}


        if (Example_Count == 0)
        {
            Example_Content_Image.sprite = SpriteChange[2];
            Example_Content_Text.text = "吴哥窟";
            Example_Count = 2;
        }
        else if (Example_Count == 2)
        {
            Example_Content_Image.sprite = SpriteChange[1];
            Example_Content_Text.text = "金字塔";
            Example_Count = 1;
        }
        else if (Example_Count == 1)
        {
            Example_Content_Image.sprite = SpriteChange[0];
            Example_Content_Text.text = "长城";
            Example_Count = 0;
        }
    }
    */


    //通过序号加减来切换案例图片和名称
    //右键
    public void Right_Button() {
        //向右最后一张图片的时候跳回第一张
        if (Example_Count== SpriteChange.Length - 1)
        {
            Example_Count =0;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }
        else
        {
            Example_Count++;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }
    }

    //左键
    public void Left_Button() {
        if (Example_Count == 0)
        {
            Example_Count = SpriteChange.Length - 1;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }
        else
        {
            Example_Count--;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }

    }






    //AR看房
    public void ARScene_Button()
    {
        SceneManager.LoadScene("AScene");
    }

    //AR视频
    public void ARVideo_Button()
    {

        SceneManager.LoadScene("Movie");
    }
}

VedioCtrl

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

public class VedioCtrl : MonoBehaviour {

    public MediaPlayerCtrl scrMedia;

    public bool m_bFinish = false;
    // Use this for initialization
    void Start()
    {
        scrMedia.OnEnd += OnEnd;

    }


    // Update is called once per frame
    void Update()
    {



    }

    //播放视频1
    public void Vedio_01() {
        scrMedia.Load("k3.mp4");
        m_bFinish = false;
        //开启协程;
        //StartCoroutine("PlayVedio");
        StartCoroutine(PlayVedio());
    }


    public void Vedio_02()
    {
        scrMedia.Load("Demo.mp4");
        m_bFinish = false;
        //开启协程;
        //StartCoroutine("PlayVedio");
        StartCoroutine(PlayVedio());
    }

    //停止加载
    public void UnLoad(){
        scrMedia.UnLoad();

    }

    //协程函数
    IEnumerator PlayVedio() {
        
        yield return new WaitForSeconds(0.3f);
        scrMedia.Play();
        m_bFinish = false;
    }


   /* void OnGUI()
    {


        if (GUI.Button(new Rect(50, 50, 100, 100), "Load"))
        {
            scrMedia.Load("EasyMovieTexture.mp4");
            m_bFinish = false;
        }

        if (GUI.Button(new Rect(50, 200, 100, 100), "Play"))
        {
            scrMedia.Play();
            m_bFinish = false;
        }

        if (GUI.Button(new Rect(50, 350, 100, 100), "stop"))
        {
            scrMedia.Stop();
        }

        if (GUI.Button(new Rect(50, 500, 100, 100), "pause"))
        {
            scrMedia.Pause();
        }

        if (GUI.Button(new Rect(50, 650, 100, 100), "Unload"))
        {
            scrMedia.UnLoad();
        }

        if (GUI.Button(new Rect(50, 800, 100, 100), " " + m_bFinish))
        {

        }

        if (GUI.Button(new Rect(200, 50, 100, 100), "SeekTo"))
        {
            scrMedia.SeekTo(10000);
        }


        if (scrMedia.GetCurrentState() == MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING)
        {
            if (GUI.Button(new Rect(200, 200, 100, 100), scrMedia.GetSeekPosition().ToString()))
            {

            }

            if (GUI.Button(new Rect(200, 350, 100, 100), scrMedia.GetDuration().ToString()))
            {

            }

            if (GUI.Button(new Rect(200, 450, 100, 100), scrMedia.GetVideoWidth().ToString()))
            {

            }

            if (GUI.Button(new Rect(200, 550, 100, 100), scrMedia.GetVideoHeight().ToString()))
            {

            }
        }

        if (GUI.Button(new Rect(200, 650, 100, 100), scrMedia.GetCurrentSeekPercent().ToString()))
        {

        }


    }

    */

    void OnEnd()
    {
        m_bFinish = true;
    }



    //退出房间
    public void Out_Button()
    {
        SceneManager.LoadScene("Main");
    }

}

三、效果展示

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,542评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,596评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,021评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,682评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,792评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,985评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,107评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,845评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,299评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,612评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,747评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,441评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,072评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,828评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,069评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,545评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,658评论 2 350

推荐阅读更多精彩内容