抖音开放平台Logo
开发者文档
控制台

PC玩法适配移动端输入(Unity)

收藏
我的收藏

Unity 相关 API​

API​
在 PC 上操作时,接口的表现​
通过移动端接入时,接口的表现​
Input.touchSupported
返回 false​
返回 true​
Input.touchCount
返回 0​
返回 按下手指的数量​
Input.touches
空数组​
返回 按下手指对应的 Touch 结构体数组​
Input.mousePosition
返回鼠标位置,屏幕空间​
返回手指位置,屏幕空间​
多指触控情况下,返回中心位置​
Input.GetMouseButtonDown(0)
鼠标左键按下时返回 true​
手指按下时返回 true​
Input.GetMouseButtonUp(0)
鼠标左键抬起时返回 true​
手指抬起时返回 true​
Input.GetMouseButton(0)
鼠标左键持续按下时返回 true​
手指持续按下时返回 true​
Input.GetAxis("Mouse X")
返回鼠标移动距离​
返回值与手指移动距离不匹配​

点击拖拽能力​

代码参考:​
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; //安卓 PC都好用 点击物体移动 /// <summary> /// 物体上要有collider /// 场景中要存在EventSystem /// </summary> [RequireComponent(typeof(RectTransform))] public class EventClick : MonoBehaviour, IPointerClickHandler { public Text text; private bool isDrag = false; //偏移量 private Vector3 offset = Vector3.zero; public void OnPointerClick(PointerEventData eventData) { text.text="点到我啦!!!"; } IEnumerator OnMouseDown() { //将物体由世界坐标系转化为屏幕坐标系,由vector3 结构体变量ScreenSpace存储,以用来明确屏幕坐标系Z轴的位置 Vector3 ScreenSpace = Camera.main.WorldToScreenPoint(transform.position); //完成了两个步骤,1由于鼠标的坐标系是2维的,需要转化成3维的世界坐标系,2只有三维的情况下才能来计算鼠标位置与物体的距离,offset即是距离 Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenSpace.z)); Debug.Log("down"); //当鼠标左键按下时 while (Input.GetMouseButton(0)) { //得到现在鼠标的2维坐标系位置 Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenSpace.z); //将当前鼠标的2维位置转化成三维的位置,再加上鼠标的移动量 Vector3 CurPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset; //CurPosition就是物体应该的移动向量赋给transform的position属性 transform.position = CurPosition; yield return new WaitForFixedUpdate(); } } }

双指放大缩放旋转物体​

代码参考:​
using System.Collections; using System.Collections.Generic; using UnityEngine; public enum FingerIE { zero, OneFinger, TwoFinger, } public class Demo : MonoBehaviour { public Vector3 initialRot; public Vector3 initialSca; public static Demo instance; IEnumerator ie; FingerIE finger_num = FingerIE.zero; void Awake() { instance = this; } // Update is called once per frame void Update() { if (Input.touchCount == 0) { if (finger_num != FingerIE.zero) { StopCoroutine(ie); ie = null; finger_num = FingerIE.zero; } } else if (Input.touchCount == 1) { if (finger_num != FingerIE.OneFinger) { if (ie != null) { StopCoroutine(ie); } ie = IMonitorMouseOneFinger(); StartCoroutine(ie); finger_num = FingerIE.OneFinger; } } else if (Input.touchCount == 2) { if (finger_num != FingerIE.TwoFinger) { if (ie != null) { StopCoroutine(ie); } ie = IIMonitorMouseTwoFinger(); StartCoroutine(ie); finger_num = FingerIE.TwoFinger; } } } /// <summary> /// 一根手指控制转动 /// </summary> /// <returns></returns> IEnumerator IMonitorMouseOneFinger() { //单手操作 Touch oneFingerTouch; while (true) { oneFingerTouch = Input.GetTouch(0); if (oneFingerTouch.phase == TouchPhase.Moved) { Vector2 deltaPos = oneFingerTouch.deltaPosition; transform.Rotate(-Vector3.up * deltaPos.x * 0.2f, Space.World); transform.Rotate(-Vector3.left * deltaPos.y * 0.2f, Space.World); } yield return 0; } } /// <summary> /// 两个手指控制缩放 /// </summary> /// <returns></returns> IEnumerator IIMonitorMouseTwoFinger() { Touch firstOldTouch; Touch secondOldTouch; Touch firstNewTouch; Touch secondNewTouch; float oldDistance; float newDistance; while (true) { firstOldTouch = Input.GetTouch(0); secondOldTouch = Input.GetTouch(1); oldDistance = Vector2.Distance(firstOldTouch.position, secondOldTouch.position); yield return 0; firstNewTouch = Input.GetTouch(0); secondNewTouch = Input.GetTouch(1); newDistance = Vector2.Distance(firstNewTouch.position, secondNewTouch.position); if (oldDistance > newDistance && this.transform.localScale.x > 0.25f) { this.transform.localScale -= Vector3.one * 0.1f; } else if (oldDistance < newDistance && this.transform.localScale.x < 2f) { this.transform.localScale += Vector3.one * 0.1f; } } } /// <summary> /// 复位 /// </summary> public void ResetRot() { this.transform.localEulerAngles = initialRot; this.transform.localScale = initialSca; } }