我有一个游戏,主要是为个人电脑。但是对于那些拥有表面专业版或其他平板电脑的人来说,这些平板电脑运行的是触摸屏,我想知道是否需要添加一些额外的代码:
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设备使用哪种设备?这能解决我的问题吗?
发布于 2015-10-16 13:47:53
要检测触摸屏设备,请考虑使用SystemInfo.deviceType,而不是检查每一个可能的RuntimePlatform。
if (SystemInfo.deviceType == DeviceType.Handheld) {
_endPosition = HandleTouchInput();
}
else {
_endPosition = HandleMouseInput();
}如果您绝对需要知道它是否是Surface,您可以尝试将它与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();
}希望这能有所帮助!如果你有任何问题,请告诉我。
(我不确定Unity是否会正确地将Surface作为DeviceType.Handheld或DeviceType.Desktop来处理,但这绝对值得一试。)
https://stackoverflow.com/questions/33169573
复制相似问题