Skip to content

Commit b4c56c9

Browse files
committed
feat: TextMeshPro multi-atlas and font fallback support.
1 parent b69f90f commit b4c56c9

File tree

12 files changed

+935
-722
lines changed

12 files changed

+935
-722
lines changed

Assets/Scripts/Core/Container.cs

Lines changed: 78 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class Container : DisplayObject
5353
List<DisplayObject> _children;
5454
DisplayObject _mask;
5555
Rect? _clipRect;
56-
List<DisplayObject> _descendants;
56+
List<BatchElement> _batchElements;
5757

5858
internal int _panelOrder;
5959
internal DisplayObject _lastFocus;
@@ -415,6 +415,7 @@ public DisplayObject mask
415415
if (_mask != value)
416416
{
417417
_mask = value;
418+
_flags |= Flags.BatchingRequested;
418419
UpdateBatchingFlags();
419420
}
420421
}
@@ -692,8 +693,8 @@ internal void UpdateBatchingFlags()
692693
{
693694
if (newValue)
694695
_flags |= Flags.BatchingRequested;
695-
else if (_descendants != null)
696-
_descendants.Clear();
696+
else if (_batchElements != null)
697+
_batchElements.Clear();
697698

698699
InvalidateBatchingState();
699700
}
@@ -733,7 +734,10 @@ public void SetChildrenLayer(int value)
733734
for (int i = 0; i < cnt; i++)
734735
{
735736
DisplayObject child = _children[i];
736-
child._SetLayerDirect(value);
737+
if (child._paintingMode > 0)
738+
child.paintingGraphics.gameObject.layer = value;
739+
else
740+
child._SetLayerDirect(value);
737741
if ((child is Container) && child._paintingMode == 0)
738742
((Container)child).SetChildrenLayer(value);
739743
}
@@ -794,7 +798,7 @@ override public void Update(UpdateContext context)
794798
else
795799
{
796800
if (_mask != null)
797-
_mask.renderingOrder = context.renderingOrder++;
801+
_mask.SetRenderingOrder(context, false);
798802

799803
int cnt = _children.Count;
800804
for (int i = 0; i < cnt; i++)
@@ -809,7 +813,7 @@ override public void Update(UpdateContext context)
809813
if (child.visible)
810814
{
811815
if (!(child.graphics != null && child.graphics._maskFlag == 1)) //if not a mask
812-
child.renderingOrder = context.renderingOrder++;
816+
child.SetRenderingOrder(context, false);
813817

814818
child.Update(context);
815819
}
@@ -825,7 +829,7 @@ override public void Update(UpdateContext context)
825829
if ((_flags & Flags.FairyBatching) != 0)
826830
{
827831
if (context.batchingDepth == 1)
828-
SetRenderingOrder(context);
832+
SetRenderingOrderAll(context);
829833
context.batchingDepth--;
830834
}
831835

@@ -845,23 +849,22 @@ override public void Update(UpdateContext context)
845849
onUpdate();
846850
}
847851

848-
private void SetRenderingOrder(UpdateContext context)
852+
private void SetRenderingOrderAll(UpdateContext context)
849853
{
850854
if ((_flags & Flags.BatchingRequested) != 0)
851855
DoFairyBatching();
852856

853857
if (_mask != null)
854-
_mask.renderingOrder = context.renderingOrder++;
858+
_mask.SetRenderingOrder(context, false);
855859

856-
int cnt = _descendants.Count;
860+
int cnt = _batchElements.Count;
857861
for (int i = 0; i < cnt; i++)
858862
{
859-
DisplayObject child = _descendants[i];
860-
if (!(child.graphics != null && child.graphics._maskFlag == 1))
861-
child.renderingOrder = context.renderingOrder++;
863+
BatchElement batchElement = _batchElements[i];
864+
batchElement.owner.SetRenderingOrder(context, true);
862865

863-
if ((child._flags & Flags.BatchingRoot) != 0)
864-
((Container)child).SetRenderingOrder(context);
866+
if (batchElement.isRoot)
867+
((Container)batchElement.owner).SetRenderingOrderAll(context);
865868
}
866869

867870
if (_mask != null)
@@ -875,33 +878,33 @@ private void DoFairyBatching()
875878
{
876879
_flags &= ~Flags.BatchingRequested;
877880

878-
if (_descendants == null)
879-
_descendants = new List<DisplayObject>();
881+
if (_batchElements == null)
882+
_batchElements = new List<BatchElement>();
880883
else
881-
_descendants.Clear();
884+
_batchElements.Clear();
882885
CollectChildren(this, false);
883886

884-
int cnt = _descendants.Count;
887+
int cnt = _batchElements.Count;
885888

886889
int i, j, k, m;
887890
object curMat, testMat, lastMat;
888-
DisplayObject current, test;
889-
float[] bound;
891+
BatchElement current, test;
892+
float[] bounds;
890893
for (i = 0; i < cnt; i++)
891894
{
892-
current = _descendants[i];
893-
bound = current._batchingBounds;
895+
current = _batchElements[i];
896+
bounds = current.bounds;
894897
curMat = current.material;
895-
if (curMat == null || (current._flags & Flags.SkipBatching) != 0)
898+
if (curMat == null || current.breakBatch)
896899
continue;
897900

898901
k = -1;
899902
lastMat = null;
900903
m = i;
901904
for (j = i - 1; j >= 0; j--)
902905
{
903-
test = _descendants[j];
904-
if ((test._flags & Flags.SkipBatching) != 0)
906+
test = _batchElements[j];
907+
if (test.breakBatch)
905908
break;
906909

907910
testMat = test.material;
@@ -917,10 +920,10 @@ private void DoFairyBatching()
917920
k = m;
918921
}
919922

920-
if ((bound[0] > test._batchingBounds[0] ? bound[0] : test._batchingBounds[0])
921-
<= (bound[2] < test._batchingBounds[2] ? bound[2] : test._batchingBounds[2])
922-
&& (bound[1] > test._batchingBounds[1] ? bound[1] : test._batchingBounds[1])
923-
<= (bound[3] < test._batchingBounds[3] ? bound[3] : test._batchingBounds[3]))
923+
if ((bounds[0] > test.bounds[0] ? bounds[0] : test.bounds[0])
924+
<= (bounds[2] < test.bounds[2] ? bounds[2] : test.bounds[2])
925+
&& (bounds[1] > test.bounds[1] ? bounds[1] : test.bounds[1])
926+
<= (bounds[3] < test.bounds[3] ? bounds[3] : test.bounds[3]))
924927
{
925928
if (k == -1)
926929
k = m;
@@ -929,8 +932,8 @@ private void DoFairyBatching()
929932
}
930933
if (k != -1 && i != k)
931934
{
932-
_descendants.RemoveAt(i);
933-
_descendants.Insert(k, current);
935+
_batchElements.RemoveAt(i);
936+
_batchElements.Insert(k, current);
934937
}
935938
}
936939

@@ -943,46 +946,33 @@ private void CollectChildren(Container initiator, bool outlineChanged)
943946
for (int i = 0; i < count; i++)
944947
{
945948
DisplayObject child = _children[i];
946-
if (!child.visible)
949+
if (!child.visible || child == initiator._mask)
947950
continue;
948951

949-
if (child._batchingBounds == null)
950-
child._batchingBounds = new float[4];
951-
952-
if (child is Container)
952+
bool childOutlineChanged = outlineChanged || (child._flags & Flags.OutlineChanged) != 0;
953+
bool isRoot = (child._flags & Flags.BatchingRoot) != 0;
954+
BatchElement batchElement = child.AddToBatch(initiator._batchElements, isRoot);
955+
if (batchElement != null)
953956
{
954-
Container container = (Container)child;
955-
if ((container._flags & Flags.BatchingRoot) != 0)
956-
{
957-
initiator._descendants.Add(container);
958-
if (outlineChanged || (container._flags & Flags.OutlineChanged) != 0)
959-
{
960-
Rect rect = container.GetBounds(initiator);
961-
container._batchingBounds[0] = rect.xMin;
962-
container._batchingBounds[1] = rect.yMin;
963-
container._batchingBounds[2] = rect.xMax;
964-
container._batchingBounds[3] = rect.yMax;
965-
}
966-
if ((container._flags & Flags.BatchingRequested) != 0)
967-
container.DoFairyBatching();
968-
}
969-
else
970-
container.CollectChildren(initiator, outlineChanged || (container._flags & Flags.OutlineChanged) != 0);
971-
}
972-
else if (child != initiator._mask)
973-
{
974-
if (outlineChanged || (child._flags & Flags.OutlineChanged) != 0)
957+
batchElement.isRoot = isRoot;
958+
if (childOutlineChanged)
975959
{
976960
Rect rect = child.GetBounds(initiator);
977-
child._batchingBounds[0] = rect.xMin;
978-
child._batchingBounds[1] = rect.yMin;
979-
child._batchingBounds[2] = rect.xMax;
980-
child._batchingBounds[3] = rect.yMax;
961+
batchElement.bounds[0] = rect.xMin;
962+
batchElement.bounds[1] = rect.yMin;
963+
batchElement.bounds[2] = rect.xMax;
964+
batchElement.bounds[3] = rect.yMax;
965+
child._flags &= ~Flags.OutlineChanged;
981966
}
982-
initiator._descendants.Add(child);
983967
}
984968

985-
child._flags &= ~Flags.OutlineChanged;
969+
if (isRoot)
970+
{
971+
if ((child._flags & Flags.BatchingRequested) != 0)
972+
((Container)child).DoFairyBatching();
973+
}
974+
else if (child is Container)
975+
((Container)child).CollectChildren(initiator, childOutlineChanged);
986976
}
987977
}
988978

@@ -1121,4 +1111,28 @@ public void Dispose()
11211111
}
11221112
}
11231113
}
1114+
1115+
1116+
public class BatchElement
1117+
{
1118+
public Material material;
1119+
public float[] bounds;
1120+
public IBatchable owner;
1121+
public bool isRoot;
1122+
public bool breakBatch;
1123+
1124+
public BatchElement(IBatchable owner, float[] bounds)
1125+
{
1126+
this.owner = owner;
1127+
this.bounds = bounds ?? new float[4];
1128+
}
1129+
}
1130+
1131+
/// <summary>
1132+
///
1133+
/// </summary>
1134+
public interface IBatchable
1135+
{
1136+
void SetRenderingOrder(UpdateContext context, bool inBatch);
1137+
}
11241138
}

0 commit comments

Comments
 (0)