unity+Vuforia实现用虚拟按钮进行交互,用手势控制模型旋转和缩放

用unity+Vuforia制作了一个简单的AR演示,实现用虚拟按钮进行交互,用手势控制模型旋转和缩放。

    • Vuforia虚拟按钮
    • 手势控制模型旋转缩放

视频链接:https://www.bilibili.com/video/BV1Ny4y147aW/

unity+Vuforia实现用虚拟按钮进行交互,用手势控制模型旋转和缩放_第1张图片

关于Vuforia sdk的安装,unity的安卓环境配置和打包apk,网上教程很多,这里就不介绍了,主要放一下虚拟按钮和手势控制的代码,供大家参考学习。

我的unity版本是2019.4.24f1c1,Vuforia sdk版本是目前最新版9.8.5,人物模型是从unity商店免费下载的。

Vuforia虚拟按钮

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

public class VbController : MonoBehaviour
{
    public GameObject girl;
    private Animator animator;
    void Start()
    {
        girl = GameObject.Find("girl");
        animator = girl.GetComponent();

        var vbbs = GetComponentsInChildren();
        for (int i = 0; i < vbbs.Length; ++i)
        {
            vbbs[i].RegisterOnButtonPressed(OnButtonPressed);
            vbbs[i].RegisterOnButtonReleased(OnButtonReleased);
        }
    }

    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {

        switch (vb.VirtualButtonName)
        {

            case "RunButton":
                animator.SetBool("walk", false);
                animator.SetBool("run", true);
                break;

            case "WalkButton":
                animator.SetBool("run", false);
                animator.SetBool("walk", true);
                break;

            default:
                animator.SetBool("walk", false);
                animator.SetBool("run", false);
                break;
        }
    }

    public void OnButtonReleased(VirtualButtonBehaviour vb)
    {
        animator.Play("Idle");
    }
}

手势控制模型旋转缩放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public enum FingerIE
{
    zero,
    OneFinger,
    TwoFinger,
}
public class FingerController : MonoBehaviour
{

    public Vector3 initialRot;
    public Vector3 initialSca;
    public static FingerController instance;
    IEnumerator ie;
    FingerIE finger_num = FingerIE.zero;
    void Awake()
    {
        instance = this;
    }
    // Update is called once per frame
    void Update()
    {

        if (Input.touchCount == 0)
        {
            if (finger_num != FingerIE.zero)
            {
                StopCoroutine(ie);
                ie = null;
                finger_num = FingerIE.zero;
            }
        }
        else if (Input.touchCount == 1)
        {
            if (finger_num != FingerIE.OneFinger)
            {
                if (ie != null)
                {
                    StopCoroutine(ie);
                }
                ie = IMonitorMouseOneFinger();
                StartCoroutine(ie);
                finger_num = FingerIE.OneFinger;
            }
        }
        else if (Input.touchCount == 2)
        {
            if (finger_num != FingerIE.TwoFinger)
            {
                if (ie != null)
                {
                    StopCoroutine(ie);
                }
                ie = IIMonitorMouseTwoFinger();
                StartCoroutine(ie);
                finger_num = FingerIE.TwoFinger;
            }
        }

    }
    /// 
    /// 一根手指控制转动
    /// 
    /// 
    IEnumerator IMonitorMouseOneFinger()
    {

        Touch oneFingerTouch;
        while (true)
        {
            oneFingerTouch = Input.GetTouch(0);
            if (oneFingerTouch.phase == TouchPhase.Moved)
            {
                Vector2 deltaPos = oneFingerTouch.deltaPosition;
                //transform.Rotate(-Vector3.forward * deltaPos.x * 0.2f, Space.Self);
                //transform.Rotate(-Vector3.left * deltaPos.y * 0.2f, Space.Self);
                //模型绕自身y轴旋转
                transform.Rotate(-Vector3.up * deltaPos.x * 0.2f, Space.Self);
            }
            yield return 0;
        }
    }
    /// 
    /// 两个手指控制缩放
    /// 
    /// 
    IEnumerator IIMonitorMouseTwoFinger()
    {
        Touch firstOldTouch;
        Touch secondOldTouch;
        Touch firstNewTouch;
        Touch secondNewTouch;
        float oldDistance;
        float newDistance;
        while (true)
        {
            firstOldTouch = Input.GetTouch(0);
            secondOldTouch = Input.GetTouch(1);
            oldDistance = Vector2.Distance(firstOldTouch.position, secondOldTouch.position);
            yield return 0;
            firstNewTouch = Input.GetTouch(0);
            secondNewTouch = Input.GetTouch(1);
            newDistance = Vector2.Distance(firstNewTouch.position, secondNewTouch.position);
            if (oldDistance > newDistance && this.transform.localScale.x > 0.25f)
            {
                this.transform.localScale -= Vector3.one * 0.1f;
            }
            else if (oldDistance < newDistance && this.transform.localScale.x < 2f)
            {
                this.transform.localScale += Vector3.one * 0.1f;
            }
        }
    }
    /// 
    /// 复位
    /// 
    public void ResetRot()
    {
        this.transform.localEulerAngles = initialRot;
        this.transform.localScale = initialSca;
    }
}

你可能感兴趣的:(AR演示,Vuforia,unity)