Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit f33e744

Browse files
committed
fix mouse position bug when canvas.renderMode == ScreenSpaceCamera (https://answers.unity.com/questions/849117/46-ui-image-follow-mouse-position.html)
1 parent 0916450 commit f33e744

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

Runtime/engine/UIWidgetsPanel.cs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,30 @@ void OnGUI() {
240240

241241
void handleMouseMovement() {
242242
var pos = this.getPointPosition(Input.mousePosition);
243+
if (pos == null) {
244+
return;
245+
}
243246
this._windowAdapter.postPointerEvent(new PointerData(
244247
timeStamp: Timer.timespanSinceStartup,
245248
change: PointerChange.hover,
246249
kind: PointerDeviceKind.mouse,
247250
device: this.getMouseButtonDown(),
248-
physicalX: pos.x,
249-
physicalY: pos.y
251+
physicalX: pos.Value.x,
252+
physicalY: pos.Value.y
250253
));
251254
}
252255

253256
void handleMouseScroll() {
254257
if (Input.mouseScrollDelta.y != 0 || Input.mouseScrollDelta.x != 0) {
255258
var scaleFactor = this.canvas.scaleFactor;
256259
var pos = this.getPointPosition(Input.mousePosition);
260+
if (pos == null) {
261+
return;
262+
}
257263
this._windowAdapter.onScroll(Input.mouseScrollDelta.x * scaleFactor,
258264
Input.mouseScrollDelta.y * scaleFactor,
259-
pos.x,
260-
pos.y,
265+
pos.Value.x,
266+
pos.Value.y,
261267
InputUtils.getScrollButtonKey());
262268
}
263269
}
@@ -276,31 +282,41 @@ int getMouseButtonDown() {
276282
}
277283

278284
public void OnPointerDown(PointerEventData eventData) {
279-
EventSystem.current.SetSelectedGameObject(this.gameObject, eventData);
280285
var position = this.getPointPosition(eventData);
286+
if (position == null) {
287+
return;
288+
}
289+
EventSystem.current.SetSelectedGameObject(this.gameObject, eventData);
281290
this._windowAdapter.postPointerEvent(new PointerData(
282291
timeStamp: Timer.timespanSinceStartup,
283292
change: PointerChange.down,
284293
kind: InputUtils.getPointerDeviceKind(eventData),
285294
device: InputUtils.getPointerDeviceKey(eventData),
286-
physicalX: position.x,
287-
physicalY: position.y
295+
physicalX: position.Value.x,
296+
physicalY: position.Value.y
288297
));
289298
}
290299

291300
public void OnPointerUp(PointerEventData eventData) {
292301
var position = this.getPointPosition(eventData);
302+
if (position == null) {
303+
return;
304+
}
293305
this._windowAdapter.postPointerEvent(new PointerData(
294306
timeStamp: Timer.timespanSinceStartup,
295307
change: PointerChange.up,
296308
kind: InputUtils.getPointerDeviceKind(eventData),
297309
device: InputUtils.getPointerDeviceKey(eventData),
298-
physicalX: position.x,
299-
physicalY: position.y
310+
physicalX: position.Value.x,
311+
physicalY: position.Value.y
300312
));
301313
}
302314

303-
public Vector2 getPointPosition(PointerEventData eventData) {
315+
public Vector2? getPointPosition(PointerEventData eventData) {
316+
if (eventData.enterEventCamera == null && this.canvas.renderMode == RenderMode.ScreenSpaceCamera) {
317+
return null;
318+
}
319+
304320
Vector2 localPoint;
305321
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, eventData.position,
306322
eventData.enterEventCamera, out localPoint);
@@ -310,14 +326,17 @@ public Vector2 getPointPosition(PointerEventData eventData) {
310326
return localPoint;
311327
}
312328

313-
public Vector2 getPointPosition(Vector2 position) {
329+
public Vector2? getPointPosition(Vector2 position) {
314330
Vector2 localPoint;
315331
Camera eventCamera = null;
316332

317333
if (this.canvas.renderMode != RenderMode.ScreenSpaceCamera) {
318334
eventCamera = this.canvas.GetComponent<GraphicRaycaster>().eventCamera;
319335
}
320336

337+
if (eventCamera == null && this.canvas.renderMode == RenderMode.ScreenSpaceCamera) {
338+
return null;
339+
}
321340

322341
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, position,
323342
eventCamera, out localPoint);
@@ -329,44 +348,52 @@ public Vector2 getPointPosition(Vector2 position) {
329348

330349
public void OnDrag(PointerEventData eventData) {
331350
var position = this.getPointPosition(eventData);
351+
if (position == null) {
352+
return;
353+
}
332354
this._windowAdapter.postPointerEvent(new PointerData(
333355
timeStamp: Timer.timespanSinceStartup,
334356
change: PointerChange.move,
335357
kind: InputUtils.getPointerDeviceKind(eventData),
336358
device: InputUtils.getPointerDeviceKey(eventData),
337-
physicalX: position.x,
338-
physicalY: position.y
359+
physicalX: position.Value.x,
360+
physicalY: position.Value.y
339361
));
340362
}
341363

342364
public void OnPointerEnter(PointerEventData eventData) {
365+
var position = this.getPointPosition(eventData);
366+
if (position == null) {
367+
return;
368+
}
343369
var pointerKey = InputUtils.getPointerDeviceKey(eventData);
344370
this._enteredPointers.Add(pointerKey);
345371

346372
this._lastMouseMove = eventData.position;
347-
var position = this.getPointPosition(eventData);
348373
this._windowAdapter.postPointerEvent(new PointerData(
349374
timeStamp: Timer.timespanSinceStartup,
350375
change: PointerChange.hover,
351376
kind: InputUtils.getPointerDeviceKind(eventData),
352377
device: pointerKey,
353-
physicalX: position.x,
354-
physicalY: position.y
378+
physicalX: position.Value.x,
379+
physicalY: position.Value.y
355380
));
356381
}
357382

358383
public void OnPointerExit(PointerEventData eventData) {
384+
var position = this.getPointPosition(eventData);
385+
if (position == null) {
386+
return;
387+
}
359388
var pointerKey = InputUtils.getPointerDeviceKey(eventData);
360389
this._enteredPointers.Remove(pointerKey);
361-
362-
var position = this.getPointPosition(eventData);
363390
this._windowAdapter.postPointerEvent(new PointerData(
364391
timeStamp: Timer.timespanSinceStartup,
365392
change: PointerChange.hover,
366393
kind: InputUtils.getPointerDeviceKind(eventData),
367394
device: pointerKey,
368-
physicalX: position.x,
369-
physicalY: position.y
395+
physicalX: position.Value.x,
396+
physicalY: position.Value.y
370397
));
371398
}
372399

0 commit comments

Comments
 (0)