交互技术QQ群: 139077032
1. Ini 配置文件插件
2. 读取配置文件人的位置和偏移
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IniRead : MonoBehaviour {
public string INIPath;
public static IniRead Instance;
public float SetPrimaryPlayerPosX;
public float SetPrimaryPlayerPosZ;
public float SetPrimaryPlayerPosXOffset;
public float SetPrimaryPlayerPosZOffset;
void Awake()
{
Instance = this;
INIPath = Application.streamingAssetsPath + "/Config.ini";
IniReadFile(INIPath);
}
void IniReadFile(string path)
{
INIParser iniParser = new INIParser();
iniParser.Open(path);
SetPrimaryPlayerPosX = Convert.ToSingle(iniParser.ReadValue("SetPrimaryPlayerPos", "x", 0d));
SetPrimaryPlayerPosZ = Convert.ToSingle(iniParser.ReadValue("SetPrimaryPlayerPos", "z", 1.8d));
SetPrimaryPlayerPosXOffset = Convert.ToSingle(iniParser.ReadValue("SetPrimaryPlayerPos", "xOffset", 0.15d));
SetPrimaryPlayerPosZOffset = Convert.ToSingle(iniParser.ReadValue("SetPrimaryPlayerPos", "zOffset", 0.15d));
Debug.Log(SetPrimaryPlayerPosX);
iniParser.Close();
}
}
3. 正式代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MyKinectController : MonoBehaviour
{
public Text PersonPos;
KinectManager _manager;
public Text Msg_Text;
public Text text;
public Text text1;
public Text text2;
public Text Tip_text;
// Use this for initialization
void Start()
{
if (KinectManager.Instance) _manager = KinectManager.Instance;
}
// Update is called once per frame
///
///
///
void Update()
{
if (_manager && _manager.IsInitialized() && _manager.IsUserDetected())
{
int userCount = _manager.GetUsersCount();
//bool b = MakeBow(_manager.GetPrimaryUserID());
//if (b)
//{
// DoSomething();
//}
// 遍历得到的当前用户id
foreach (var item in _manager.GetAllUserIds())
{
// 追踪当前用户骨骼
if (_manager.IsJointTracked(item, 0))
{
Vector3 SpineBasePos = _manager.GetJointKinectPosition(item, 0);
if ((IniRead.Instance.SetPrimaryPlayerPosX - IniRead.Instance.SetPrimaryPlayerPosXOffset) < SpineBasePos.x && SpineBasePos.x < (IniRead.Instance.SetPrimaryPlayerPosX + IniRead.Instance.SetPrimaryPlayerPosXOffset) &&
SpineBasePos.z > (IniRead.Instance.SetPrimaryPlayerPosZ - IniRead.Instance.SetPrimaryPlayerPosZOffset) && SpineBasePos.z < (IniRead.Instance.SetPrimaryPlayerPosZ + IniRead.Instance.SetPrimaryPlayerPosZOffset))
{
PersonPos.text = "当前用户: " + userCount + " 识别id: " + item + " userIndex: " + _manager.GetUserIndexById(item) + " 识别你到指定区域: " + SpineBasePos;
if (MakeBow(item))
{
DoSomething();
}
}
GameObject.Find("User_Panel").transform.GetChild(_manager.GetUserIndexById(item)).GetComponent().text =
"用户数量: " + userCount + " id: " + item + " userIndex: " + _manager.GetUserIndexById(item) + " kinect 坐标: " + SpineBasePos;
}
}
}
}
bool isStartMakeBow = true;
///
/// 判断作揖
/// 1. 双手靠近
/// 2. 双手在身前
/// 3. 双手在身体中心
/// 4. 上下挥动手臂
///
///
bool MakeBow(long userID)
{
text.text = "HandClosed(userID) " + HandClosed(userID);
text1.text = "HandForwardBody(userID) " + HandForwardBody(userID);
text2.text = "HandCenterBody(userID) " + HandCenterBody(userID);
if (HandClosed(userID) && HandForwardBody(userID) && HandCenterBody(userID)) // 满足3个条件
{
if (isStartMakeBow) // 初始化信息
{
lastPosY = _manager.GetJointKinectPosition(userID, 23).y;
isSwipeUp = true;
currentIndex = 0;
isStartMakeBow = false;
}
if (HandUpDownWave(userID))
{
DoSomething();
}
}
else
{
isStartMakeBow = true;
}
return false;
}
///
/// 双手靠近
///
///
///
bool HandClosed(long userID)
{
float dist = Mathf.Abs(Vector3.Distance(_manager.GetJointKinectPosition(userID, 7), _manager.GetJointKinectPosition(userID, 11)));
Msg_Text.text = dist.ToString();
if (dist < 0.2f)
{
return true;
}
return false;
}
///
/// 双手在身前
///
///
///
bool HandForwardBody(long userID)
{
if (_manager.GetJointKinectPosition(userID, 7).z < _manager.GetJointKinectPosition(userID, 0).z)
{
return true;
}
return false;
}
///
/// 双手在身中心
///
///
///
bool HandCenterBody(long userID)
{
float leftCenterOffset = _manager.GetJointKinectPosition(userID, 7).x - _manager.GetJointKinectPosition(userID, 0).x;
float rightCenterOffset = _manager.GetJointKinectPosition(userID, 11).x - _manager.GetJointKinectPosition(userID, 0).x;
if (leftCenterOffset > -0.2f && rightCenterOffset < 0.2f)
{
return true;
}
return false;
}
public int count = 2;
int currentIndex;
float lastPosY;
float currenPosY;
bool isSwipeUp = true;
public float offset = 0.2f;
///
/// 双手作揖挥动
///
///
bool HandUpDownWave(long userID)
{
currenPosY = _manager.GetJointKinectPosition(userID, 23).y;
if (currentIndex < count) // 控制上下挥动的次数
{
if (isSwipeUp)
{
if ((currenPosY - lastPosY) > offset)
{
lastPosY = currenPosY;
Debug.Log("SwipeUP");
isSwipeUp = !isSwipeUp;
}
}
else
{
if ((currenPosY - lastPosY) < -offset)
{
lastPosY = currenPosY;
isSwipeUp = !isSwipeUp;
Debug.Log("SwipeDown");
currentIndex++;
Debug.Log(currentIndex);
if (currentIndex.ToString() == count.ToString()) // 整数相等判断
{
return true;
}
}
}
}
return false;
}
int num;
///
/// 判断作揖之后,做某些事
///
void DoSomething()
{
num++;
Tip_text.text = "作揖 " + num;
}
}
运行结果如图: