雷达 + 投影 + unity 基本上属于展示类项目的主流
与投影交互原理
TUIO(Table-Top User Interfaces Objects): 一种多点触控通讯协议,默认端口号3333
TUIO模拟器下载 百度网盘下载 提取码 pdd2
一、Untiy从资源商店下载TouchScript插件
勾选上Enable Debug Mode、Enable TUIO后,将自动添加以下宏定义
二、TUIO关键代码分析
TuioInput.cs
namespace TouchScript.InputSources { public sealed class TuioInput : InputSource { //监听3333端口 private void connect() { if (!Application.isPlaying) return; if (server != null) disconnect(); server = new TuioServer(TuioPort); server.Connect(); updateInputs(); } //收到TUIO协议 public override void INTERNAL_DiscardPointer(Pointer pointer) { if (pointer.Type == Pointer.PointerType.Touch) { touchPool.Release(pointer as TouchPointer); } else if (pointer.Type == Pointer.PointerType.Object) { objectPool.Release(pointer as ObjectPointer); } } } }
三、Unity接收TUIO事件
1、在主摄像机上挂上StandardLayer脚本,用来设置要响应触碰操作的对象类型和层。
StandardLayer脚本会屏蔽掉UGUI自己的事件响应(即,无法直接点击UI)
2、场景中添加TuioInput脚本
Tuio Port: 要监听TUIO协议的端口号
Input Types: 要监听的事件类型
TuioListener.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; //需要引入这两个命名空间 using TouchScript; using TouchScript.Pointers; public class TuioListener : MonoBehaviour { void Start() { TouchManager.Instance.PointersPressed += OnPointersPressed; } private void OnPointersPressed(object sender, PointerEventArgs e) { Debug.Log("[Test] OnPointersPressed"); //打印所有触点信息 IList<Pointer> pointers = e.Pointers; for (int i=0; i<pointers.Count; i++) { Pointer p = pointers[i]; Debug.LogFormat("Id={0}, Type={1}, Buttons={2}, Position={3}, PreviousPosition={4}, Flags={5}", p.Id, p.Type, p.Buttons, p.Position.ToString(), p.PreviousPosition.ToString(), p.Flags); //检测是否点中了3D物体 Raycast(p.Position); } } private Vector3 origin, direction; private void Update() { //在场景中显示射线方便观察调式 Debug.DrawRay(origin, direction, Color.red); } //发射射线 private void Raycast(Vector2 screenPoint) { Ray ray = Camera.main.ScreenPointToRay(screenPoint); origin = ray.origin; direction = ray.direction; RaycastHit hit; if (Physics.Raycast(ray, out hit, int.MaxValue)) { Debug.Log("检测到物体: " + hit.transform.name); } } }