Unity-AR基础

步骤:

1.访问摄像头,获取摄像头拍到内容,在游戏视图中进行全屏显示;
2.访问陀螺仪,通过陀螺仪控制场景中的摄像机的旋转;
3.通过touch触摸事件,控制场景中游戏对象的旋转,放大,缩小
4.UGUI
5.AssetBundle资源打包,加载资源
6.家具展示/玩具(玩具车,布娃娃)展示/动漫人物展示/

测试方法:

1.手机安装unity remote5.0(ios需在电脑上安装itunes)

2.通过数据线连接电脑。
unity:->项目设置project setting->Editor->device->手机系统

3.打开手机remote
运行unity项目

访问摄像头:

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

public class WebCamera : MonoBehaviour {
    //public MeshRenderer panel;
    public CanvasRenderer render;
	// Use this for initialization
	void Start () {
       
        StartCoroutine(this.WebCam());
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    IEnumerator WebCam() {
        //请求访问摄像头
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        //判断是否请求成功
        if (Application.HasUserAuthorization(UserAuthorization.WebCam)) {
            //获取摄像头设备
            WebCamDevice[] arr = WebCamTexture.devices;
            //设备的名字
            string deviceName = arr[0].name;
            //创建摄像头纹理,记录摄像头拍摄到的内容
            WebCamTexture webTexture = new WebCamTexture(deviceName,Screen.width,Screen.height,30);
           

            //把纹理应用到Panel对象的材质上
            //panel.material.mainTexture = webTexture;
            render.SetTexture(webTexture);
            //播放
            webTexture.Play();
        }
    }
}

 

访问陀螺仪:

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

public class GyroDemo : MonoBehaviour {

	// Use this for initialization
    bool isGyro;
    Vector3 angle;
    Gyroscope gyro;//陀螺仪

	void Start () {
        //获取设备是否支持陀螺仪,是返回true
        isGyro = SystemInfo.supportsGyroscope;
        //访问陀螺仪
        gyro = Input.gyro;
        //启用陀螺仪
        gyro.enabled = true;
	}
	
	// Update is called once per frame
	void Update () {
        if (isGyro && gyro.enabled) {
            //陀螺仪的旋转值直接应用到摄像机身上(会因为镜像模式导致差异)

            //this.transform.rotation = gyro.attitude;
            angle = gyro.attitude.eulerAngles;
            this.transform.eulerAngles = new Vector3(-angle.x, -angle.y, angle.z);// *Time.deltaTime;
            this.transform.Rotate(Vector3.right * 90, Space.World);
        }
	}
    private void OnGUI()
    {
        GUI.Label(new Rect(0,0,1000,100),angle.ToString());
    }
}

 

Touch触摸事件:

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

public class ObjManager : MonoBehaviour {

    // Use this for initialization
    Touch lastTouch;
    Touch oldTouch1;
    Touch oldTouch2;
    Transform cube;
	void Start () {
        cube = GameObject.Find("Cube").transform;
	}
	
	// Update is called once per frame
	void Update () {
        //控制旋转
        //if (Input.touchCount >=0)
        //{
        //    //Touch[] arr = Input.touches;
        //    Touch t = Input.GetTouch(0);
        //    if (t.phase == TouchPhase.Moved) {
        //        if (lastTouch.fingerId == t.fingerId) {
        //            if (t.position.x - lastTouch.position.x > 0.1f) {
        //                cube.Rotate(0,-5,0);
        //            }
        //        }

        //    }
        //    lastTouch = t;   
       
        //}

        //双指控制放大缩小
        if (Input.touchCount >=2) {
            Touch newt1 = Input.GetTouch(0);
            Touch newt2 = Input.GetTouch(1);
          
            //第2点刚开始接触屏幕, 只记录,不做处理
            if (newt2.phase == TouchPhase.Began)
            {
                oldTouch2 = newt2;
                oldTouch1 = newt1;
                return;
            }

            float newDis = Vector2.Distance(newt1.position, newt2.position);
            float oldDis = Vector2.Distance(oldTouch1.position,oldTouch2.position);
            float offset = newDis - oldDis;

            cube.localScale = new Vector3(cube.localScale.x+offset/100f, cube.localScale.x + offset/100f, cube.localScale.x + offset/100f);
            oldTouch1 = newt1;
            oldTouch2 = newt2;
        }

    }
}

 

AssetBundle加载资源:

猜你喜欢

转载自blog.csdn.net/qq_42485607/article/details/82682935