-
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/sorting-layer-attribute' of https://github.com/…
…purejenix/NaughtyAttributes into purejenix-feature/sorting-layer-attribute
- Loading branch information
Showing
7 changed files
with
243 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.