一、获取用户站在固定位置触发跳跃动画
1、创建空场景并且把KinectController放到场景种
2、UserCountManager类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using com.rfilkov.kinect;
using UnityEngine.UI;
public class UserCountManager : MonoBehaviour
{
private KinectManager kinectManager = null;
public Text m_ShowUserPos;
public Text m_ShowIsJump;
/// <summary>
/// 显示关节的对象
/// </summary>
public Transform overlayObject;
public KinectInterop.JointType trackedJoint = KinectInterop.JointType.Head;
/// <summary>
/// 跳跃的数量
/// </summary>
private int m_jumpcounts;
void Start()
{
if(kinectManager==null)
{
kinectManager = KinectManager.Instance;
}
if(overlayObject==null)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.localPosition = Vector3.zero;
go.name = "HeadObject";
overlayObject = go.transform;
}
}
// Update is called once per frame
void Update()
{
if(kinectManager&& kinectManager.IsInitialized())
{
int usercount = kinectManager.GetUsersCount();
if (usercount > 0)
{
foreach (var item in kinectManager.GetAllUserIds())
{
bool istrackedJoint = kinectManager.IsJointTracked(item, (int)trackedJoint);
int userID = kinectManager.GetUserIndexById(item);
Vector3 pos = kinectManager.GetUserPosition(item);
m_ShowUserPos.text = pos.ToString();
if(istrackedJoint)
{
//通过小球标识头部检测点
Vector3 vector3 = kinectManager.GetJointPosition(item, (int)trackedJoint);
vector3.z = -10 + vector3.z;
overlayObject.position = vector3;
//添加用户跳跃检测
if (!kinectManager.gestureManager.IsTrackingGesture(item, GestureType.Jump))
{
kinectManager.gestureManager.DetectGesture(item, GestureType.Jump);
}
if (GetUserPos_L(pos))
{
Debug.Log("到达中间");
//检查用户是否完成跳跃
if (kinectManager.gestureManager.IsGestureComplete(item, GestureType.Jump, true))
{
Debug.Log("完成跳跃");
m_jumpcounts++;
m_ShowIsJump.text = "完成跳跃" + m_jumpcounts;
}
}
}
}
}
}
}
#region 获取用户是否站在指定位置
public bool GetUserPos_L(Vector3 pos)
{
return GetUserArrivePos(GameMain.Instance.GetJsonDatas().UserPosconfig_L, pos);
}
public bool GetUserPos_M(Vector3 pos)
{
return GetUserArrivePos(GameMain.Instance.GetJsonDatas().UserPosconfig_M, pos);
}
public bool GetUserPos_R(Vector3 pos)
{
return GetUserArrivePos(GameMain.Instance.GetJsonDatas().UserPosconfig_R, pos);
}
private bool GetUserArrivePos(UserPosconfig userPosconfig,Vector3 pos)
{
float x_l = float.Parse(userPosconfig.X_L);
float x_r = float.Parse(userPosconfig.X_R);
float y_f = float.Parse(userPosconfig.Y_F);
float y_b = float.Parse(userPosconfig.Y_B);
if (pos.x> x_l&&pos.x<x_r&&pos.z>y_f&&pos.z<y_b)
{
return true;
}
return false;
}
#endregion
}
二、Json配置位置坐标
1、config.json
{
"UserPosconfig_L": {
"userindex": 0,
"X_L": "-1.2",
"X_R": "-0.8",
"Y_F": "1.5",
"Y_B": "2"
},
"UserPosconfig_M": {
"userindex": 1,
"X_L": "-0.5",
"X_R": "0.5",
"Y_F": "1.5",
"Y_B": "2"
},
"UserPosconfig_R": {
"userindex": 2,
"X_L": "0.8",
"X_R": "-1.2",
"Y_F": "1.5",
"Y_B": "2"
}
}
2、创建坐标类
[System.Serializable]
public class JsonDatas
{
public UserPosconfig UserPosconfig_L;
public UserPosconfig UserPosconfig_M;
public UserPosconfig UserPosconfig_R;
}
public class UserPosconfig
{
/// <summary>
/// 用户索引
/// </summary>
public int userindex {
get; set; }
/// <summary>
/// 设置地面的X
/// </summary>
public string X_L {
get; set; }
public string X_R {
get; set; }
/// <summary>
/// 设置地面的Y
/// </summary>
public string Y_F {
get; set; }
public string Y_B {
get; set; }
}
3、json读取
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using System;
using System.Text;
public class JsonLoad
{
private static JsonLoad instance;
public static JsonLoad Instance
{
get
{
if (instance == null)
{
instance = new JsonLoad();
}
return instance;
}
}
public T LoadJsonDatas<T>(string path)
{
StreamReader sr = new StreamReader(path);
string readContent = sr.ReadToEnd();
T jsonDatas = JsonMapper.ToObject<T>(readContent);
sr.Close();
sr.Dispose();
return jsonDatas;
}
public T LoadFromJsonDate<T>(string UnityAssetSubPath)
{
try
{
string filePath = Application.dataPath + UnityAssetSubPath;
T t = JsonMapper.ToObject<T>(File.ReadAllText(filePath));
if (t == null)
{
Debug.Log("JsonDate=Null");
return default(T);
}
return t;
}
catch (Exception E)
{
Debug.LogError(E);
return default(T);
}
}
//保存
public string SaveJsonDate<T>(T t, string UnityAssetSubPath)
{
string filePath = Application.dataPath + UnityAssetSubPath;
FileInfo file = new FileInfo(filePath);
string jsonstr = JsonMapper.ToJson(t);
using (System.IO.StreamWriter thefile = new StreamWriter(filePath, false, Encoding.UTF8))
{
thefile.WriteLine(jsonstr);
}
//在编辑器下
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
return jsonstr;
}
}
json数据读取
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameMain : Singleton<GameMain>
{
private JsonDatas m_JsonDatas;
protected override void Awake()
{
base.Awake();
m_JsonDatas = JsonLoad.Instance.LoadJsonDatas<JsonDatas>(Application.streamingAssetsPath + "/Json/config.json");
}
/// <summary>
/// 获取到json数据
/// </summary>
/// <returns></returns>
public JsonDatas GetJsonDatas()
{
return m_JsonDatas!=null? m_JsonDatas:null;
}
}
泛型单例类Singleton
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton <T>:MonoBehaviour where T: Singleton<T>
{
private static T instance;
public static T Instance
{
get {
return instance; }
}
protected virtual void Awake()
{
if(instance!=null)
{
Destroy(gameObject);
}else
{
instance = (T)this;
}
}
}