Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrizov committed Feb 12, 2022
1 parent c608556 commit 131a48f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 125 deletions.
18 changes: 9 additions & 9 deletions Samples/DemoScene/DemoScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,8 @@ Transform:
- {fileID: 933563385}
- {fileID: 1989156459}
- {fileID: 1194502638}
- {fileID: 1380469385}
- {fileID: 1609261820}
- {fileID: 1380469385}
- {fileID: 732714204}
- {fileID: 1784643785}
- {fileID: 572382749}
Expand Down Expand Up @@ -1212,7 +1212,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 19
m_RootOrder: 20
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1380469386
MonoBehaviour:
Expand Down Expand Up @@ -1659,7 +1659,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 20
m_RootOrder: 19
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1609261821
MonoBehaviour:
Expand All @@ -1673,14 +1673,14 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 8ed73e666d447964d93c4840f05423dc, type: 3}
m_Name:
m_EditorClassIdentifier:
layerNumber0: 0
layerName0: Default
layerNumber0: 146379811
layerName0: SortingLayer0
nest1:
layerNumber1: 0
layerName1: Default
layerNumber1: -2136883699
layerName1: SortingLayer1
nest2:
layerNumber2: 0
layerName2: Default
layerNumber2: 1583436447
layerName2: SortingLayer2
--- !u!1 &1622801695
GameObject:
m_ObjectHideFlags: 0
Expand Down
8 changes: 4 additions & 4 deletions Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace NaughtyAttributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class SortingLayerAttribute : DrawerAttribute
{
}
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class SortingLayerAttribute : DrawerAttribute
{
}
}
142 changes: 71 additions & 71 deletions Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,87 @@

namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(SortingLayerAttribute))]
public class SortingLayerPropertyDrawer : PropertyDrawerBase
{
private const string TypeWarningMessage = "{0} must be an int or a string";
[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;
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();
}
return validPropertyType
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}

protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
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;
}
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();
}
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 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];
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];
}
}
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;
}
}
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);
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;
}
}
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);
}
}
private static int IndexOf(string[] layers, string layer)
{
var index = Array.IndexOf(layers, layer);
return Mathf.Clamp(index, 0, layers.Length - 1);
}
}
}
82 changes: 41 additions & 41 deletions Scripts/Test/SortingLayerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,45 @@

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;
}
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;
}
}

0 comments on commit 131a48f

Please sign in to comment.