Skip to content
Permalink
Browse files

Optimized UI events processing. Updates are no longer processed if no…

…t over a UI element, or not pressed a UI element.
  • Loading branch information...
valyard committed Jul 29, 2017
1 parent 0c368b5 commit 04aa00e3e543d282f910536ec4e4b8eaa81eb69b
Showing with 49 additions and 9 deletions.
  1. +49 −9 Source/Assets/TouchScript/Scripts/Layers/UI/TouchScriptInputModule.cs
@@ -10,6 +10,8 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Pointer = TouchScript.Pointers.Pointer;
using UnityEngine.Profiling;

namespace TouchScript.Layers.UI
{
@@ -62,7 +64,7 @@ public static TouchScriptInputModule Instance
private static TouchScriptInputModule instance;
private static FieldInfo raycastersProp;
private static PropertyInfo canvasProp;
private static Dictionary<int, Canvas> raycasterCanvasCache = new Dictionary<int, Canvas>();
private static Dictionary<int, Canvas> raycasterCanvasCache = new Dictionary<int, Canvas>(10);

private int refCount = 0;
private UIStandardInputModule ui;
@@ -218,11 +220,7 @@ private void disable()

#endregion

#region Event handlers

#endregion

#region Copypasted code from UI
#region Copy-pasted code from UI

/// <summary>
/// Basically, copied code from UI Input Module which handles all UI pointer processing logic.
@@ -232,9 +230,13 @@ private class UIStandardInputModule
{
protected TouchScriptInputModule input;

private CustomSampler uiSampler;

public UIStandardInputModule(TouchScriptInputModule input)
{
this.input = input;

uiSampler = CustomSampler.Create("[TouchScript] Update UI");
}

#region Unchanged from PointerInputModule
@@ -243,7 +245,7 @@ public UIStandardInputModule(TouchScriptInputModule input)
private Vector2 m_LastMoveVector;
private float m_PrevActionTime;

private Dictionary<int, PointerEventData> m_PointerData = new Dictionary<int, PointerEventData>();
private Dictionary<int, PointerEventData> m_PointerData = new Dictionary<int, PointerEventData>(10);

public bool IsPointerOverGameObject(int pointerId)
{
@@ -429,15 +431,24 @@ private void convertRaycast(RaycastHitUI old, ref RaycastResult current)

public virtual void ProcessUpdated(object sender, PointerEventArgs pointerEventArgs)
{
uiSampler.Begin();

var pointers = pointerEventArgs.Pointers;
var raycast = new RaycastResult();
var count = pointers.Count;
for (var i = 0; i < count; i++)
{
var pointer = pointers[i];
// Don't update the pointer if it is pressed not over an UI element
if ((pointer.Buttons & Pointer.PointerButtonState.AnyButtonPressed) > 0)
{
var press = pointer.GetPressData();
if (press.Type != HitData.HitType.UI) continue;
}

var over = pointer.GetOverData();
if (over.Type != HitData.HitType.UI && over.Type != HitData.HitType.ScreenSpace) continue;
// Don't update the pointer if it is not over an UI element
if (over.Type != HitData.HitType.UI) continue;

PointerEventData data;
GetPointerData(pointer.Id, out data, true);
@@ -487,18 +498,23 @@ public virtual void ProcessUpdated(object sender, PointerEventArgs pointerEventA
ExecuteEvents.ExecuteHierarchy(scrollHandler, data, ExecuteEvents.scrollHandler);
}
}

uiSampler.End();
}

public virtual void ProcessPressed(object sender, PointerEventArgs pointerEventArgs)
{
uiSampler.Begin();

var pointers = pointerEventArgs.Pointers;
var count = pointers.Count;
for (var i = 0; i < count; i++)
{
var pointer = pointers[i];

var over = pointer.GetOverData();
if (over.Type != HitData.HitType.UI && over.Type != HitData.HitType.ScreenSpace) continue;
// Don't update the pointer if it is not over an UI element
if (over.Type != HitData.HitType.UI) continue;

PointerEventData data;
GetPointerData(pointer.Id, out data, true);
@@ -560,15 +576,23 @@ public virtual void ProcessPressed(object sender, PointerEventArgs pointerEventA
if (data.pointerDrag != null)
ExecuteEvents.Execute(data.pointerDrag, data, ExecuteEvents.initializePotentialDrag);
}

uiSampler.End();
}

public virtual void ProcessReleased(object sender, PointerEventArgs pointerEventArgs)
{
uiSampler.Begin();

var pointers = pointerEventArgs.Pointers;
var count = pointers.Count;
for (var i = 0; i < count; i++)
{
var pointer = pointers[i];
var press = pointer.GetPressData();
// Don't update the pointer if it is was not pressed over an UI element
if (press.Type != HitData.HitType.UI) continue;

var over = pointer.GetOverData();

PointerEventData data;
@@ -611,15 +635,20 @@ public virtual void ProcessReleased(object sender, PointerEventArgs pointerEvent
input.HandlePointerExitAndEnter(data, currentOverGo);
}
}

uiSampler.End();
}

public virtual void ProcessCancelled(object sender, PointerEventArgs pointerEventArgs)
{
uiSampler.Begin();

var pointers = pointerEventArgs.Pointers;
var count = pointers.Count;
for (var i = 0; i < count; i++)
{
var pointer = pointers[i];

var over = pointer.GetOverData();

PointerEventData data;
@@ -648,21 +677,32 @@ public virtual void ProcessCancelled(object sender, PointerEventArgs pointerEven
ExecuteEvents.ExecuteHierarchy(data.pointerEnter, data, ExecuteEvents.pointerExitHandler);
data.pointerEnter = null;
}

uiSampler.End();
}

public virtual void ProcessRemoved(object sender, PointerEventArgs pointerEventArgs)
{
uiSampler.Begin();

var pointers = pointerEventArgs.Pointers;
var count = pointers.Count;
for (var i = 0; i < count; i++)
{
var pointer = pointers[i];

var over = pointer.GetOverData();
// Don't update the pointer if it is not over an UI element
if (over.Type != HitData.HitType.UI) continue;

PointerEventData data;
GetPointerData(pointer.Id, out data, true);

if (data.pointerEnter) ExecuteEvents.ExecuteHierarchy(data.pointerEnter, data, ExecuteEvents.pointerExitHandler);
RemovePointerData(pointer.Id);
}

uiSampler.End();
}

#endregion

0 comments on commit 04aa00e

Please sign in to comment.
You can’t perform that action at this time.