Unity + 雷达功能的实现

下载带有 TUIO 协议的 touchScript 插件,

这把博客只是为了让那些 要实现雷达交互的人入个门,给你们一个思路,毕竟现在感觉写这个雷达交互的博客好像压根没有,我也是大佬带着才能入门的,感谢我家老大,哈哈哈

OK,我们来用 Unity 实现雷达的交互

第一种方式:新建一个空物体,挂上下面两个脚本

Unity + 雷达功能的实现_第1张图片

ok,可以写代码了,简单吧,新建一个脚本继承 BBSimpleTouchableObject类,挂载到你需要实现交互的对象身上去,重写里面的 单指 双指的方法

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

public class Tafdafa : BBSimpleTouchableObject  {

    public bool allowDrag = true;
    public bool allowScale = true;
    public bool allowRotate = true;

    public float minimumScale = 0.0f;
    public float maximumScale = 30.0f;

    private Transform saveParent;
    private Vector3 movement;

    public GameObject pivot;
    public override void handleSingleTouch(iPhoneTouch touch)
    {
        print(touch.position);

        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit;
        if (Physics.Raycast(ray,out hit))
        {
            if (hit.collider.gameObject.name == "Button")
            {
                print("草拟吗");
            }
        }
    }
    public override void handleDoubleTouch(ArrayList events)
    {
        if (!allowRotate && !allowScale) return;
       // double touch can be a scale or a rotate, or both
            // 
            // let's do the rotate first
            // since this is a 2 touch gesture, we can only rotate in 2d, which in this case is in the camera plane
            // pivot on the lower touch index

        iPhoneTouch touch0 = (iPhoneTouch)events[0];
        iPhoneTouch touch1 = (iPhoneTouch)events[1];
        if (touch0.fingerId > touch1.fingerId)
        {
            // flip them, 0 should be the earlier index
            touch0 = (iPhoneTouch)events[1];
            touch1 = (iPhoneTouch)events[0];
        }

        this.startPivot(gameObject.transform.position);

        // 	
        // 	//////////////////////////////// ROTATE
        // 	

        float zDistanceFromCamera = Vector3.Distance(renderingCamera.transform.position, gameObject.transform.position);

        Vector3 screenPosition0 = new Vector3(touch0.position.x, touch0.position.y, zDistanceFromCamera);
        Vector3 lastScreenPosition0 = new Vector3(touch0.position.x - touch0.deltaPosition.x, touch0.position.y - touch0.deltaPosition.y, zDistanceFromCamera);

        Vector3 screenPosition1 = new Vector3(touch1.position.x, touch1.position.y, zDistanceFromCamera);
        Vector3 lastScreenPosition1 = new Vector3(touch1.position.x - touch1.deltaPosition.x, touch1.position.y - touch1.deltaPosition.y, zDistanceFromCamera);


        // 
        // 	///////////////////////////  SCALE
        // 
        if (allowScale)
        {
            float distNow = (screenPosition0 - screenPosition1).magnitude;
            float distThen = (lastScreenPosition0 - lastScreenPosition1).magnitude;

            float scale = distNow / distThen;

            // presume for the time being that our scales are uniform
            if (transform.localScale.x * scale < minimumScale) scale = minimumScale / transform.localScale.x;
            if (transform.localScale.x * scale > maximumScale) scale = maximumScale / transform.localScale.x;

            Vector3 local = pivot.transform.localScale;
            local.x *= scale;
            local.y *= scale;
            local.z *= scale;
            pivot.transform.localScale = local;
        }

        this.endPivot();
    }

    virtual protected void startPivot(Vector3 pivotPosition)
    {
        if (pivot == null)
        {
            pivot = new GameObject();
            pivot.name = "BBBasicTouchManipulation Pivot";
            pivot.transform.position = pivotPosition;
        }

        saveParent = gameObject.transform.parent;
        gameObject.transform.parent = null;
        pivot.transform.parent = saveParent;
        gameObject.transform.parent = pivot.transform;
    }

    virtual protected void endPivot()
    {
        gameObject.transform.parent = saveParent;
        pivot.transform.parent = null;
        Destroy(pivot);
    }
}

第二种方式 随便打开touchScript插件自带的场景,加上一个 TuioInput.cs的脚本,

Unity + 雷达功能的实现_第2张图片

Ok,剩下的就是用TouchScript插件 自带的单指,双指,多点等的方法去写代码吧,不需要管雷达,这个时候你无论是触摸还是雷达都已经联系上了,神奇吧至于 touchScript插件的使用,你们可以去看我以前写的博客,我这里就不赘述了

你可能感兴趣的:(雷达)