Skip to content

Commit

Permalink
Merge branch 'feature/sorting-layer-attribute' of https://github.com/…
Browse files Browse the repository at this point in the history
…purejenix/NaughtyAttributes into purejenix-feature/sorting-layer-attribute
  • Loading branch information
dbrizov committed Feb 12, 2022
2 parents b25f8ae + 7fef240 commit c608556
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 12 deletions.
76 changes: 64 additions & 12 deletions Samples/DemoScene/DemoScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 23
m_RootOrder: 24
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &114650325
MonoBehaviour:
Expand Down Expand Up @@ -345,7 +345,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 27
m_RootOrder: 28
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &237121642
MonoBehaviour:
Expand Down Expand Up @@ -393,7 +393,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 30
m_RootOrder: 31
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &369789278
MonoBehaviour:
Expand Down Expand Up @@ -441,7 +441,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 22
m_RootOrder: 23
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &572382751
MonoBehaviour:
Expand Down Expand Up @@ -584,7 +584,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 20
m_RootOrder: 21
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &779670086
GameObject:
Expand Down Expand Up @@ -930,6 +930,7 @@ Transform:
- {fileID: 1989156459}
- {fileID: 1194502638}
- {fileID: 1380469385}
- {fileID: 1609261820}
- {fileID: 732714204}
- {fileID: 1784643785}
- {fileID: 572382749}
Expand Down Expand Up @@ -1163,7 +1164,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 25
m_RootOrder: 26
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1293624597
MonoBehaviour:
Expand Down Expand Up @@ -1352,7 +1353,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 31
m_RootOrder: 32
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1463483879
MonoBehaviour:
Expand Down Expand Up @@ -1405,7 +1406,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 24
m_RootOrder: 25
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1524906392
MonoBehaviour:
Expand Down Expand Up @@ -1629,6 +1630,57 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1609261819
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1609261820}
- component: {fileID: 1609261821}
m_Layer: 0
m_Name: 'SortingLayer '
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1609261820
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1609261819}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 20
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1609261821
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1609261819}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8ed73e666d447964d93c4840f05423dc, type: 3}
m_Name:
m_EditorClassIdentifier:
layerNumber0: 0
layerName0: Default
nest1:
layerNumber1: 0
layerName1: Default
nest2:
layerNumber2: 0
layerName2: Default
--- !u!1 &1622801695
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1706,7 +1758,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 28
m_RootOrder: 29
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1706612701
GameObject:
Expand Down Expand Up @@ -1737,7 +1789,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 29
m_RootOrder: 30
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1706612703
MonoBehaviour:
Expand Down Expand Up @@ -1873,7 +1925,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 21
m_RootOrder: 22
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1784643786
MonoBehaviour:
Expand Down Expand Up @@ -1926,7 +1978,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 26
m_RootOrder: 27
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1871127831
MonoBehaviour:
Expand Down
9 changes: 9 additions & 0 deletions Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace NaughtyAttributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class SortingLayerAttribute : DrawerAttribute
{
}
}
11 changes: 11 additions & 0 deletions Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs.meta

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

91 changes: 91 additions & 0 deletions Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;

namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(SortingLayerAttribute))]
public class SortingLayerPropertyDrawer : PropertyDrawerBase
{
private const string TypeWarningMessage = "{0} must be an int or a string";

protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
bool validPropertyType = property.propertyType == SerializedPropertyType.String || property.propertyType == SerializedPropertyType.Integer;

return validPropertyType
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}

protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);

switch (property.propertyType)
{
case SerializedPropertyType.String:
DrawPropertyForString(rect, property, label, GetLayers());
break;
case SerializedPropertyType.Integer:
DrawPropertyForInt(rect, property, label, GetLayers());
break;
default:
string message = string.Format(TypeWarningMessage, property.name);
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
break;
}

EditorGUI.EndProperty();
}

private string[] GetLayers()
{
Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility);
PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
return (string[])sortingLayersProperty.GetValue(null, new object[0]);
}

private static void DrawPropertyForString(Rect rect, SerializedProperty property, GUIContent label, string[] layers)
{
int index = IndexOf(layers, property.stringValue);
int newIndex = EditorGUI.Popup(rect, label.text, index, layers);
string newLayer = layers[newIndex];

if (!property.stringValue.Equals(newLayer, StringComparison.Ordinal))
{
property.stringValue = layers[newIndex];
}
}

private static void DrawPropertyForInt(Rect rect, SerializedProperty property, GUIContent label, string[] layers)
{
int index = 0;
string layerName = SortingLayer.IDToName(property.intValue);
for (int i = 0; i < layers.Length; i++)
{
if (layerName.Equals(layers[i], StringComparison.Ordinal))
{
index = i;
break;
}
}

int newIndex = EditorGUI.Popup(rect, label.text, index, layers);
string newLayerName = layers[newIndex];
int newLayerNumber = SortingLayer.NameToID(newLayerName);

if (property.intValue != newLayerNumber)
{
property.intValue = newLayerNumber;
}
}

private static int IndexOf(string[] layers, string layer)
{
var index = Array.IndexOf(layers, layer);
return Mathf.Clamp(index, 0, layers.Length - 1);
}
}
}
11 changes: 11 additions & 0 deletions Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs.meta

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

46 changes: 46 additions & 0 deletions Scripts/Test/SortingLayerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using UnityEngine;

namespace NaughtyAttributes.Test
{
public class SortingLayerTest : MonoBehaviour
{
[SortingLayer]
public int layerNumber0;

[SortingLayer]
public string layerName0;

public SortingLayerNest1 nest1;

[Button]
public void DebugLog()
{
Debug.LogFormat("{0} = {1}", nameof(layerNumber0), layerNumber0);
Debug.LogFormat("{0} = {1}", nameof(layerName0), layerName0);
Debug.LogFormat("LayerToName({0}) = {1}", layerNumber0, SortingLayer.IDToName(layerNumber0));
Debug.LogFormat("NameToLayer({0}) = {1}", layerName0, SortingLayer.NameToID(layerName0));
}
}

[System.Serializable]
public class SortingLayerNest1
{
[SortingLayer]
public int layerNumber1;

[SortingLayer]
public string layerName1;

public SortingLayerNest2 nest2;
}

[System.Serializable]
public struct SortingLayerNest2
{
[SortingLayer]
public int layerNumber2;

[SortingLayer]
public string layerName2;
}
}
11 changes: 11 additions & 0 deletions Scripts/Test/SortingLayerTest.cs.meta

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

0 comments on commit c608556

Please sign in to comment.