您的位置:首页 > 移动开发 > Unity3D

Unity手机触屏输入

2022-04-23 23:09 2779 查看

#1、获取屏幕输入 新建脚本

TouchInput
,添加到MainCamera(主摄像机)上

public class TouchInput : MonoBehaviour
{
public LayerMask touchInputMask;  //声明层级,射线只与设定的层级进行检测
private Camera myCamera;          //声明摄像机

private List<GameObject> touchList = new List<GameObject>();    //保存当前按下的物体,需要用List进行动态添加
private GameObject[] touchesOld; //保存上一次按下的物体,不需要动态添加,与当前按下的物体比较,判断哪些物体取消了点击
private RaycastHit hit;
void Start()
{
myCamera = GetComponent<Camera>();
}
void Update()
{
if (Input.touchCount > 0)   //如果存在触屏输入
{
//初始化touchesOld,并保存上一帧按下的所有按键
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();  //清空列表,用于获取新的输入,保存当前按下的物体

foreach (Touch touch in Input.touches)  //遍历所有的屏触屏输入
{
Ray ray = myCamera.ScreenPointToRay(touch.position);    //从触屏的地方发射射线

if (Physics.Raycast(ray, out hit, touchInputMask))
{
GameObject recipient = hit.transform.gameObject;    //获取射线碰撞的物体
touchList.Add(recipient);     //将该物体加入当前触碰列表

if (touch.phase == TouchPhase.Began)    //判断触屏的类型,如果为刚刚接触屏幕
{
//发送委托,调用物体OnTouchDown的方法
recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Ended)    //判断触屏的类型,如果手指离开屏幕
{
//发送委托,调用物体OnTouchUp的方法
recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
}
//如果长按屏幕或在屏幕上滑动
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
{
//发送委托,调用物体OnTouchStay的方法
recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Canceled) //如果手指数量超过追踪上线,或用脸等平面接触屏幕
{
//发送委托,调用物体OnTouchExit的方法
recipient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld)    //遍历上一帧点击的物体
{
if (!touchList.Contains(g))         //如果列表中不包含上一帧的物体
{
//那么委托调用物体的OnTouchExit方法,该物已经取消点击
g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
}
}

新建脚本

Button

public class Button : MonoBehaviour
{
public Color defaultColor;	//默认颜色
public Color selectedColor;	//点击后的颜色
private Material mat;
private void Start()
{
mat = gameObject.GetComponent<Renderer>().material;
}
private void OnTouchDown()
{
mat.color = selectedColor;
}
private void OnTouchUp()
{
mat.color = defaultColor;
}
private void OnTouchStay()
{
mat.color = selectedColor;
}
private void OnTouchExit()
{
mat.color = defaultColor;
}
}

添加几个Cube,将

Button
添加到Cube上 设置每个Cube的颜色 Build到手机后的运行结果

#2、拖动效果 这里的Slider并不是UnityUI中的Slider

是使用基本物体拼装成的Slider

这里使用了3D物体中的Quad和Cube 然后可以在 Slider上创建一个BoxCollider 来限制Button的移动

新建脚本

Slider

public class Slider : MonoBehaviour
{
public Transform button;
private Vector3 targetPos;
private void Start()
{
targetPos = button.position;
}
private void Update()
{
button.position = Vector3.Lerp(button.position, targetPos, Time.deltaTime * 10f);
}
void OnTouchStay(Vector3 point)
{
targetPos = new Vector3(point.x, targetPos.y, targetPos.z);
}
}

然后将

Button
添加到Button上

运行后就能拖动Button

接下来是显示数据,添加一个3DText设为Slider的子物体,并调整到合适的位置和大小

新建脚本

Slider
,添加到Slider上

public class Slider : MonoBehaviour
{
public Transform knbo;
public TextMesh textMesh;
private Vector3 targetPos;

private float sliderPercent;
private float sliderLength;

private void Start()
{
sliderLength = gameObject.GetComponent<BoxCollider>().size.x - 0.4f;
targetPos = knbo.position;
}
private void Update()
{
knbo.position = Vector3.Lerp(knbo.position, targetPos, Time.deltaTime * 10f);
//计算slider的比例
sliderPercent = Mathf.Clamp01((knbo.localPosition.x + sliderLength / 2) / sliderLength);
//保留一位小数
textMesh.text = string.Format(sliderName + ": " + "{0:F1}", sliderPercent);
}
public float GetSliderPercent()
{
return sliderPercent;
}
void OnTouchStay(Vector3 point)
{
targetPos = new Vector3(point.x, targetPos.y, targetPos.z);
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: