将UnityEngine.Vector3转换为UnityEngine.Touch或鼠标输入 [英] Convert UnityEngine.Vector3 to UnityEngine.Touch or mouse input

查看:431
本文介绍了将UnityEngine.Vector3转换为UnityEngine.Touch或鼠标输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在桌面以及触摸/平板电脑环境中测试我的脚本.因此,我需要在脚本中将mouseinputs转换为Touch.我试过了:

I want to test my Script in Desktop as well as touch/tablet environment. Therefore I need to convert mouseinputs into Touch in my script. I tried:

        var screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

        Touch touchZero = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

它说不能隐式转换类型UnityEngine.Vector3' to UnityEngine.Touch'

It says Cannot implicitly convert type UnityEngine.Vector3' toUnityEngine.Touch'

是否有机会以某种方式转换类型,所以我不必每次都写两次?

Any chance to convert the type somehow so I do not have to write everything twice?

推荐答案

您可以像这样通过鼠标输入来构建触摸:

You can build a touch from mouse input like this:

Touch fakeTouch = new Touch ();
fakeTouch.fingerId = 10;
fakeTouch.position = Input.mousePosition;
fakeTouch.deltaTime = Time.deltaTime;
fakeTouch.deltaPosition = Input.mousePosition - lastMousePosition;
fakeTouch.phase = (Input.GetMouseButtonDown (0) ? TouchPhase.Began :
                  (fakeTouch.deltaPosition.sqrMagnitude > 1f ? 
                  TouchPhase.Moved : TouchPhase.Stationary));
fakeTouch.tapCount = 1;

这篇关于将UnityEngine.Vector3转换为UnityEngine.Touch或鼠标输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆