Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
RectMask2D Optimization
Disable Culling & Softness (Toggles exposed to Inspector)
  • Loading branch information
mitay-walle committed Jul 7, 2022
1 parent 1ec28b9 commit 3035e5e
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Editor/UI/RectMask2DEditor.cs
Expand Up @@ -3,7 +3,7 @@

namespace UnityEditor.UI
{
[CustomEditor(typeof(RectMask2D), true)]
//[CustomEditor(typeof(RectMask2D), true)]
[CanEditMultipleObjects]
/// <summary>
/// Custom editor for the RectMask2d component.
Expand Down
8 changes: 8 additions & 0 deletions Runtime/External.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions Runtime/External/ObjectPoolOptimized.cs
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using UnityEngine.Pool;
namespace External
{

public class ObjectPoolOptimized<T> : IDisposable, IObjectPool<T> where T : class
{
static IComparer<T> Sorter = Comparer<T>.Create(Comparison);
static int Comparison(T x, T y) => 0;

internal readonly HashSet<T> m_Collection;
internal readonly List<T> m_Collection2;
private readonly Func<T> m_CreateFunc;
private readonly Action<T> m_ActionOnGet;
private readonly Action<T> m_ActionOnRelease;
private readonly Action<T> m_ActionOnDestroy;
private readonly int m_MaxSize;
internal bool m_CollectionCheck;

public int CountAll { get; private set; }

public int CountActive => CountAll - CountInactive;

public int CountInactive => m_Collection.Count;

public ObjectPoolOptimized(
Func<T> createFunc,
Action<T> actionOnGet = null,
Action<T> actionOnRelease = null,
Action<T> actionOnDestroy = null,
bool collectionCheck = true,
int defaultCapacity = 10,
int maxSize = 10000)
{
if (createFunc == null)
throw new ArgumentNullException(nameof(createFunc));
if (maxSize <= 0)
throw new ArgumentException("Max Size must be greater than 0", nameof(maxSize));
m_Collection = new HashSet<T>();
m_Collection2 = new List<T>(defaultCapacity);
for (int i = 0; i < defaultCapacity; i++)
{
var element = createFunc();
m_Collection.Add(element);
m_Collection.Add(element);
}
m_CreateFunc = createFunc;
m_MaxSize = maxSize;
m_ActionOnGet = actionOnGet;
m_ActionOnRelease = actionOnRelease;
m_ActionOnDestroy = actionOnDestroy;
m_CollectionCheck = collectionCheck;
}

public T Get()
{
T obj;
if (m_Collection2.Count == 0)
{
obj = m_CreateFunc();
++CountAll;
}
else
{
obj = m_Collection2[0];
m_Collection.Remove(obj);
m_Collection2.RemoveAt(0);
}
Action<T> actionOnGet = m_ActionOnGet;
if (actionOnGet != null)
actionOnGet(obj);
return obj;
}

public PooledObject<T> Get(out T v) => new PooledObject<T>(v = Get(), (IObjectPool<T>)this);

public void Release(T element)
{
if (m_CollectionCheck && m_Collection.Count > 0 && m_Collection.Contains(element))
{
return;
throw new InvalidOperationException($"Trying to release an object '{element}' that has already been released to the pool.");
}
Action<T> actionOnRelease = m_ActionOnRelease;
if (actionOnRelease != null)
actionOnRelease(element);
if (CountInactive < m_MaxSize)
{
m_Collection.Add(element);
m_Collection2.Add(element);
}
else
{
Action<T> actionOnDestroy = m_ActionOnDestroy;
if (actionOnDestroy != null)
actionOnDestroy(element);
}
}

public void Clear()
{
if (m_ActionOnDestroy != null)
{
foreach (T obj in m_Collection)
m_ActionOnDestroy(obj);
}
m_Collection.Clear();
m_Collection2.Clear();
CountAll = 0;
}

public void Dispose() => Clear();
}
}
11 changes: 11 additions & 0 deletions Runtime/External/ObjectPoolOptimized.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions Runtime/UI/Core/RectMask2D.cs
Expand Up @@ -48,7 +48,12 @@ public class RectMask2D : UIBehaviour, IClipper, ICanvasRaycastFilter

[SerializeField]
private Vector4 m_Padding = new Vector4();


[SerializeField]
private bool UseCulling;
[SerializeField]
private bool UseSoftness;

/// <summary>
/// Padding to be applied to the masking
/// X = Left
Expand Down Expand Up @@ -244,7 +249,7 @@ public virtual void PerformClipping()
foreach (MaskableGraphic maskableTarget in m_MaskableTargets)
{
maskableTarget.SetClipRect(clipRect, validRect);
maskableTarget.Cull(clipRect, validRect);
if (UseCulling) maskableTarget.Cull(clipRect, validRect);
}
}
else if (m_ForceClip)
Expand All @@ -258,23 +263,28 @@ public virtual void PerformClipping()
{
maskableTarget.SetClipRect(clipRect, validRect);

if (maskableTarget.canvasRenderer.hasMoved)
if (maskableTarget.canvasRenderer.hasMoved && UseCulling)
{
maskableTarget.Cull(clipRect, validRect);
}
}
}
else
{
foreach (MaskableGraphic maskableTarget in m_MaskableTargets)
if (UseCulling)
{
//Case 1170399 - hasMoved is not a valid check when animating on pivot of the object
maskableTarget.Cull(clipRect, validRect);
foreach (MaskableGraphic maskableTarget in m_MaskableTargets)
{
//Case 1170399 - hasMoved is not a valid check when animating on pivot of the object
maskableTarget.Cull(clipRect, validRect);
}
}
}

m_LastClipRectCanvasSpace = clipRect;
m_ForceClip = false;

UpdateClipSoftness();
if (UseSoftness) UpdateClipSoftness();
}

public virtual void UpdateClipSoftness()
Expand Down

0 comments on commit 3035e5e

Please sign in to comment.