-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasSorter.cs
More file actions
39 lines (34 loc) · 1.01 KB
/
CanvasSorter.cs
File metadata and controls
39 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using UnityEngine;
using UnityEngine.Rendering;
[RequireComponent(typeof(SortingGroup))]
[ExecuteInEditMode]
public class CanvasSorter : MonoBehaviour
{
[SerializeField] private int _canvasSortOffset = 1;
private SortingGroup _sortingGroup;
private Canvas[] _childCanvases;
private void Awake()
{
if (_sortingGroup == null)
{
_sortingGroup = GetComponent<SortingGroup>();
if (_sortingGroup == null)
{
Debug.LogError("No Sorting Group for Canvas Sorter to pull from.");
Destroy(this);
return;
}
}
// TODO: update list on children updated
_childCanvases = GetComponentsInChildren<Canvas>();
}
private void Update()
{
foreach (var canvas in _childCanvases)
{
canvas.sortingLayerID = _sortingGroup.sortingLayerID;
canvas.sortingOrder = _sortingGroup.sortingOrder + _canvasSortOffset;
}
}
}