我有一款主要用于PC的游戏。但对于那些有表面专业版或其他平板电脑运行Windows并且是触摸屏的人,我想知道是否需要添加一些额外的代码:
public GameObject thingToMove;
public float smooth = 2;
private Vector3 _endPosition;
private Vector3 _startPosition;
private void Awake() {
_startPosition = thingToMove.transform.position;
}
private Vector3 HandleTouchInput() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
var screenPosition = Input.GetTouch(i).position;
_startPosition = Camera.main.ScreenToWorldPoint(screenPosition); } }
return _startPosition; }
private Vector3 HandleMouseInput() {
if(Input.GetMouseButtonDown(0)) {
var screenPosition = Input.mousePosition;
_startPosition = Camera.main.ScreenToWorldPoint(screenPosition); }
return _startPosition; }
这是我的播放器正常移动的方式,但是..对于触摸屏选项,我已将其添加到:
public void Update() {
if (Application.platform == RuntimePlatform. || Application.platform == RuntimePlatform.IPhonePlayer) {
_endPosition = HandleTouchInput(); }
else {
_endPosition = HandleMouseInput(); }
thingToMove.transform.position = Vector3.Lerp(thingToMove.transform.position, new Vector3(transform.position.x, _endPosition.y, 0), Time.deltaTime * smooth);
}
RuntimePlatform.
的位置..我使用哪种设备用于触摸屏Windows设备?这会解决我的问题吗?
答案 0 :(得分:1)
要检测触摸屏设备,请考虑使用SystemInfo.deviceType
而不是检查每一个可能的RuntimePlatform
:
if (SystemInfo.deviceType == DeviceType.Handheld) {
_endPosition = HandleTouchInput();
}
else {
_endPosition = HandleMouseInput();
}
如果你绝对需要知道它是否是Surface Pro,你可以尝试将其与Application.platform
结合使用:
if (SystemInfo.deviceType == DeviceType.Handheld) {
_endPosition = HandleTouchInput();
if (Application.platform == RuntimePlatform.WindowsPlayer){
// (Probably) a Surface Pro/some other Windows touchscreen device?
}
}
else {
_endPosition = HandleMouseInput();
}
希望这有帮助!如果您有任何问题,请告诉我。
(我并非100%确定Unity是否能够将{@ 1}}或DeviceType.Handheld
正确处理Surface Pro,但它绝对值得尝试。)