Unity指が触れる方向(片手)
4811 ワード
最近、クールなゲームを書いて、中の知識点をまとめました:O(∩∩)O~
using UnityEngine;
using System.Collections;
public class Demo : MonoBehaviour
{
public Vector3 lastMonseDown;
/// <summary>
///
/// </summary>
/// <returns></returns>
TouchDir GetTouchDir()
{
//
if (Input.GetMouseButtonDown(0))
{
lastMonseDown = Input.mousePosition;
}
//UICamera.hoveredObject NGUI
if (Input.GetMouseButtonUp(0) && UICamera.hoveredObject == null)
{
// -
Vector3 mouseUp = Input.mousePosition;
Vector3 touchOffset = mouseUp - lastMonseDown;
// 50 ,
if (Mathf.Abs(touchOffset.x) > 50 || Mathf.Abs(touchOffset.y) > 50)
{
if (Mathf.Abs(touchOffset.x) > Mathf.Abs(touchOffset.y) && touchOffset.x > 0)
{
return TouchDir.Right;
}
else if (Mathf.Abs(touchOffset.x) > Mathf.Abs(touchOffset.y) && touchOffset.x < 0)
{
return TouchDir.Left;
}
else if (Mathf.Abs(touchOffset.x) < Mathf.Abs(touchOffset.y) && touchOffset.y > 0)
{
return TouchDir.Top;
}
else if (Mathf.Abs(touchOffset.x) < Mathf.Abs(touchOffset.y) && touchOffset.y < 0)
{
return TouchDir.Bottom;
}
}
else
{
return TouchDir.None;
}
}
return TouchDir.None;
}
}
/// <summary>
///
/// </summary>
public enum TouchDir
{
None,
Left,
Right,
Top,
Bottom
}