Skip to content

Commit 641a8fc

Browse files
svc-reach-platform-supportEvergreen
authored andcommitted
[Port] [6000.4] [UUM-130317][6000.5] Add back sort at root
1 parent 4c04599 commit 641a8fc

1 file changed

Lines changed: 64 additions & 45 deletions

File tree

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,120 @@
1-
using UnityEditorInternal;
21
using UnityEngine;
32
using UnityEngine.Rendering;
43
using UnityEngine.Rendering.Universal;
54

65
namespace UnityEditor
76
{
8-
[CustomEditor(typeof(UnityEngine.Rendering.SortingGroup))]
7+
[CustomEditor(typeof(SortingGroup))]
98
[SupportedOnRenderPipeline(typeof(UniversalRenderPipelineAsset))]
109
[CanEditMultipleObjects]
1110
internal class SortingGroupEditor2DURP : SortingGroupEditor
1211
{
13-
private static class Styles
12+
private enum SortType
1413
{
15-
public static GUIContent sort3DAs2D = EditorGUIUtility.TrTextContent("Sort 3D as 2D", "Clears z values on 3D meshes affected by a Sorting Group allowing them to sort with other 2D objects and Sort 3D as 2D sorting groups.");
14+
Default,
15+
SortAtRoot,
16+
Sort3DAs2D
17+
}
18+
19+
private static class GUIStyles
20+
{
21+
public static GUIContent Default = EditorGUIUtility.TrTextContent("Sorting Type",
22+
"Default sorting based on sorting layer and sorting order.");
23+
24+
public static GUIContent sortAtRootStyle = EditorGUIUtility.TrTextContent("Sorting Type",
25+
"Ignores all parent Sorting Groups and sorts at the root level against other Sorting Groups and Renderers");
26+
27+
public static GUIContent sort3DAs2D = EditorGUIUtility.TrTextContent("Sorting Type",
28+
"Clears z values on 3D meshes affected by a Sorting Group allowing them to sort with other 2D objects and Sort 3D as 2D sorting groups. This option also enables Sort At Root");
1629
}
1730

18-
private SerializedProperty m_SortingOrder;
19-
private SerializedProperty m_SortingLayerID;
2031
private SerializedProperty m_Sort3DAs2D;
32+
private SortType m_SortType;
2133

2234
public override void OnEnable()
2335
{
2436
base.OnEnable();
2537

26-
alwaysAllowExpansion = true;
27-
m_SortingOrder = serializedObject.FindProperty("m_SortingOrder");
28-
m_SortingLayerID = serializedObject.FindProperty("m_SortingLayerID");
2938
m_Sort3DAs2D = serializedObject.FindProperty("m_Sort3DAs2D");
39+
40+
// Initialize m_SortType
41+
m_SortType = m_Sort3DAs2D.boolValue ? SortType.Sort3DAs2D : m_SortAtRoot.boolValue ? SortType.SortAtRoot : SortType.Default;
3042
}
3143

32-
public RenderAs2D TryToFindCreatedRenderAs2D(SortingGroup sortingGroup)
44+
void OnInspectorGUIFor2D()
3345
{
34-
RenderAs2D[] renderAs2Ds = sortingGroup.GetComponents<RenderAs2D>();
35-
foreach (RenderAs2D renderAs2D in renderAs2Ds)
36-
{
37-
if (renderAs2D.IsOwner(sortingGroup))
38-
return renderAs2D;
39-
}
46+
serializedObject.Update();
4047

41-
return null;
42-
}
48+
SortingLayerEditorUtility.RenderSortingLayerFields(m_SortingOrder, m_SortingLayerID);
4349

44-
bool DrawToggleWithLayout(bool flatten, GUIContent content)
45-
{
46-
Rect rect = EditorGUILayout.GetControlRect();
47-
var boolValue = EditorGUI.Toggle(rect, content, flatten);
48-
return boolValue;
49-
}
50+
var prevSortType = m_SortType;
51+
var label = m_Sort3DAs2D.boolValue ? GUIStyles.sort3DAs2D : m_SortAtRoot.boolValue ? GUIStyles.sortAtRootStyle : GUIStyles.Default;
52+
m_SortType = (SortType)EditorGUILayout.EnumPopup(label, m_SortType);
5053

51-
void DirtyScene()
52-
{
53-
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEngine.SceneManagement.SceneManager.GetActiveScene());
54-
}
54+
if (prevSortType != m_SortType)
55+
{
56+
switch (m_SortType)
57+
{
58+
case SortType.SortAtRoot:
59+
m_SortAtRoot.boolValue = true;
60+
m_Sort3DAs2D.boolValue = false;
61+
break;
62+
63+
case SortType.Sort3DAs2D:
64+
m_SortAtRoot.boolValue = true;
65+
m_Sort3DAs2D.boolValue = true;
66+
break;
67+
68+
default:
69+
m_SortAtRoot.boolValue = false;
70+
m_Sort3DAs2D.boolValue = false;
71+
break;
72+
}
73+
}
5574

56-
void RenderSort3DAs2D()
57-
{
58-
EditorGUILayout.PropertyField(m_Sort3DAs2D, Styles.sort3DAs2D);
5975
foreach (var target in targets)
6076
{
6177
SortingGroup sortingGroup = (SortingGroup)target;
78+
GameObject go = sortingGroup.gameObject;
79+
go.TryGetComponent(out RenderAs2D renderAs2D);
80+
6281
if (sortingGroup.sort3DAs2D)
6382
{
64-
GameObject go = sortingGroup.gameObject;
65-
go.TryGetComponent<RenderAs2D>(out RenderAs2D renderAs2D);
66-
6783
if (renderAs2D != null && !renderAs2D.IsOwner(sortingGroup))
6884
{
69-
Component.DestroyImmediate(renderAs2D, true);
85+
DestroyImmediate(renderAs2D, true);
7086
renderAs2D = null;
7187
}
7288

73-
if(renderAs2D == null)
89+
if (renderAs2D == null)
7490
{
7591
Material mat = AssetDatabase.LoadAssetAtPath<Material>("Packages/com.unity.render-pipelines.universal/Runtime/Materials/RenderAs2D-Flattening.mat");
7692
renderAs2D = go.AddComponent<RenderAs2D>();
7793
renderAs2D.Init(sortingGroup);
7894
renderAs2D.material = mat;
79-
EditorUtility.SetDirty(sortingGroup.gameObject);
95+
EditorUtility.SetDirty(go);
96+
}
97+
}
98+
else
99+
{
100+
if (renderAs2D != null)
101+
{
102+
DestroyImmediate(renderAs2D, true);
103+
renderAs2D = null;
80104
}
81105
}
82106
}
107+
108+
serializedObject.ApplyModifiedProperties();
83109
}
84110

85111
public override void OnInspectorGUI()
86112
{
87-
serializedObject.Update();
88-
89113
var rpAsset = UniversalRenderPipeline.asset;
90114
if (rpAsset != null && (rpAsset.scriptableRenderer is Renderer2D))
91-
{
92-
SortingLayerEditorUtility.RenderSortingLayerFields(m_SortingOrder, m_SortingLayerID);
93-
RenderSort3DAs2D();
94-
}
115+
OnInspectorGUIFor2D();
95116
else
96117
base.OnInspectorGUI();
97-
98-
serializedObject.ApplyModifiedProperties();
99118
}
100119
}
101120
}

0 commit comments

Comments
 (0)