新建GestureListener脚本,继承 KinectGestures.GestureListenerInterface 接口。
public class GestureListener : MonoBehaviour, KinectGestures.GestureListenerInterface{}
继承接口后要实现接口内的所有内容,按提示实现接口,共五个。
// 检测到用户时
public void UserDetected(long userId, int userIndex){}
// 丢失用户时
public void UserLost(long userId, int userIndex){}
// 检测到手势正在进行
public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos){}
// 手势完成
public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos){}
// 手势没有完成
public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint){}
通常在以上函数内需要判断是否连接到Kinect和当前用户
// 声明一个int型变量表明第几个用户
private int playerIndex = 0;
// 每个接口内最好都判断
public void UserDetected(long userId, int userIndex)
{
KinectManager manager = KinectManager.Instance;
if (!manager || (userIndex != playerIndex))
return;
}
具体使用:
public class GestureListener : MonoBehaviour, KinectGestures.GestureListenerInterface
{
private static GestureListener instance = null;
// 右手往左挥
private bool swipeLeft;
// 左手往右挥
private bool swipeRight;
private int playerIndex = 0;
void Awake()
{
instance = this;
}
public static GestureListener Instance
{
get
{
return instance;
}
}
public bool IsSwipeLeft()
{
if (swipeLeft)
{
swipeLeft = false;
return true;
}
return false;
}
public bool IsSwipeRight()
{
if (swipeRight)
{
swipeRight = false;
return true;
}
return false;
}
// 识别到第一个用户后,在用户手势列表中增加两个挥手手势
public void UserDetected(long userId, int userIndex)
{
KinectManager manager = KinectManager.Instance;
if (!manager || (userIndex != playerIndex))
return;
manager.DetectGesture(userId, KinectGestures.Gestures.SwipeLeft);
manager.DetectGesture(userId, KinectGestures.Gestures.SwipeRight);
}
// 第一个用户丢失后,移除手势列表里的手势
public void UserLost(long userId, int userIndex)
{
KinectManager manager = KinectManager.Instance;
if (!manager || (userIndex != playerIndex))
return;
manager.DeleteGesture(userId, KinectGestures.Gestures.SwipeLeft);
manager.DeleteGesture(userId, KinectGestures.Gestures.SwipeRight);
}
// 手势进行中时,progress展示完成度
public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
{
KinectManager manager = KinectManager.Instance;
if (!manager || (userIndex != playerIndex))
return;
}
// 手势完成后
public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
{
KinectManager manager = KinectManager.Instance;
if (!manager || (userIndex != playerIndex))
return false;
// 左挥完成后,swipeLeft为true
if (gesture == KinectGestures.Gestures.SwipeLeft)
{
swipeLeft = true;
}
else if (gesture == KinectGestures.Gestures.SwipeRight)
{
swipeRight = true;
}
return true;
}
// 手势没做完
public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
{
KinectManager manager = KinectManager.Instance;
if (!manager || (userIndex != playerIndex))
return false;
return true;
}
}
可以通过GestureListener.Instance.IsSwipeLeft()或GestureListener.Instance.IsSwipeRight()判断是否完成了对应挥手动作,从而实现具体逻辑。
可以在其他对应接口里实现自己需要的逻辑。



