Foxlair

Unity对TUIO的整合

· q8f13

利用了c#版TUIOListener中的代码

用到的库:

首次尝试

给TUIO的cursor, blob, object都做监听 然后用TUIO simulator来实现向本机发送tcp数据 默认端口: 3333。注意检查是否被其他程序占用

代码示例

        // 初始化tuio connector和回调监听 
        _server = new TuioServer(PORT); 


        CursorProcessor cp = new CursorProcessor(); 
        cp.CursorAdded += OnCursorAdded; 
        cp.CursorUpdated += OnCursorUpdated; 
        cp.CursorRemoved += OnCursorRemoved; 


        BlobProcessor bp = new BlobProcessor(); 
        bp.BlobAdded += OnBlobAdded; 
        bp.BlobUpdated += OnBlobUpdated; 
        bp.BlobRemoved += OnBlobRemoved; 


        ObjectProcessor op = new ObjectProcessor(); 
        op.ObjectAdded += OnObjectAdded; 
        op.ObjectUpdated += OnObjectUpdated; 
        op.ObjectRemoved += OnObjectRemoved; 


        _server.Connect(); 


        _server.AddDataProcessor(cp); 
        _server.AddDataProcessor(bp); 
        _server.AddDataProcessor(op); 


        Debug.Log("<Server>: start connecting")
    // 以触摸点为例的回调方法
    void OnCursorAdded(object sender, TuioCursorEventArgs e) 
    { 


    } 


    void OnCursorUpdated(object sender, TuioCursorEventArgs e) 
    { 
        Debug.Log(string.Format("<Cursor>: id: {2}, x:{0}, y:{1}", e.Cursor.X, e.Cursor.Y, e.Cursor.Id)); 
    }


    void OnCursorRemoved(object sender, TuioCursorEventArgs e)
    {
        ...
    }

实测对应座标位置,以及如何对应到unity中

在tuio simulator中,左上为0,0;右下为1,1

若设计上需要在场景中放置地面,那么以这个地面prefab的包围盒四角对应到触摸范围的四角。以实现任意尺寸内容都能对应到场景中的正确位置

#unity   #game development   #TUIO