您当前的位置:首页 VR软件 OSG|OsgEarth 正文

【苹果ARKit教程】零基础,制作AR丧尸游戏

查看: 438| 评论: 0 2017-9-5 06:11 PM| 发布者: PeckBang

1. 苹果设备

因为搭建需要在iOS 11上完成,你需要一个拥有A9或者A10芯片的机器,所以AR Kit只能在一下机器上:

-iPhone SE

-iPhone 6s

-iPhone 6s Plus

-iPhone 7

-iPhone 7 Plus

-iPad Pro

- 9.7-inch iPad 2017


2. 安装Xcode 9

你需要在电脑上安装Xcode 9  https://developer.apple.com/xcode/


3. 升级到 iOS 11

你需要在支持iOS和AR Kit的设备上安装iOS 11 ,推荐使用手机操作,方便匹配。


登陆注册地址 

https://beta.apple.com/sp/betaprogram/guide#ios

 

下载profile地址  

https://beta.apple.com/sp/betaprogram/redemption#ios

 

然后回到手机设置 —> 通用 —> 软件更新,然后你就会看到iOS11更新选项了。点击更新,顺着流程完成更新下载。

 

4. 安装 Unity

官网下载:Unity - Download

注意安装 5.6版本以上的软件,我录制视频时使用的是 2017.1.0f3 的最新版本。

 

5. 开发

为表诚意,以下是Unity中修改的两个C#的文件,可以跟着视频自己敲,也可以参考这里的:


ZombieControl.cs

using System.Collections;using System.Collections.Generic;using UnityEngine;public class ZombieControl : MonoBehaviour {

	private Animation animation;
	private bool moving = false;

	// Use this for initialization
	void Start () {
		animation = GetComponent <Animation>();

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

			transform.Translate (Vector3.forward * Time.deltaTime * (transform.localScale.x * .05f));
		
		}
	}

	public void Walk(){
	
		if (!animation.isPlaying) {

			animation.Play ();
			moving = true;
		
		} else {
			animation.Stop ();
			moving = false;
		}
	
	}

	public void LookAt(){
	
		transform.LookAt (Camera.main.transform.position);
		transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
	
	}

	public void Bigger(){
	
		transform.localScale += new Vector3(1,1,1);

	}

	public void Smaller(){

		if (transform.localScale.x > 1) {

			transform.localScale -= new Vector3(1,1,1);
		
		}

	}}


UnityARHitTestExample.cs

using System;using System.Collections.Generic;using UnityEngine.EventSystems;namespace UnityEngine.XR.iOS{
	public class UnityARHitTestExample : MonoBehaviour
	{
		public Transform m_HitTransform;

		bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes)
		{
			List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);
			if (hitResults.Count > 0) {
				foreach (var hitResult in hitResults) {
					Debug.Log ("Got hit!");
					m_HitTransform.position = UnityARMatrixOps.GetPosition (hitResult.worldTransform);
					m_HitTransform.rotation = UnityARMatrixOps.GetRotation (hitResult.worldTransform);
					Debug.Log (string.Format ("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
					return true;
				}
			}
			return false;
		}

		// Update is called once per frame
		void Update () {
			if (Input.touchCount > 0 && m_HitTransform != null)
			{
				var touch = Input.GetTouch(0);
				if (touch.phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject(0))
				{
					transform.localPosition = Vector3.zero;

					var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
					ARPoint point = new ARPoint {
						x = screenPosition.x,
						y = screenPosition.y
					};

					// prioritize reults types
					ARHitTestResultType[] resultTypes = {
						ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 
						// if you want to use infinite planes use this:
						//ARHitTestResultType.ARHitTestResultTypeExistingPlane,
						ARHitTestResultType.ARHitTestResultTypeHorizontalPlane, 
						ARHitTestResultType.ARHitTestResultTypeFeaturePoint
					}; 

					foreach (ARHitTestResultType resultType in resultTypes)
					{
						if (HitTestWithResultType (point, resultType))
						{
							return;
						}
					}
				}
			}
		}
	}}


52VR.COM微信扫一扫
52vr公众号
专注于VR的学习、开发和人才交流

52VR开发交流

手机版|VR开发网 |网站地图 统计  津ICP备16006248号

GMT+8, 2017-11-24 11:08 PM

返回顶部