一、框架视图
二、主要代码
注:本文中的通讯协议为虚拟协议
Hand
Hand_Indexfinger
using System.Threading;
using UnityEngine;
public class Hand_Indexfinger : MonoBehaviour
{
public GameObject indexfinger;
private Quaternion _indexfinger;//食指旋转角度
private float indexfingerZ;//食指
private float indexfingerZchild;
private float tempindexfingerZ;
//食指锁定协议
public byte byteIndexfinger_H;
public byte byteIndexfinger_L;
public bool isTouch_indexfinger; //是否触碰到
public float elasticIndexfinger;//食指的弹性变化度
public float elasticIndexfingerTemp;//食指的弹性变化度
public byte[] byteIndexfinger; //存贮食指高字节和低字节
// public float timer=0; //定义定时器
void Awake()
{
_indexfinger = indexfinger.transform.rotation;
//食指
indexfingerZ = indexfinger.transform.localEulerAngles.z;
indexfingerZchild = indexfinger.transform.GetChild(0).GetChild(0).localEulerAngles.z;
tempindexfingerZ = indexfingerZchild;
}
void Start()
{
// HandManager.instance.timer = HandManager.instance.HandManager.instance.timer;
}
void FixedUpdate()
{
Ctrl_indexfinger();
if (isTouch_indexfinger)
{
//HandManager.instance.timer += Time.deltaTime;
if (HandManager.instance.timer >= 0f && HandManager.instance.timer < 1f)
{
//elasticIndexfinger= elasticIndexfingerTemp+30;
elasticIndexfinger += 2;
Debug.Log("食指0.5秒++elasticIndexfinger-----------" + elasticIndexfinger);
}
if (HandManager.instance.timer >= 1f)
{
// elasticIndexfinger = elasticIndexfingerTemp;
// elasticIndexfinger -= 200;
//elasticIndexfinger = elasticIndexfingerTemp -30;
elasticIndexfinger -= -2;
Debug.Log("食指1秒++elasticIndexfinger-----------" + elasticIndexfinger);
}
//if (HandManager.instance.timer>=1f)
//{
// HandManager.instance.timer = 0;
// Debug.Log("食指时间归零");
//}
byteIndexfinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticIndexfinger);
byteIndexfinger_H = byteIndexfinger[0];
byteIndexfinger_L = byteIndexfinger[1];
}
else
{
// byteIndexfinger_H = 0X00;
// byteIndexfinger_L = 0X00;
}
}
///
/// 进入触发器
///
///
void OnTriggerEnter(Collider coll)
{
//if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name !=null )
{
if (coll.gameObject.tag=="Heart")
{
elasticIndexfinger = HandManager.instance.handSocket.angleX_indexfinger;
elasticIndexfingerTemp = elasticIndexfinger;
isTouch_indexfinger = true;
}
else
{
byteIndexfinger_H = HandManager.instance.handSocket.recvDataTemp[20];
byteIndexfinger_L = HandManager.instance.handSocket.recvDataTemp[21];
}
}
}
///
/// 离开触发器
///
///
void OnTriggerExit(Collider coll)
{
//if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name != null)
{
byteIndexfinger_H = 0X00;
byteIndexfinger_L = 0X00;
isTouch_indexfinger = false;
}
}
///
/// 退出时 复位
///
void OnApplicationQuit()
{
indexfinger.transform.rotation = _indexfinger;
}
///
/// 食指
///
private void Ctrl_indexfinger()
{
// indexfinger.transform.localEulerAngles = new Vector3(indexfinger.transform.localEulerAngles.x, Map(-angleY_indexfinger, 693, 769, -48, 0)+120, -MapData(angleX_indexfinger, 220, 900, -90.137f,5.733f)-60);
indexfinger.transform.localEulerAngles = new Vector3(indexfinger.transform.localEulerAngles.x, indexfinger.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_indexfinger, 220, 900, -90.137f, 5.733f) - 60);
if (indexfinger.transform.localEulerAngles.z - 600 < indexfingerZ)
{
tempindexfingerZ = -MapData(HandManager.instance.handSocket.angleX_indexfinger, 220, 900, -90.137f, 5.733f) - 60;
}
else
{
tempindexfingerZ = indexfingerZchild;
}
indexfinger.transform.GetChild(0).localEulerAngles = new Vector3(indexfinger.transform.GetChild(0).localEulerAngles.x, indexfinger.transform.GetChild(0).localEulerAngles.y, tempindexfingerZ - 25);
indexfinger.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(indexfinger.transform.GetChild(0).localEulerAngles.x, indexfinger.transform.GetChild(0).localEulerAngles.y, tempindexfingerZ - 15);
}
///
/// 获取两数之间对应的取值范围
///
///
///
///
///
///
///
public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
{
if (min_in > max_in)
{
min_in = min_in + max_in;
max_in = min_in - max_in;
min_in = min_in - max_in;
min_out = min_out + max_out;
max_out = min_out - max_out;
min_out = min_out - max_out;
}
if (value < min_in)
value = min_in;
if (value > max_in)
value = max_in;
return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
}
}
Hand_Midfiger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class Hand_Midfiger : MonoBehaviour {
public GameObject midfinger;
private Quaternion _midfinger;
private float midfingerZ;//中指
private float midfingerZchild;
private float tempmidfingerZ;
//中指锁定协议
public byte byteMidfinger_H;
public byte byteMidfinger_L;
public bool isTouch_midfinger; //是否触碰到中指
public float elasticMidfinger;//中指弹性变化度
public float elasticMidfingerTemp;//中指弹性变化度
public byte[] byteMinfinger; //存贮中指的高字节和低字节
// public float timer=0; //计时器
void Awake() {
_midfinger = midfinger.transform.rotation; //初始赋值
//中指
midfingerZ = midfinger.transform.localEulerAngles.z;
midfingerZchild = midfinger.transform.GetChild(0).localEulerAngles.z;
tempmidfingerZ = midfingerZchild;
}
void Start() {
// HandManager.instance.timer =HandManager.instance.timer;
}
void FixedUpdate() {
Ctrl_midfinger(); //获取手指状态信息
if (isTouch_midfinger)
{
// HandManager.instance.timer += Time.deltaTime;
if (HandManager.instance.timer >= 0f&&HandManager.instance.timer<1f)
{
//elasticMidfinger = elasticMidfingerTemp + 30;
elasticMidfinger += 2;
Debug.Log("0.5f中指::"+elasticMidfinger);
}
if (HandManager.instance.timer>=1f&&HandManager.instance.timer<2f)
{
//elasticMidfinger = elasticMidfingerTemp - 30;
elasticMidfinger -= 2;
Debug.Log("1f中指::"+elasticMidfinger);
}
byteMinfinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticMidfinger);
byteMidfinger_H = byteMinfinger[0];
byteMidfinger_L = byteMinfinger[1];
}
else
{
// byteMidfinger_H = 0X00;
// byteMidfinger_L = 0X00;
}
}
void OnApplicationQuit()
{
midfinger.transform.rotation = _midfinger;
}
///
/// 进入触发器
void OnTriggerEnter(Collider coll)
{
//if (coll.gameObject.name == Tags.Sphere)
//if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name !=null )
{
if (coll.gameObject.tag=="Heart")
{
isTouch_midfinger = true;
elasticMidfinger = HandManager.instance.handSocket.angleX_midfinger;
elasticMidfingerTemp = elasticMidfinger;
}
else
{
byteMidfinger_H = HandManager.instance.handSocket.recvDataTemp[14];
byteMidfinger_L = HandManager.instance.handSocket.recvDataTemp[15];
}
}
}
///
/// 离开触发器
///
///
void OnTriggerExit(Collider coll)
{
if (coll.gameObject.name != null)
{
byteMidfinger_H = 0X00;
byteMidfinger_L = 0X00;
isTouch_midfinger = false;
}
}
///
/// 中指状态
///
private void Ctrl_midfinger()
{
midfinger.transform.localEulerAngles = new Vector3(midfinger.transform.localEulerAngles.x, midfinger.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_midfinger, 260, 900, -90.137f, 5.733f) - 85);
if (midfinger.transform.localEulerAngles.z - 600 < midfingerZ)
{
// tempmidfingerZ = -MapData(HandManager.instance.handSocket.angleX_midfinger, 260, 900, -90.137f, 5.733f) - 85;
tempmidfingerZ = -MapData(HandManager.instance.handSocket.angleX_midfinger, 220, 900, -90.137f, 5.733f) - 60;
}
else
{
tempmidfingerZ = midfingerZchild;
}
midfinger.transform.GetChild(0).localEulerAngles = new Vector3(midfinger.transform.GetChild(0).localEulerAngles.x, midfinger.transform.GetChild(0).localEulerAngles.y, tempmidfingerZ - 25);
midfinger.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(midfinger.transform.GetChild(0).localEulerAngles.x, midfinger.transform.GetChild(0).localEulerAngles.y, tempmidfingerZ - 15);
}
///
/// 获取两数间的取值范围
///
public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
{
if (min_in > max_in)
{
min_in = min_in + max_in;
max_in = min_in - max_in;
min_in = min_in - max_in;
min_out = min_out + max_out;
max_out = min_out - max_out;
min_out = min_out - max_out;
}
if (value < min_in)
value = min_in;
if (value > max_in)
value = max_in;
return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
}
}
Hand_Pinkie
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class Hand_Pinkie : MonoBehaviour
{
public GameObject pinkie;
private Quaternion _pinkie;
private float pinkieZ; //尾指
private float pinkieZchild;
private float temppinkieZ;
//锁定协议
public byte bytePinkie_H;
public byte bytePinkie_L;
public bool isTouch_pinkie; //是否触碰到尾指
public float elasticPinkie;//尾指弹性变化度
public float elasticPinkieTemp; //尾指弹性变化度
public byte[] bytePinkie;//存贮尾指的高字节和低字节
//public float timer = 0;//计时器
void Awake()
{
_pinkie = pinkie.transform.rotation;
//尾指
pinkieZ = pinkie.transform.localEulerAngles.z;
pinkieZchild = pinkie.transform.GetChild(0).localEulerAngles.z;
temppinkieZ = pinkieZchild;
}
void Start()
{
// HandManager.instance.timer = HandManager.instance.timer;
}
void FixedUpdate()
{
Ctrl_pinkie();
if (isTouch_pinkie)
{
// HandManager.instance.timer += Time.deltaTime;
if (HandManager.instance.timer >= 0 && HandManager.instance.timer <1f)
{
//elasticPinkie = elasticPinkieTemp + 30;
elasticPinkie +=2;
Debug.Log("0.5f尾指::" + elasticPinkie);
}
if (HandManager.instance.timer >= 2f && HandManager.instance.timer < 2f)
{
//elasticPinkie = elasticPinkie - 30;
elasticPinkie -=2;
Debug.Log("1f尾指::" + elasticPinkie);
}
bytePinkie = HandManager.instance.handSocket.GetHightByteLowByte(elasticPinkie);
bytePinkie_H = bytePinkie[0];
bytePinkie_L = bytePinkie[1];
}
}
///
/// 进入触发器
///
///
void OnTriggerEnter(Collider coll)
{
//if (coll.gameObject.name == Tags.Sphere)
// if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name != null)
{
if (coll.gameObject.tag == "Heart")
{
isTouch_pinkie = true;
elasticPinkie = HandManager.instance.handSocket.angleX_pinkie;
elasticPinkieTemp = elasticPinkie;
}
else
{
bytePinkie_H = HandManager.instance.handSocket.recvDataTemp[2];
bytePinkie_L = HandManager.instance.handSocket.recvDataTemp[3];
}
}
}
///
/// 离开触发器
///
///
void OnTriggerExit(Collider coll)
{
if (coll.gameObject.name != null)
{
bytePinkie_H = 0X00;
bytePinkie_L = 0X00;
isTouch_pinkie = false;
}
}
void OnApplicationQuit()
{
pinkie.transform.rotation = _pinkie;
}
///
/// 尾指
///
private void Ctrl_pinkie()
{
pinkie.transform.localEulerAngles = new Vector3(pinkie.transform.localEulerAngles.x, pinkie.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_pinkie, 260, 900, -90.137f, 5.733f) - 85);
if (pinkie.transform.localEulerAngles.z - 600 < pinkieZ)
{
temppinkieZ = -MapData(HandManager.instance.handSocket.angleX_pinkie, 260, 900, -90.137f, 5.733f) - 60;
//tempringfingerZ = -MapData(HandManager.instance.handSocket.angleX_ringfinger, 260, 900, -90.137f, 5.733f) - 85;
}
else
{
temppinkieZ = pinkieZchild;
}
pinkie.transform.GetChild(0).localEulerAngles = new Vector3(pinkie.transform.GetChild(0).localEulerAngles.x, pinkie.transform.GetChild(0).localEulerAngles.y, temppinkieZ - 25);
pinkie.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(pinkie.transform.GetChild(0).localEulerAngles.x, pinkie.transform.GetChild(0).localEulerAngles.y, temppinkieZ - 15);
}
public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
{
if (min_in > max_in)
{
min_in = min_in + max_in;
max_in = min_in - max_in;
min_in = min_in - max_in;
min_out = min_out + max_out;
max_out = min_out - max_out;
min_out = min_out - max_out;
}
if (value < min_in)
value = min_in;
if (value > max_in)
value = max_in;
return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
}
}
Hand_Ringfiger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class Hand_Ringfiger : MonoBehaviour
{
public GameObject ringfinger; //持有无名指的引用
private Quaternion _ringfinger; //记录原始角度
private float ringfingerZ; //无名指
private float ringfingerZchild;
private float tempringfingerZ;
//无名指锁定协议
public byte byteRingfinger_H;
public byte byteRingfinger_L;
public bool isTouch_ringfinger; //是否触碰到无名指
public float elasticRingfinger;//无名指弹性变化度
public float elasticRingfingerTemp;//无名指弹性变化度
public byte[] byteRingfinger;//存贮无名指的高字节和低字节
//public float timer = 0;//计时器
void Awake()
{
_ringfinger = ringfinger.transform.rotation;
//无名指
ringfingerZ = ringfinger.transform.localEulerAngles.z;
ringfingerZchild = ringfinger.transform.GetChild(0).localEulerAngles.z;
tempringfingerZ = ringfingerZchild;
}
void Start()
{
// HandManager.instance.timer = HandManager.instance.timer;
}
void FixedUpdate()
{
Ctrl_ringfinger(); //检测手指状态
if (isTouch_ringfinger)
{
// HandManager.instance.timer += Time.deltaTime;
if (HandManager.instance.timer >= 0f&&HandManager.instance.timer<1f)
{
//elasticRingfinger = elasticRingfingerTemp + 30;
elasticRingfinger +=2;
Debug.Log("0.5f无名指::" + elasticRingfinger);
}
if (HandManager.instance.timer >= 1f && HandManager.instance.timer < 2f)
{
//elasticRingfinger = elasticRingfingerTemp - 30;
elasticRingfinger -=2;
Debug.Log("1.0f无名指::" + elasticRingfinger);
}
byteRingfinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticRingfinger);
byteRingfinger_H = byteRingfinger[0];
byteRingfinger_L = byteRingfinger[1];
}
else
{
// byteRingfinger_H = 0X00;
// byteRingfinger_L = 0X00;
}
}
///
/// 进入触发器
///
///
void OnTriggerEnter(Collider coll)
{
//if (coll.gameObject.name == Tags.Sphere)
//if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name != null)
{
if (coll.gameObject.tag == "Heart")
{
isTouch_ringfinger = true;
elasticRingfinger = HandManager.instance.handSocket.angleX_ringfinger;
elasticRingfingerTemp = elasticRingfinger;
}
else
{
byteRingfinger_H = HandManager.instance.handSocket.recvDataTemp[8];
byteRingfinger_L = HandManager.instance.handSocket.recvDataTemp[9];
}
}
}
///
/// 离开触发器
///
///
void OnTriggerExit(Collider coll)
{
// if (coll.gameObject.name == Tags.Sphere)
// if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name != null)
{
byteRingfinger_H = 0X00;
byteRingfinger_L = 0X00;
isTouch_ringfinger = false;
}
}
void OnApplicationQuit()
{
ringfinger.transform.rotation = _ringfinger;
}
///
/// 无名指
///
private void Ctrl_ringfinger()
{
ringfinger.transform.localEulerAngles = new Vector3(ringfinger.transform.localEulerAngles.x, ringfinger.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_ringfinger, 260, 900, -90.137f, 5.733f) - 60);
if (ringfinger.transform.localEulerAngles.z - 600 < ringfingerZ)
{
//tempringfingerZ = -MapData(HandManager.instance.handSocket.angleX_ringfinger, 260, 900, -90.137f, 5.733f) - 85;
tempringfingerZ = -MapData(HandManager.instance.handSocket.angleX_ringfinger, 220, 900, -90.137f, 5.733f) - 60;
}
else
{
tempringfingerZ = ringfingerZchild;
}
ringfinger.transform.GetChild(0).localEulerAngles = new Vector3(ringfinger.transform.GetChild(0).localEulerAngles.x, ringfinger.transform.GetChild(0).localEulerAngles.y, tempringfingerZ - 25);
ringfinger.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(ringfinger.transform.GetChild(0).localEulerAngles.x, ringfinger.transform.GetChild(0).localEulerAngles.y, tempringfingerZ - 15);
}
///
/// 转换值
///
public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
{
if (min_in > max_in)
{
min_in = min_in + max_in;
max_in = min_in - max_in;
min_in = min_in - max_in;
min_out = min_out + max_out;
max_out = min_out - max_out;
min_out = min_out - max_out;
}
if (value < min_in)
value = min_in;
if (value > max_in)
value = max_in;
return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
}
}
Hand_Thumb
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class Hand_Thumb : MonoBehaviour
{
public GameObject thumb; //持有拇指的引用
//记录旋转角度
private Quaternion _thumb;
private float thumbZ; //拇指
private float thumbZchild; //拇指的子物体
private float tempthumbZ; //存放值
//拇指锁定协议
public byte byteThumb_H;
public byte byteThumb_L;
public bool isTouch_thumb; //是否触碰到拇指
public float elasticThumb;//拇指的弹性变化度
public float elasticThumbTemp;//拇指的弹性变化度
public byte[] byteThumb; //存贮拇指的高字节和低字节
//public float timer=0; //计时器
public bool isExit;
void Awake()
{
_thumb = thumb.transform.rotation;
thumbZ = thumb.transform.localEulerAngles.z;
thumbZchild = thumb.transform.GetChild(0).GetChild(0).localEulerAngles.z;
tempthumbZ = thumbZchild;
}
///
/// 物理检测手指
///
void FixedUpdate()
{
Ctrl_thumb();
if (isTouch_thumb)
{
// HandManager.instance.timer += Time.deltaTime;
if (HandManager.instance.timer >= 0f && HandManager.instance.timer < 1f)
{
//elasticThumb = elasticThumbTemp + 50;
elasticThumb += 2;
Debug.Log("0.5f拇指" + elasticThumb);
}
if (HandManager.instance.timer >= 1f && HandManager.instance.timer <2f)
{
//elasticThumb = elasticThumbTemp - 50;
elasticThumb -= 2;
Debug.Log("1f拇指:" + elasticThumb);
}
byteThumb = HandManager.instance.handSocket.GetHightByteLowByte(elasticThumb);
byteThumb_H = byteThumb[0];
byteThumb_L = byteThumb[1];
}
else
{
// byteThumb_H = 0X00;
// byteThumb_L = 0X00;
}
}
///
/// 进入触发器
///
///
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.name != null)
{
if (coll.gameObject.tag == "Heart")
{
isTouch_thumb = true;
elasticThumb = HandManager.instance.handSocket.angleX_thumb;
elasticThumbTemp = elasticThumb;
}
else
{
byteThumb_H = HandManager.instance.handSocket.recvDataTemp[26];
byteThumb_L = HandManager.instance.handSocket.recvDataTemp[27];
}
if (coll.gameObject.name == "R_thumb_end")
{
isExit = false;
}
}
}
///
/// 离开触发器
///
///
void OnTriggerExit(Collider coll)
{
if (coll.gameObject.name != null)
{
byteThumb_H = 0X00;
byteThumb_L = 0X00;
isTouch_thumb = false;
}
if (coll.gameObject.name == "R_thumb_end")
{
isExit = true;
}
}
void OnApplicationQuit()
{
thumb.transform.rotation = _thumb;
}
///
/// 拇指
///
private void Ctrl_thumb()
{
//thumb.transform.localEulerAngles = new Vector3(thumb.transform.localEulerAngles.x, Map(angleY_thumb, 693, 769, -35, 40), MapData(angleX_thumb, 400, 730, -26.361f, -115.18f));
thumb.transform.localEulerAngles = new Vector3(thumb.transform.localEulerAngles.x, thumb.transform.localEulerAngles.y, MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f) + 25);
if (thumb.transform.localEulerAngles.z - 800 < thumbZ)
{
tempthumbZ = MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f);
}
else
{
tempthumbZ = thumbZchild;
}
thumb.transform.GetChild(0).localEulerAngles = new Vector3(thumb.transform.GetChild(0).localEulerAngles.x, thumb.transform.GetChild(0).localEulerAngles.y, tempthumbZ + 45);
// str = "Y轴: " + MapData(angleY_thumb, 693, 769, -35, 40) + " Z轴::" + MapData(angleX_thumb, 400, 730, -26.361f, -115.18f);
}
public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
{
if (min_in > max_in)
{
min_in = min_in + max_in;
max_in = min_in - max_in;
min_in = min_in - max_in;
min_out = min_out + max_out;
max_out = min_out - max_out;
min_out = min_out - max_out;
}
if (value < min_in)
value = min_in;
if (value > max_in)
value = max_in;
return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
}
}
HandManager
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class HandManager : MonoBehaviour
{
//SkinnedMesh 播放动画时与MeshCollider不同步问题解决方案
public SkinnedMeshRenderer meshRenderer;
public MeshCollider meshCollider;
public static HandManager instance;//单例模式
[HideInInspector]
public HandSocket handSocket;
[HideInInspector]
public Hand_Thumb hand_Thumb;
[HideInInspector]
public Hand_Indexfinger hand_Indexfinger;
[HideInInspector]
public Hand_Midfiger hand_Midfiger;
[HideInInspector]
public Hand_Ringfiger hand_Ringfiger;
[HideInInspector]
public Hand_Pinkie hand_Pinkie;
// private Thread connectThread;
public float timer = 0; //计时器
public bool isTouch; //是否触碰指定物体
void Awake()
{
instance = this;
handSocket = FindObjectOfType();
//获取持有的脚本
hand_Thumb = FindObjectOfType();
hand_Indexfinger = FindObjectOfType();
hand_Midfiger = FindObjectOfType();
hand_Ringfiger = FindObjectOfType();
hand_Pinkie = FindObjectOfType();
//connectThread = new Thread(new ThreadStart(SocketSendToHand));
//connectThread.IsBackground = true;
//connectThread.Start();
}
void Start()
{
// print("整型::"+a+"高字节:::"+GetHightWord(a)+"低字节::"+GetLowWord(a));
//byte[] bs2 = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)888));// 注意0x1234
//print("字节:::"+(bs2[0].ToString("X2") + " " + bs2[1].ToString("X2")));
}
//更新皮肤渲染跟网络碰撞器的同步
void Update()
{
Mesh collideMesh = new Mesh();
meshRenderer.BakeMesh(collideMesh); //更新mesh
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = collideMesh; //将新的mesh赋值给meshcollider
if (isTouch)
{
timer += Time.deltaTime;
if (timer>=2f)
{
timer = 0;
}
Debug.Log(timer);
}
}
//更新发送数据给手柄
void FixedUpdate()
{
SocketSendToHand();
}
///
/// 发送数据给手柄
///
void SocketSendToHand()
{
//正向锁定协议
handSocket.handBytesFore[2] = hand_Pinkie.bytePinkie_H;
handSocket.handBytesFore[3] = hand_Pinkie.bytePinkie_L;
handSocket.handBytesFore[4] = hand_Ringfiger.byteRingfinger_H;
handSocket.handBytesFore[5] = hand_Ringfiger.byteRingfinger_L;
handSocket.handBytesFore[6] = hand_Midfiger.byteMidfinger_H;
handSocket.handBytesFore[7] = hand_Midfiger.byteMidfinger_L;
handSocket.handBytesFore[8] = hand_Indexfinger.byteIndexfinger_H;
handSocket.handBytesFore[9] = hand_Indexfinger.byteIndexfinger_L;
handSocket.handBytesFore[10] = hand_Thumb.byteThumb_H;
handSocket.handBytesFore[11] = hand_Thumb.byteThumb_L;
// Debug.Log("触碰到的字节数::::::" + handSocket.ByteToHexStr(handSocket.handBytesFore));
handSocket.SocketSendTo(handSocket.handBytesFore);
}
}
HandSocket
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandSocket : HandUdpClient {
//手柄触碰协议
public byte[] handBytesColl;
//手柄正向锁定协议
public byte[] handBytesFore = new byte[15]; //定义好长度
//手柄释放电机
public byte[] handBytesRelax;
void Awake() {
//定义 初始化字节数 触碰协议
handBytesColl = new byte[10];
handBytesColl[0] = 0XED;
handBytesColl[1] = 0XED;
handBytesColl[2] = 0X00;
handBytesColl[3] = 0X00;
handBytesColl[4] = 0X00;
handBytesColl[5] = 0X00;
handBytesColl[6] = 0X00;
handBytesColl[7] = 0X00;
handBytesColl[8] = 0XFF;
handBytesColl[9] = 0XFF;
//释放电机
handBytesRelax = new byte[10];
handBytesRelax[0] = 0XAD;
handBytesRelax[1] = 0XAD;
handBytesRelax[2] = 0X00;
handBytesRelax[3] = 0X00;
handBytesRelax[4] = 0X00;
handBytesRelax[5] = 0X00;
handBytesRelax[6] = 0X00;
handBytesRelax[7] = 0X00;
handBytesRelax[8] = 0XFF;
handBytesRelax[9] = 0XFF;
//正向锁定协议
handBytesFore = new byte[15];
handBytesFore[0] = 0XBD;
handBytesFore[1] = 0XBD;
handBytesFore[2] = 0X00;
handBytesFore[3] = 0X00;
handBytesFore[4] = 0X00;
handBytesFore[5] = 0X00;
handBytesFore[6] = 0X00;
handBytesFore[7] = 0X00;
handBytesFore[8] = 0X00;
handBytesFore[9] = 0X00;
handBytesFore[10] = 0X00;
handBytesFore[11] = 0X00;
handBytesFore[12] = 0X00;
handBytesFore[13] = 0XFF;
handBytesFore[14] = 0XFF;
//初始化udp
InitSocket();
}
void Start () {
}
void Update () {
}
///
/// 退出
///
void OnApplicationQuit()
{
SocketQuit();
}
///
/// 发送触碰事件给手
///
public void SocketInteraction() {
handBytesColl[7] = ResultSumToByte(handBytesColl);
// handBytes[9] = ResultSumToByte(handBytes);
string str = ByteToHexStr(handBytesColl);
//Debug.Log("发送的字节数组是:::"+str);
SocketSendTo(handBytesColl);
}
///
/// 发送正向锁定协议给手柄
///
public void SocketInteraction2() {
SocketSendTo(handBytesColl);
}
///获取校验位
public byte ResultSumToByte(byte[] bytes)
{
int sum = 0;
string str;
for (int i = 2; i < bytes.Length - 3; i++)
{
str = bytes[i].ToString("X");
sum += Convert.ToInt32(str, 16);
}
Debug.Log("校验位数值:" + sum);
byte b_t = Convert.ToByte(sum);
return b_t;
}
}
HandUdpClient
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System;
public class HandUdpClient : MonoBehaviour
{
public string serverIP = "192.168.1.1";
public int port = 8899;
private Socket socket;
private EndPoint serverEnd;
private IPEndPoint ipEnd;
private string recvStr;
private string sendStr;
private byte[] recvData = new byte[1024];
private byte[] sendData = new byte[1024];
private int recvLen;
private Thread connectThread;
private int tempI;
//键指参数
public float angleX_pinkie; //尾指
public float angleY_pinkie;
public float force_pinkie;
public float angleX_ringfinger;//无名指
public float angleY_ringfinger;
public float force_ringfinger;
public float angleX_midfinger;//中指
public float angleY_midfinger;
public float force_midfinger;
public float angleX_indexfinger;//食指
public float angleY_indexfinger;
public float force_indexfinger;
public float angleX_thumb;//大拇指
public float angleY_thumb;
public float force_thumb;
//电量
public float electricity;
//对应手指的键 字节数 纵向 横向 力度
public byte[] recvDataTemp= new byte[34];
///
/// 初始化
///
public void InitSocket()
{
ipEnd = new IPEndPoint(IPAddress.Parse(serverIP), port);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
serverEnd = (EndPoint)sender;
print("Waiting for sending UDP dgram");
SocketSend("Hello World");
//SocketSendTo(new byte[15]); //0107
connectThread = new Thread(new ThreadStart(SocketReceive));
connectThread.IsBackground = true;
connectThread.Start();
}
///
/// 发送数据给服务端
///
public void SocketSend(string sendStr)
{
sendData = new byte[1024];
sendData = Encoding.ASCII.GetBytes(sendStr);
socket.SendTo(sendData, sendData.Length, SocketFlags.None, ipEnd);
}
///
/// 发送手指信息给服务端
///
public void SocketSendTo(byte[] MsgData)
{
socket.SendTo(MsgData, MsgData.Length, SocketFlags.None, ipEnd);
}
///
/// 接收服务器数据
///
public void SocketReceive()
{
while (true)
{
recvData = new byte[1024];
recvLen = socket.ReceiveFrom(recvData, ref serverEnd);
recvDataTemp = new byte[34]; //0104
for (int i = 0; i < recvData.Length; i++)
{
if (recvData.Length >= 37 && recvData[i] == 0xBD && recvData[i + 1] == 0xBD && recvData[i + 35] == 0xFF && recvData[i + 36] == 0xFF)
{
recvDataTemp = new byte[34];
tempI = i;
// print("接收服务端发来的数据:" + recvStr + "---------" + i);
for (int j = 0; j < recvDataTemp.Length; j++)
{
recvDataTemp[j] = recvData[i];
i++;
}
i = tempI;
recvStr = ByteToHexStr(recvDataTemp);
// print("接收服务端发来的数据:" + recvStr+"---------"+i);
angleX_pinkie = HexStrFloat(recvStr.Substring(4, 4)) ;
angleY_pinkie = HexStrFloat(recvStr.Substring(4 * 2, 4)) ;
force_pinkie = HexStrFloat(recvStr.Substring(4 * 3, 4)) ;
angleX_ringfinger = HexStrFloat(recvStr.Substring(4 * 4, 4));
angleY_ringfinger = HexStrFloat(recvStr.Substring(4 * 5, 4));
force_ringfinger = HexStrFloat(recvStr.Substring(4 * 6, 4));
angleX_midfinger = HexStrFloat(recvStr.Substring(4 * 7, 4));
angleY_midfinger = HexStrFloat(recvStr.Substring(4 * 8, 4));
force_midfinger = HexStrFloat(recvStr.Substring(4 * 9, 4));
angleX_indexfinger = HexStrFloat(recvStr.Substring(4 * 10, 4));
angleY_indexfinger = HexStrFloat(recvStr.Substring(4 * 11, 4));
force_indexfinger = HexStrFloat(recvStr.Substring(4 * 12, 4));
angleX_thumb = HexStrFloat(recvStr.Substring(4 * 13, 4));
angleY_thumb = HexStrFloat(recvStr.Substring(4 * 14, 4));
force_thumb = HexStrFloat(recvStr.Substring(4 * 15, 4));
electricity = HexStrFloat(recvStr.Substring(4 * 16, 4));
}
}
// recvStr = null;
// recvData = null;
// Thread.Sleep(0); //重点0107
}
}
///
/// 16进制字符串转字节数
///
public byte[] HexStrToByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += "";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(Convert.ToInt32(hexString.Substring(i * 2, 2).Trim(), 16));
}
return returnBytes;
}
///
/// 字节数组转16进制字符串
///
public string ByteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
///
/// 16进制字符串转换成浮点型
///
public float HexStrFloat(string hexStr)
{
if (hexStr == null)
return 0;
UInt32 x = Convert.ToUInt32(hexStr, 16);
float fy = x;
// fy = fy * 90 / 900;
return fy;
}
///
/// 将浮点型转换成两字节
///
public byte[] GetHightByteLowByte(float num)
{
byte[] bs2 = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)num));// 注意
// print("高字节:::" + (bs2[0].ToString("X2") + " 低字节::" + bs2[1].ToString("X2")));
return bs2;
}
///
/// 获取16进制的byte[]数组的和与和的后两位校验值
///
public byte GetCalibrationValue(byte[] dateByte)
{
byte byteTemp;
int sum = 0;
for (int j = 0; j < dateByte.Length - 1; j++)
{
string str1 = dateByte[j].ToString("x");
sum += Convert.ToInt32(str1, 16);
}
sum = sum % 256; //12251622
string sum16 = Convert.ToString(sum, 16);
int sum16Length = sum16.Length;
if (sum16Length >= 2)
{
byteTemp = (byte)Convert.ToInt32(sum16.Substring(sum16Length - 2), 16);
}
else
{
byteTemp = (byte)Convert.ToInt32((sum16), 16);
}
return byteTemp;
}
///
/// 连接关闭
///
public void SocketQuit() {
//关闭线程
if (connectThread!=null)
{
connectThread.Interrupt();
connectThread.Abort();
}
//最后关闭socket
if (socket!=null)
{
socket.Close();
}
}
}
UIManager
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
public Image fill; //背景图
public Text mText; //显示文字
public Slider slider;
private float elec;
void Update()
{
//显示数值
elec = HandManager.instance.handSocket.electricity / 1000;
// mText.text = (int)(elec/(slider.value)*100) + "%";
mText.text = Mathf.Floor((elec / (slider.value) * 100)) + "%";
// mText.text = elec + "";
if (elec > 6)
{
fill.color = Color.green;
}
else if (elec > 3 && elec <= 6)
{
fill.color = Color.yellow;
}
else
{
fill.color = Color.red;
}
}
}
Items
Bottle
public class Bottle : MonoBehaviour
{
public bool isThumbIn;
public bool isIndexIn;
///
/// 碰到
///
///
void OnTriggerEnter(Collider collision)
{
// Debug.Log("执行了碰撞事件"+ collision.transform.name);
if (collision.transform.name == "R_thumb_end")
{
isThumbIn = true;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = true;
}
Grab();
}
//抓住
void Grab()
{
if (isIndexIn&&isThumbIn)
{
gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
gameObject.transform.localPosition = new Vector3(0.0105f, 0.097f, -0.0389f);
gameObject.transform.localEulerAngles = new Vector3(-150, -100, 0);
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
}
}
///
/// 离开
///
///
void OnTriggerExit(Collider collision)
{
if (collision.transform.name== "R_thumb_end") //
{
Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
isThumbIn = false;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = false;
}
Relex();
}
//松开
void Relex()
{
if (!isIndexIn&&!isThumbIn)
{
gameObject.transform.parent = null;
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.None; //解冻所有
}
}
}
Box
public class Box : MonoBehaviour
{
public bool isThumbIn;
public bool isIndexIn;
// public TrackObject trackObject;
///
/// 碰到
///
///
void OnTriggerEnter(Collider collision)
{
// Debug.Log("执行了碰撞事件"+ collision.transform.name);
if (collision.transform.name == "R_thumb_end")
{
isThumbIn = true;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = true;
}
Grab();
}
//抓住
void Grab()
{
if (isIndexIn&&isThumbIn)
{
gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
// gameObject.transform.localPosition = new Vector3(0.0575f, 0.0886f, -0.0455f);
// gameObject.transform.localPosition = new Vector3(0.0515f, 0.106f, -0.0316f);
//gameObject.transform.localEulerAngles = new Vector3(-45f, -60f, -112f);
gameObject.transform.localPosition = new Vector3(0.0154f, 0.1241f, -0.0196f);
gameObject.transform.localEulerAngles = new Vector3(51.688f, 122.983f, -245.338f);
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
}
}
///
/// 碰撞逗留 检测碰撞所有物体的名称
///
void OnCollisionStay(Collision collision)
{
//Collider[] m_Collider = Physics.OverlapBox(transform.position, transform.GetComponent().size);
//List m_ColliderNames = new List(); //定义一个集合存贮名称
//for (int i = 0; i < m_Collider.Length; i++)
//{
// Debug.Log("被碰撞物的名称::" + m_Collider[i].transform.name);
// m_ColliderNames.Add(m_Collider[i].transform.name);
//}
//if ((m_ColliderNames.Contains("R_index_end") || m_ColliderNames.Contains("R_middle_end") || m_ColliderNames.Contains("R_ring_end") || m_ColliderNames.Contains("R_pinky_end")) && m_ColliderNames.Contains("R_thumb_end"))
//{
// gameObject.transform.GetComponent().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
//}
//else
//{
// gameObject.transform.parent = null;
// gameObject.transform.parent = null;
// gameObject.transform.GetComponent().constraints = RigidbodyConstraints.None; //解冻所有
//}
//m_ColliderNames.Clear();
// if (HandManager.instance.handSocket.force_thumb<20)
// {
// isExit=true;
//}
}
///
/// 离开
///
///
void OnTriggerExit(Collider collision)
{
if (collision.transform.name== "R_thumb_end") //
{
Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
isThumbIn = false;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = false;
}
Relex();
}
//松开
void Relex()
{
if (!isIndexIn&&!isThumbIn)
{
gameObject.transform.parent = null;
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.None; //解冻所有
// gameObject.transform.GetComponent().AddForce(-trackObject.hand.velocity,ForceMode.Impulse);
// gameObject.transform.GetComponent().AddForceAtPosition(Vector3.right- trackObject.hand.velocity, gameObject.transform.position,ForceMode.VelocityChange);
// Debug.Log("tracker速度::"+ trackObject.hand.velocity);
// gameObject.transform.GetComponent().velocity = trackObject.hand.velocity; //刚体速度=tracker速度
}
}
}
Container
public class Container : MonoBehaviour
{
public bool isThumbIn;
public bool isIndexIn;
///
/// 碰到
///
///
void OnTriggerEnter(Collider collision)
{
// Debug.Log("执行了碰撞事件"+ collision.transform.name);
if (collision.transform.name == "R_thumb_end")
{
isThumbIn = true;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = true;
}
Grab();
}
//抓住
void Grab()
{
if (isIndexIn&&isThumbIn)
{
gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
// gameObject.transform.localPosition = new Vector3(0.0575f, 0.0886f, -0.0455f);
// gameObject.transform.localPosition = new Vector3(0.0515f, 0.106f, -0.0316f);
//gameObject.transform.localEulerAngles = new Vector3(-45f, -60f, -112f);
gameObject.transform.localPosition = new Vector3(0.0154f, 0.1241f, -0.0196f);
gameObject.transform.localEulerAngles = new Vector3(51.688f, 122.983f, -245.338f);
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
}
}
///
/// 碰撞逗留 检测碰撞所有物体的名称
///
void OnCollisionStay(Collision collision)
{
//Collider[] m_Collider = Physics.OverlapBox(transform.position, transform.GetComponent().size);
//List m_ColliderNames = new List(); //定义一个集合存贮名称
//for (int i = 0; i < m_Collider.Length; i++)
//{
// Debug.Log("被碰撞物的名称::" + m_Collider[i].transform.name);
// m_ColliderNames.Add(m_Collider[i].transform.name);
//}
//if ((m_ColliderNames.Contains("R_index_end") || m_ColliderNames.Contains("R_middle_end") || m_ColliderNames.Contains("R_ring_end") || m_ColliderNames.Contains("R_pinky_end")) && m_ColliderNames.Contains("R_thumb_end"))
//{
// gameObject.transform.GetComponent().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
//}
//else
//{
// gameObject.transform.parent = null;
// gameObject.transform.parent = null;
// gameObject.transform.GetComponent().constraints = RigidbodyConstraints.None; //解冻所有
//}
//m_ColliderNames.Clear();
// if (HandManager.instance.handSocket.force_thumb<20)
// {
// isExit=true;
//}
}
///
/// 离开
///
///
void OnTriggerExit(Collider collision)
{
if (collision.transform.name== "R_thumb_end") //
{
Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
isThumbIn = false;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = false;
}
Relex();
}
//松开
void Relex()
{
if (!isIndexIn&&!isThumbIn)
{
gameObject.transform.parent = null;
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.None; //解冻所有
}
}
}
Heart
public class Heart : MonoBehaviour {
public bool isThumbIn; //拇指
public bool isIndexIn; //食指
///
/// 碰到
///
///
void OnTriggerEnter(Collider collision)
{
// Debug.Log("执行了碰撞事件"+ collision.transform.name);
if (collision.transform.name == "R_thumb_end")
{
isThumbIn = true;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = true;
}
Grab();
}
void Grab()
{
if (isIndexIn && isThumbIn)
{
HandManager.instance.isTouch = true;
gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
//gameObject.transform.localPosition = new Vector3(0.0019f, 0.1f, -0.0277f);//位置锁定
//gameObject.transform.localEulerAngles = new Vector3(0, 0, -0);
gameObject.transform.localPosition = new Vector3(0.0019f, 0.1f, -0.0277f);//位置锁定
gameObject.transform.localEulerAngles = new Vector3(-2.541f, 56.72f, 27.083f);
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
}
}
/////
///// 碰到
/////
/////
//void OnCollisionEnter(Collision collision)
//{
// // Debug.Log("执行了碰撞事件");
// if (collision.transform.tag == "Hand")
// {
// // collision.transform.GetComponent().enabled = false;
// gameObject.transform.parent = collision.transform;
// gameObject.transform.localPosition = new Vector3(0.0105f, 0.097f, -0.0389f);
// // gameObject.transform.localEulerAngles = new Vector3(-150, 100, 0);
// // gameObject.transform.GetComponent().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
// }
//}
void OnTriggerExit(Collider collision)
{
if (collision.transform.name == "R_thumb_end") //
{
Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
//collision.transform.GetComponent().enabled = false;
isThumbIn = false;
}
if (collision.transform.name == "R_index_end")
{
isIndexIn = false;
}
Relex();
}
void Relex()
{
if (!isIndexIn && !isThumbIn)
{
HandManager.instance.isTouch = false;
gameObject.transform.parent = null;
//gameObject.AddComponent();
gameObject.transform.GetComponent().constraints = RigidbodyConstraints.None; //解冻所有
}
}
}
HeartJitter
using DG.Tweening;
public class HeartJitter : MonoBehaviour {
private float timer;
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer >= 0.5f)
{
transform.DOScale(new Vector3(0.1f, 0.2f, 0.1f), 1f);
}
if (timer >= 1f)
{
transform.transform.DOScale(new Vector3(0.2f, 0.5f, 0.2f), 1f);
timer = 0;
}
// StartCoroutine(DelayShow());
}
IEnumerator DelayShow() {
transform.DOScale(new Vector3(0.005f, 0.005f, 0.005f), 0.5f);
yield return new WaitForSeconds(1f);
transform.transform.DOScale(new Vector3(1f, 1f, 1f), 1f);
}
}
Finger
public enum FingerIndex
{
Thumb,
Index,
Middle,
Ring,
Pinky,
}
public class Finger : MonoBehaviour
{
public FingerIndex index;
public GameObject fingerRoot; //持有拇指的引用
//记录旋转角度
private Quaternion fingerRotate;
private float fingerRotz; //拇指
private float fingerZchild; //拇指的子物体
private float tempFingerZ; //存放值
//拇指锁定协议
public byte byteFinger_H;
public byte byteFinger_L;
public bool isTouch_finger; //是否触碰到拇指
public float elasticFinger;//拇指的弹性变化度
public float elasticFingerTemp;//拇指的弹性变化度
public byte[] byteFinger; //存贮拇指的高字节和低字节
//public float timer=0; //计时器
public bool isExit;
void Awake()
{
fingerRotate = fingerRoot.transform.rotation;
fingerRotz = fingerRoot.transform.localEulerAngles.z;
fingerZchild = fingerRoot.transform.GetChild(0).GetChild(0).localEulerAngles.z;
tempFingerZ = fingerZchild;
}
void Start()
{
//实时获取数据同步
}
void Update()
{
}
///
/// 物理检测手指
///
void FixedUpdate()
{
Ctrl_thumb();
if (isTouch_finger)
{
// HandManager.instance.timer += Time.deltaTime;
if (HandManager.instance.timer >= 0f && HandManager.instance.timer < 1f)
{
//elasticThumb = elasticThumbTemp + 50;
elasticFinger += 5;
Debug.Log("0.5f拇指" + elasticFinger);
}
if (HandManager.instance.timer >= 1f && HandManager.instance.timer < 2f)
{
//elasticThumb = elasticThumbTemp - 50;
elasticFinger -= 5;
Debug.Log("1f拇指:" + elasticFinger);
}
//if (HandManager.instance.timer >=1f)
//{
// HandManager.instance.timer = 0;
// Debug.Log("拇指时间归零");
//}
byteFinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticFinger);
byteFinger_H = byteFinger[0];
byteFinger_L = byteFinger[1];
}
else
{
// byteThumb_H = 0X00;
// byteThumb_L = 0X00;
}
}
///
/// 进入触发器
///
///
void OnTriggerEnter(Collider coll)
{
//if (coll.gameObject.name == Tags.Sphere)
//if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name != null)
{
if (coll.gameObject.tag == "Heart")
{
isTouch_finger = true;
elasticFinger = HandManager.instance.handSocket.angleX_thumb;
elasticFingerTemp = elasticFinger;
}
else
{
byteFinger_H = HandManager.instance.handSocket.recvDataTemp[26];
byteFinger_L = HandManager.instance.handSocket.recvDataTemp[27];
}
if (coll.gameObject.name == "R_thumb_end")
{
isExit = false;
}
}
}
///
/// 离开触发器
///
///
void OnTriggerExit(Collider coll)
{
// if (coll.gameObject.name == Tags.Sphere)
// if (coll.gameObject.name == Tags.Cylinder)
if (coll.gameObject.name != null)
{
byteFinger_H = 0X00;
byteFinger_L = 0X00;
isTouch_finger = false;
}
if (coll.gameObject.name == "R_thumb_end")
{
isExit = true;
}
}
void OnApplicationQuit()
{
fingerRoot.transform.rotation = fingerRotate;
}
///
/// 拇指
///
protected virtual void Ctrl_thumb()
{
//thumb.transform.localEulerAngles = new Vector3(thumb.transform.localEulerAngles.x, Map(angleY_thumb, 693, 769, -35, 40), MapData(angleX_thumb, 400, 730, -26.361f, -115.18f));
//fingerRoot.transform.localEulerAngles = new Vector3(fingerRoot.transform.localEulerAngles.x, fingerRoot.transform.localEulerAngles.y, MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f) + 25);
//if (fingerRoot.transform.localEulerAngles.z - 800 < fingerRotz)
//{
// tempFingerZ = MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f);
//}
//else
//{
// tempFingerZ = fingerZchild;
//}
//fingerRoot.transform.GetChild(0).localEulerAngles = new Vector3(fingerRoot.transform.GetChild(0).localEulerAngles.x, fingerRoot.transform.GetChild(0).localEulerAngles.y, tempFingerZ + 45);
// str = "Y轴: " + MapData(angleY_thumb, 693, 769, -35, 40) + " Z轴::" + MapData(angleX_thumb, 400, 730, -26.361f, -115.18f);
}
public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
{
if (min_in > max_in)
{
min_in = min_in + max_in;
max_in = min_in - max_in;
min_in = min_in - max_in;
min_out = min_out + max_out;
max_out = min_out - max_out;
min_out = min_out - max_out;
}
if (value < min_in)
value = min_in;
if (value > max_in)
value = max_in;
return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
}
}
GrabableObj
public class GrabableObj : MonoBehaviour {
public bool isLockPos;
public Vector3 lockPos;
public bool isLockRot;
public Vector3 lockRot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Grab()
{
if (isLockPos)
{
transform.localPosition = lockPos;
}
if (isLockRot)
{
transform.localEulerAngles = lockRot;
}
}
}
heartAni
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class heartAni : MonoBehaviour {
// Use this for initialization
void Start () {
}
public void Bigger()
{
HandManager.instance.timer = 0;
}
public void Smaller()
{
HandManager.instance.timer = 1;
}
}
TrackObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrackObject : MonoBehaviour {
public SteamVR_Controller.Device hand;
public float speed = 2; //速度值随意设
// SteamVR_TrackedObject trackObject;
void Start () {
// trackObject = this.GetComponent();
var rightHands = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost); //获得右手柄
hand =SteamVR_Controller.Input(rightHands);
}
void Update () {
//SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackObject.index);
//if (device.GetHairTrigger())
//{
// Debug.Log("GetHairTrigger");
//}
//if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
//{ //.Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger
// Debug.Log("ExportSteamVR_Trigger");
//}
}
void FixedUpdate()
{//方法一:
if (hand.velocity.magnitude > speed)
{
// Debug.Log("手柄速度:::::"+ hand.velocity.magnitude);
}
//方法二
//if (Vector3.Distance(hand.velocity, Vector3.zero) > speed)
//{
// Debug.Log("手柄速度");
//}
}
}