Convert UnityEngine.Vector3 to UnityEngine.Touch o

2019-08-19 20:24发布

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);

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?

标签: unity3d
1条回答
Viruses.
2楼-- · 2019-08-19 21:06

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;
查看更多
登录 后发表回答