unity中手机触摸代码
#elif UNITY_IOS || UNITY_ANDROID
if(Input.touchCount <= 0)
{
return;//强制退出 不执行以下有关触摸的代码
}
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
previousPositionX = Input.GetTouch(0).position.x;
startAddSpeed = false;
startAddSpeed_reduce = false;
startReduceSpeed = false;
ShowSpeed = 15f;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
offset = Input.GetTouch(0).position.x - previousPositionX;
if (Input.GetTouch(0).deltaPosition.x > 0)
{
ShowSpeed = 55f;
}
else if (Input.GetTouch(0).deltaPosition.x < 0)
{
ShowSpeed = -30f;
}
previousPositionX = Input.GetTouch(0).position.x;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (ShowSpeed < 15)
{
startAddSpeed = true;
startAddSpeed_reduce = true;
}
else if (ShowSpeed > 15)
{
startReduceSpeed = true;
}
mouseCurrentPos = Input.GetTouch(0).position;
mouseIsMove = false;
}
if(Input.touchCount <= 0)
{
return;//强制退出 不执行以下有关触摸的代码
}
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
previousPositionX = Input.GetTouch(0).position.x;
startAddSpeed = false;
startAddSpeed_reduce = false;
startReduceSpeed = false;
ShowSpeed = 15f;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
offset = Input.GetTouch(0).position.x - previousPositionX;
if (Input.GetTouch(0).deltaPosition.x > 0)
{
ShowSpeed = 55f;
}
else if (Input.GetTouch(0).deltaPosition.x < 0)
{
ShowSpeed = -30f;
}
previousPositionX = Input.GetTouch(0).position.x;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (ShowSpeed < 15)
{
startAddSpeed = true;
startAddSpeed_reduce = true;
}
else if (ShowSpeed > 15)
{
startReduceSpeed = true;
}
mouseCurrentPos = Input.GetTouch(0).position;
mouseIsMove = false;
}