2015-10-16 106 views
-1

我有一款主要用於PC的遊戲。但是,對於那些誰擁有表面親或運行Windows,並且觸摸屏的其它平板電腦,我想知道如果我需要一些額外的代碼添加到這一點:但是如何檢測遊戲在Su​​rface Pro上運行的時間?

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設備?這會解決我的問題嗎?

+2

甚至解決在此之前,我建議你閱讀一些[代碼約定( https://msdn.microsoft.com/en-us/library/ff926074.aspx)關於C#。 –

回答

1

爲了檢測觸摸屏設備的,可以考慮使用SystemInfo.deviceType,而不是檢查,對每一個可能的RuntimePlatform

if (SystemInfo.deviceType == DeviceType.Handheld) { 
    _endPosition = HandleTouchInput(); 
} 
else { 
    _endPosition = HandleMouseInput(); 
} 

如果你絕對需要知道,如果它是一個面臨,你可以嘗試結合與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%肯定,如果統一將妥善處理曲面Pro作爲一個DeviceType.HandheldDeviceType.Desktop,但它絕對值得一試。)

+0

啊,謝謝你,這是我需要的。這並不是說我需要Surface Pro,只是爲了確保我完全兼容運行Windows的觸摸屏設備。非常感謝。 – JGrn84

+0

太好了,我很高興我可以幫你。 – Serlite