NGUI 响应touchscript 的 tuio动作

using UnityEngine;
using System.Collections;
using TouchScript;
using TouchScript.Layers;
using TouchScript.Hit;
using TouchScript.Utils;

public class NGUICameraTouchLayer : TouchLayer
{
    private UICamera uiCamera;

    void Start()
    {
        uiCamera = GetComponent<UICamera>();

        uiCamera.allowMultiTouch = true;
    }

    public override Vector3 WorldProjectionNormal
    {
        get
        {
            return uiCamera.transform.forward;
        }
    }

    public override LayerHitResult Hit(Vector2 position, out ITouchHit hit)
    {
        if (base.Hit(position, out hit) == LayerHitResult.Miss)
        {
            return LayerHitResult.Miss;
        }

        if (uiCamera.camera.enabled == false || !uiCamera.camera.gameObject.activeInHierarchy)
        {
            return LayerHitResult.Miss;
        }

        if (!uiCamera.camera.pixelRect.Contains(position))
        {
            return LayerHitResult.Miss;
        }

        if (!UICamera.Raycast(position))
        {
            return LayerHitResult.Miss;
        }

        hit = TouchHitFactory.Instance.GetTouchHit(UICamera.lastHit);
        return LayerHitResult.Hit;
    }

    public override Vector3 ProjectTo(Vector2 screenPosition, Plane projectionPlane)
    {
        return ProjectionUtils.CameraToPlaneProjection(screenPosition, uiCamera.camera, projectionPlane);
    }

    protected override void setName()
    {
        Name = "NGUI Camera";
    }

    protected override LayerHitResult beginTouch(ITouch touch, out ITouchHit hit)
    {
        TouchChanged(touch, TouchPhase.Began);
        return base.beginTouch(touch, out hit);
    }

    protected override void updateTouch(ITouch touch)
    {
        TouchChanged(touch, TouchPhase.Moved);
    }

    protected override void endTouch(ITouch touch)
    {
        TouchChanged(touch, TouchPhase.Ended);
    }

    protected override void cancelTouch(ITouch touch)
    {
        TouchChanged(touch, TouchPhase.Canceled);
    }

    private void TouchChanged(ITouch touch, TouchPhase touchPhase)
    {
        UICamera.currentTouchID = touch.Id;
        UICamera.currentTouch = UICamera.GetTouch(UICamera.currentTouchID);

        bool pressed = (touchPhase == TouchPhase.Began) || UICamera.currentTouch.touchBegan;
        bool unpressed = (touchPhase == TouchPhase.Canceled) || (touchPhase == TouchPhase.Ended);
        UICamera.currentTouch.touchBegan = false;

        if (pressed)
        {
            UICamera.currentTouch.delta = Vector2.zero;
        }
        else
        {
            UICamera.currentTouch.delta = touch.PreviousPosition - touch.Position;
        }

        UICamera.currentTouch.pos = touch.Position;
        UICamera.hoveredObject = UICamera.Raycast(UICamera.currentTouch.pos) ? UICamera.lastHit.collider.gameObject : UICamera.fallThrough;
        if (UICamera.hoveredObject == null)
        {
            UICamera.hoveredObject = UICamera.genericEventHandler;
        }
        UICamera.currentTouch.current = UICamera.hoveredObject;
        UICamera.lastTouchPosition = UICamera.currentTouch.pos;

        // We don't want to update the last camera while there is a touch happening
        if (pressed)
        {
            UICamera.currentTouch.pressedCam = UICamera.currentCamera;
        }
        else if (UICamera.currentTouch.pressed != null)
        {
            UICamera.currentCamera = UICamera.currentTouch.pressedCam;
        }

        // Process the events from this touch
        uiCamera.ProcessTouch(pressed, unpressed);

        // If the touch has ended, remove it from the list
        if (unpressed)
        {
            UICamera.RemoveTouch(UICamera.currentTouchID);
        }
        UICamera.currentTouch = null;
    }
}
用法:把此脚步放到UI Root下的camera上。

你可能感兴趣的:(unity3d,tuio,touchscript)