/*
http://www.cgsoso.com/forum-211-1.htmlCG搜搜 Unity3d 每日Unity3d插件免費(fèi)更新 更有VIP資源!CGSOSO 主打游戲開發(fā),影視設(shè)計等CG資源素材。插件如若商用,請務(wù)必官網(wǎng)購買!daily assets update for try.U should buy the asset from home store if u use it in your project!
*/usingUnityEngine;usingSystem.Collections;usingSystem;//using Windows.Kinect;publicclassCubeGestureListener:MonoBehaviour,KinectGestures.GestureListenerInterface{[Tooltip("GUI-Text to display gesture-listener messages and gesture information.")]publicGUIText gestureInfo;// singleton instance of the classprivatestaticCubeGestureListener instance =null;// internal variables to track if progress message has been displayedprivatebool progressDisplayed;privatefloat progressGestureTime;// whether the needed gesture has been detected or notprivatebool swipeLeft;privatebool swipeRight;privatebool swipeUp;/// <summary>/// Gets the singleton CubeGestureListener instance./// </summary>/// <value>The CubeGestureListener instance.</value>publicstaticCubeGestureListener Instance{get{return instance;}}/// <summary>/// Determines whether swipe left is detected./// </summary>/// <returns><c>true</c> if swipe left is detected; otherwise, <c>false</c>.</returns>publicboolIsSwipeLeft(){if(swipeLeft){swipeLeft =false;returntrue;}returnfalse;}/// <summary>/// Determines whether swipe right is detected./// </summary>/// <returns><c>true</c> if swipe right is detected; otherwise, <c>false</c>.</returns>publicboolIsSwipeRight(){if(swipeRight){swipeRight =false;returntrue;}returnfalse;}/// <summary>/// Determines whether swipe up is detected./// </summary>/// <returns><c>true</c> if swipe up is detected; otherwise, <c>false</c>.</returns>publicboolIsSwipeUp(){if(swipeUp){swipeUp =false;returntrue;}returnfalse;}/// <summary>/// Invoked when a new user is detected. Here you can start gesture tracking by invoking KinectManager.DetectGesture()-function./// </summary>/// <param name="userId">User ID</param>/// <param name="userIndex">User index</param>publicvoidUserDetected(long userId,int userIndex){// the gestures are allowed for the primary user onlyKinectManager manager = KinectManager.Instance;//多人手勢識別將此判斷userId != manager.GetPrimaryUserID())去掉就行。if(!manager ||(userId != manager.GetPrimaryUserID()))return;// detect these user specific gesturesmanager.DetectGesture(userId, KinectGestures.Gestures.SwipeLeft);manager.DetectGesture(userId, KinectGestures.Gestures.SwipeRight);manager.DetectGesture(userId, KinectGestures.Gestures.SwipeUp);manager.DetectGesture(userId, KinectGestures.Gestures.RaiseLeftHand);//舉起左手manager.DetectGesture(userId, KinectGestures.Gestures.RaiseRightHand);//舉起右手if(gestureInfo !=null){gestureInfo.GetComponent<GUIText>().text ="Swipe left, right or up to change the slides.";}}/// <summary>/// Invoked when a user gets lost. All tracked gestures for this user are cleared automatically./// </summary>/// <param name="userId">User ID</param>/// <param name="userIndex">User index</param>publicvoidUserLost(long userId,int userIndex){// the gestures are allowed for the primary user onlyKinectManager manager = KinectManager.Instance;//多人手勢識別將此判斷userId != manager.GetPrimaryUserID())去掉就行。if(!manager ||(userId != manager.GetPrimaryUserID()))return;if(gestureInfo !=null){gestureInfo.GetComponent<GUIText>().text =string.Empty;}}/// <summary>/// Invoked when a gesture is in progress./// </summary>/// <param name="userId">User ID</param>/// <param name="userIndex">User index</param>/// <param name="gesture">Gesture type</param>/// <param name="progress">Gesture progress [0..1]</param>/// <param name="joint">Joint type</param>/// <param name="screenPos">Normalized viewport position</param>publicvoidGestureInProgress(long userId,int userIndex,KinectGestures.Gestures gesture,float progress,KinectInterop.JointType joint,Vector3 screenPos){// the gestures are allowed for the primary user onlyKinectManager manager = KinectManager.Instance;//多人手勢識別將此判斷userId != manager.GetPrimaryUserID())去掉就行。if(!manager ||(userId != manager.GetPrimaryUserID()))return;if((gesture == KinectGestures.Gestures.ZoomOut || gesture == KinectGestures.Gestures.ZoomIn)&& progress >0.5f){if(gestureInfo !=null){string sGestureText =string.Format("{0} - {1:F0}%", gesture, screenPos.z *100f);gestureInfo.GetComponent<GUIText>().text = sGestureText;progressDisplayed =true;progressGestureTime = Time.realtimeSinceStartup;}}elseif((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft ||gesture == KinectGestures.Gestures.LeanRight)&& progress >0.5f){if(gestureInfo !=null){string sGestureText =string.Format("{0} - {1:F0} degrees", gesture, screenPos.z);gestureInfo.GetComponent<GUIText>().text = sGestureText;progressDisplayed =true;progressGestureTime = Time.realtimeSinceStartup;}}elseif(gesture == KinectGestures.Gestures.Run && progress >0.5f){if(gestureInfo !=null){string sGestureText =string.Format("{0} - progress: {1:F0}%", gesture, progress *100);gestureInfo.GetComponent<GUIText>().text = sGestureText;progressDisplayed =true;progressGestureTime = Time.realtimeSinceStartup;}}}/// <summary>/// Invoked if a gesture is completed./// </summary>/// <returns>true</returns>/// <c>false</c>/// <param name="userId">User ID</param>/// <param name="userIndex">User index</param>/// <param name="gesture">Gesture type</param>/// <param name="joint">Joint type</param>/// <param name="screenPos">Normalized viewport position</param>publicboolGestureCompleted(long userId,int userIndex,KinectGestures.Gestures gesture,KinectInterop.JointType joint,Vector3 screenPos){// the gestures are allowed for the primary user onlyKinectManager manager = KinectManager.Instance;//多人手勢識別將此判斷userId != manager.GetPrimaryUserID())去掉就行。if(!manager ||(userId != manager.GetPrimaryUserID()))returnfalse;if(gestureInfo !=null){string sGestureText = gesture +" detected";gestureInfo.GetComponent<GUIText>().text = sGestureText;}if(gesture == KinectGestures.Gestures.SwipeLeft){ swipeLeft =true;print("揮左手");}elseif(gesture == KinectGestures.Gestures.SwipeRight){ swipeRight =true;print("揮右手");}elseif(gesture == KinectGestures.Gestures.SwipeUp)swipeUp =true;elseif(gesture == KinectGestures.Gestures.RaiseRightHand){print("舉起右手");}elseif(gesture == KinectGestures.Gestures.RaiseLeftHand){print("舉起左手");}returntrue;}/// <summary>/// Invoked if a gesture is cancelled./// </summary>/// <returns>true</returns>/// <c>false</c>/// <param name="userId">User ID</param>/// <param name="userIndex">User index</param>/// <param name="gesture">Gesture type</param>/// <param name="joint">Joint type</param>publicboolGestureCancelled(long userId,int userIndex,KinectGestures.Gestures gesture,KinectInterop.JointType joint){// the gestures are allowed for the primary user onlyKinectManager manager = KinectManager.Instance;//多人手勢識別將此判斷userId != manager.GetPrimaryUserID())去掉就行。if(!manager ||(userId != manager.GetPrimaryUserID()))returnfalse;if(progressDisplayed){progressDisplayed =false;if(gestureInfo !=null){gestureInfo.GetComponent<GUIText>().text = String.Empty;}}returntrue;}voidAwake(){instance =this;}voidUpdate(){if(progressDisplayed &&((Time.realtimeSinceStartup - progressGestureTime)>2f)){progressDisplayed =false;gestureInfo.GetComponent<GUIText>().text = String.Empty;Debug.Log("Forced progress to end.");}}}