1. 程式人生 > >Unity + 雷達功能的實現

Unity + 雷達功能的實現

下載帶有 TUIO 協議的 touchScript 外掛,

這把部落格只是為了讓那些 要實現雷達互動的人入個門,給你們一個思路,畢竟現在感覺寫這個雷達互動的部落格好像壓根沒有,我也是大佬帶著才能入門的,感謝我家老大,哈哈哈

OK,我們來用 Unity 實現雷達的互動

第一種方式:新建一個空物體,掛上下面兩個指令碼

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的指令碼,

Ok,剩下的就是用TouchScript外掛 自帶的單指,雙指,多點等的方法去寫程式碼吧,不需要管雷達,這個時候你無論是觸控還是雷達都已經聯絡上了,神奇吧至於 touchScript外掛的使用,你們可以去看我以前寫的部落格,我這裡就不贅述了