浏览代码

Merge branch 'material' into 'master'

support touch input on ios/android mobile device

See merge request upm-packages/ui-widgets/com.unity.uiwidgets!82
/main
Shenhua Gu 6 年前
当前提交
93bca2f7
共有 3 个文件被更改,包括 94 次插入23 次删除
  1. 69
      Runtime/engine/WidgetCanvas.cs
  2. 37
      Runtime/engine/input_utils.cs
  3. 11
      Runtime/engine/input_utils.cs.meta

69
Runtime/engine/WidgetCanvas.cs


Vector2 _lastMouseMove;
bool _mouseEntered;
const int mouseButtonNum = 3;
const int mouseScrollId = mouseButtonNum + 1;
const int mouseButtonNum = 3;
//Disable touch -> mouse event on mobile devices
Input.simulateMouseWithTouches = false;
this._displayMetrics = DisplayMetricsProvider.provider();
if (_repaintEvent == null) {

this.unfocusIfNeeded();
}
#if UNITY_IOS || UNITY_ANDROID
if (this._mouseEntered && Input.touchCount != 0) {
this.handleTouchMove();
}
#else
#endif
this._lastMouseMove = Input.mousePosition;
this._lastMouseMove = Input.mousePosition;
D.assert(this._windowAdapter != null);
this._windowAdapter.Update();

}
}
void handleTouchMove() {
for (var touchIndex = 0; touchIndex < Input.touchCount; touchIndex++) {
var touchEvent = Input.GetTouch(touchIndex);
if (touchEvent.phase != TouchPhase.Moved) {
continue;
}
var pos = this.getPointPosition(touchEvent.position);
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.hover,
kind: PointerDeviceKind.touch,
device: InputUtils.getTouchFingerKey(touchEvent.fingerId),
physicalX: pos.x,
physicalY: pos.y
));
}
}
void handleMouseMove() {
var pos = this.getPointPosition(Input.mousePosition);
this._windowAdapter.postPointerEvent(new PointerData(

Input.mouseScrollDelta.y * scaleFactor,
pos.x,
pos.y,
this.getScrollButton());
InputUtils.getScrollButtonKey());
}
var deltaScroll = this._scrollInput.getScrollDelta();

}
}
int getScrollButton() {
return mouseScrollId;
}
return key;
return InputUtils.getMouseButtonKey(key);
}
}

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.down,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
kind: InputUtils.getPointerDeviceKind(),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y
));

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.up,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
kind: InputUtils.getPointerDeviceKind(),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y
));

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.move,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
kind: InputUtils.getPointerDeviceKind(),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y
));

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
kind: InputUtils.getPointerDeviceKind(),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y
));

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
kind: InputUtils.getPointerDeviceKind(),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y
));

37
Runtime/engine/input_utils.cs


using Unity.UIWidgets.ui;
using UnityEngine.EventSystems;
namespace Unity.UIWidgets.engine {
public static class InputUtils {
const int mouseScrollId = 1;
const int preservedPointerKeyNum = 10;
public static PointerDeviceKind getPointerDeviceKind() {
#if UNITY_IOS || UNITY_ANDROID
return PointerDeviceKind.touch;
#else
return PointerDeviceKind.mouse;
#endif
}
public static int getPointerDeviceKey(PointerEventData eventData) {
#if UNITY_IOS || UNITY_ANDROID
return getTouchFingerKey(eventData.pointerId);
#else
return getMouseButtonKey((int) eventData.button);
#endif
}
public static int getScrollButtonKey() {
return mouseScrollId;
}
public static int getMouseButtonKey(int buttonId) {
return buttonId + preservedPointerKeyNum;
}
public static int getTouchFingerKey(int fingerId) {
return fingerId + preservedPointerKeyNum;
}
}
}

11
Runtime/engine/input_utils.cs.meta


fileFormatVersion: 2
guid: 59b547839c038453d82019f8bef7d407
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存