ARKit--手势控制GameObj移动及缩放


step 1:
打开unity
导入ARpackage,选择一个AR场景open
创建c#脚本
创建一个GameObject或者使用现有的将要操作的obj

step 2:
将下面脚本拖到GameObject,或Gameobj父类上
share c# code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;

public class MuOuContoller : MonoBehaviour {

    public Transform MuouObj;

    private Touch oldTouch1;

    private Touch oldTouch2;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0 && MuouObj != null)
        {
            if(Input.touchCount == 1) {
                var touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
                {
                    var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                    ARPoint point = new ARPoint
                    {
                        x = screenPosition.x,
                        y = screenPosition.y
                    };

                    ARHitTestResultType[] resultTypes = {
                        ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
                        ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
                        ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                    };

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

                }
            }


            Touch newTouch1 = Input.GetTouch(0);
            Touch newTouch2 = Input.GetTouch(1);

            if (newTouch2.phase == TouchPhase.Began)
            {
                oldTouch1 = newTouch1;
                oldTouch2 = newTouch2;
                return;
            }

            float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
            float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);

            float offset = newDistance - oldDistance;

            float scaleFactor = offset / 100f;
            Vector3 localScale = transform.localScale;
            Vector3 scale = new Vector3(localScale.x + scaleFactor,
                                        localScale.y + scaleFactor,
                                        localScale.z + scaleFactor);
            if (scale.x > 0.5f && scale.y > 0.5f && scale.z > 0.5f)
            {
                transform.localScale = scale;
            }

            oldTouch1 = newTouch1;
            oldTouch2 = newTouch2;
        }
    }

    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);
        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                MuouObj.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                MuouObj.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                MuouObj.transform.LookAt(new Vector3(Camera.main.transform.position.x, MuouObj.transform.position.y, Camera.main.transform.position.z));
                return true;
            }
        }
        return false;
    }


}

将public出去的对象位置拖入要操作的对象

step3:
导出iOS版本

你可能感兴趣的:(Olami语音,OLami)