using System.Collections; using System.Collections.Generic; using UnityEngine; using YHElectric; public class InputController : MonoBehaviour { [HideInInspector] public bool inputMouseKey0 = false; [HideInInspector] public bool inputMouseKeyDown0 = false; [HideInInspector] public bool inputMouseKey1 = false; [HideInInspector] public bool inputMouseKeyDown1 = false; [HideInInspector] public bool inputMouseKey2 = false; [HideInInspector] public bool inputMouseKeyDown2 = false; [HideInInspector] public static bool inputDoubleClick = false; [HideInInspector] public bool inputKeyShift = false; [HideInInspector] public bool inputKeySpace = false; [HideInInspector] public bool inputKeyW = false; [HideInInspector] public bool inputKeyA = false; [HideInInspector] public bool inputKeyS = false; [HideInInspector] public bool inputKeyD = false; [HideInInspector] public bool inputKeyF = false; [HideInInspector] public bool inputKeyQ = false; [HideInInspector] public bool inputKeyE = false; [HideInInspector] public bool inputKeyESC = false; [HideInInspector] public float inputMouseX = 0; [HideInInspector] public float inputMouseY = 0; [HideInInspector] public float inputMoveX = 0; [HideInInspector] public float inputMoveY = 0; [HideInInspector] public float inputMouseWheel = 0; [HideInInspector] public float udpRotX = 0; [HideInInspector] public float udpRotY = 0; [HideInInspector] public float udpMoveX = 0; [HideInInspector] public float udpMoveY = 0; private Vector3 mouseStart; Vector3 offset; float offset1; private Touch oldTouch1; //上次触摸点1(手指1) private Touch oldTouch2; //上次触摸点2(手指2) bool twoToOne = false; float timer = 0.5f; private void Update() { inputDoubleClick = false; inputKeyW = Input.GetKey("w"); inputKeyA = Input.GetKey("a"); inputKeyS = Input.GetKey("s"); inputKeyD = Input.GetKey("d"); inputKeyF = Input.GetKey("f"); inputKeyQ = Input.GetKey("q"); inputKeyE = Input.GetKey("e"); inputMouseKey0 = Input.GetKey("mouse 0"); inputMouseKey1 = Input.GetKey("mouse 1"); inputMouseKey2 = Input.GetKey("mouse 2"); inputMouseKeyDown0 = Input.GetKeyDown("mouse 0"); inputMouseKeyDown1 = Input.GetKeyDown("mouse 1"); inputMouseKeyDown2 = Input.GetKeyDown("mouse 2"); //inputMouseX = Input.GetAxisRaw("Mouse X"); //inputMouseY = Input.GetAxisRaw("Mouse Y"); //inputMouseWheel = Input.GetAxisRaw("Mouse ScrollWheel"); inputMouseWheel = offset1; inputKeyShift = Input.GetKey("left shift"); inputKeySpace = Input.GetKey("space"); inputKeyESC = Input.GetKey("escape"); if (HaveClickTwice(0.3f, ref twiceTime)) { inputDoubleClick = true; } timer -= Time.deltaTime; if (inputMouseKeyDown0) { mouseStart = Input.mousePosition; } if (Input.touchCount == 1&&timer<0) { offset = Input.mousePosition - mouseStart; inputMouseX = offset.x; inputMouseY = offset.y; mouseStart = Input.mousePosition; } else if (Input.touchCount == 2) { timer = 0.4f; Touch newTouch1 = Input.GetTouch(0); Touch newTouch2 = Input.GetTouch(1); //第2点刚开始接触屏幕, 只记录,不做处理 if (newTouch2.phase == TouchPhase.Began) { oldTouch2 = newTouch2; oldTouch1 = newTouch1; return; } //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型 float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position); float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position); //两个距离之差,为正表示放大手势, 为负表示缩小手势 offset1 = newDistance - oldDistance; oldTouch1 = newTouch1; oldTouch2 = newTouch2; mouseStart = newTouch2.position; } //else if (Input.touchCount == 3) //{ // CameraOperate.controlType = ControlType.Move; // offset = Input.mousePosition - mouseStart; // inputMoveX = offset.x; // inputMoveY = offset.y; // mouseStart = Input.mousePosition; //} else { offset1 = 0; } } float twiceTime; /// /// 鼠标双击判断 /// /// /// /// bool HaveClickTwice(float offsetTime, ref float timer) { if (inputMouseKeyDown0) return HaveExecuteTwiceAtTime(offsetTime, ref timer); else return false; } static bool HaveExecuteTwiceAtTime(float offsetTime, ref float timer) { if (Time.time - timer < offsetTime) return true; else { timer = Time.time; return false; } } }