Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复Canvas中存在sortingOrder时的渲染错误 #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions Assets/Scripts/Core/GoWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class GoWrapper : DisplayObject
protected List<RendererInfo> _renderers;
protected Dictionary<Material, Material> _materialsBackup;
protected Canvas _canvas;
protected List<Canvas> _subCanvas;
protected int _subCanvasIndex = 0;
protected bool _cloneMaterial;
protected bool _shouldCloneMaterial;

Expand Down Expand Up @@ -90,6 +92,7 @@ public void SetWrapTarget(GameObject target, bool cloneMaterial)
_wrapTarget.transform.SetParent(null, false);

_canvas = null;
_subCanvas = null;
_wrapTarget = target;
_shouldCloneMaterial = false;
_renderers.Clear();
Expand All @@ -108,6 +111,10 @@ public void SetWrapTarget(GameObject target, bool cloneMaterial)
rt.pivot = new Vector2(0, 1);
rt.position = new Vector3(0, 0, 0);
this.SetSize(rt.rect.width, rt.rect.height);

_subCanvas = new List<Canvas>();
SearchChildrenCanvas(_canvas.transform);
ResortChildrenCanvas();
}
else
{
Expand All @@ -119,6 +126,42 @@ public void SetWrapTarget(GameObject target, bool cloneMaterial)
}
}


private void SearchChildrenCanvas(Transform rootTransform)
{
for (int i = 0; i < rootTransform.childCount; i++)
{
var transform = rootTransform.GetChild(i);
var canvas = transform.GetComponent<Canvas>();
if (canvas != null)
{
if (canvas.overrideSorting)
{
_subCanvas.Add(canvas);
}
}
SearchChildrenCanvas(transform);
}
}


private void ResortChildrenCanvas()
{
_subCanvas.Sort((canvas, canvas1) => canvas.sortingOrder - canvas1.sortingOrder);
_subCanvasIndex = 0;

for (int i = 0; i < _subCanvas.Count; i++)
{
if (_subCanvas[i].sortingOrder >= _canvas.sortingOrder)
{
_subCanvasIndex = i;
break;
}

_subCanvasIndex++;
}
}

/// <summary>
/// GoWrapper will cache all renderers of your gameobject on constructor.
/// If your gameobject change laterly, call this function to update the cache.
Expand Down Expand Up @@ -261,7 +304,27 @@ public override int renderingOrder
base.renderingOrder = value;

if (_canvas != null)
_canvas.sortingOrder = value;
{

if (_subCanvas != null && _subCanvas.Count>0)
{
for (int i = 0; i < _subCanvas.Count; i++)
{
if (i == _subCanvasIndex)
{
_canvas.sortingOrder = value;
value++;
}
_subCanvas[i].sortingOrder = value;
value++;
}
UpdateContext.current.renderingOrder = value;
}
else
{
_canvas.sortingOrder = value;
}
}
else
{
int cnt = _renderers.Count;
Expand Down Expand Up @@ -386,4 +449,4 @@ public override void Dispose()
base.Dispose();
}
}
}
}