First commit for private source control. Older commits available on Github.

This commit is contained in:
2026-03-26 12:52:52 +00:00
parent a04c602626
commit 2d449c4a17
2176 changed files with 408185 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
using UnityEngine;
using UnityEditor;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(AllowNestingAttribute))]
public class AllowNestingPropertyDrawer : PropertyDrawerBase
{
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
EditorGUI.PropertyField(rect, property, label, true);
EditorGUI.EndProperty();
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: a3175e7041b8f4348bd652485a78e7b1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AllowNestingPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,173 @@
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(AnimatorParamAttribute))]
public class AnimatorParamPropertyDrawer : PropertyDrawerBase
{
private const string InvalidAnimatorControllerWarningMessage = "Target animator controller is null";
private const string InvalidTypeWarningMessage = "{0} must be an int or a string";
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute<AnimatorParamAttribute>(property);
bool validAnimatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName) != null;
bool validPropertyType = property.propertyType == SerializedPropertyType.Integer || property.propertyType == SerializedPropertyType.String;
return (validAnimatorController && validPropertyType)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute<AnimatorParamAttribute>(property);
AnimatorController animatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName);
if (animatorController == null)
{
DrawDefaultPropertyAndHelpBox(rect, property, InvalidAnimatorControllerWarningMessage, MessageType.Warning);
return;
}
int parametersCount = animatorController.parameters.Length;
List<AnimatorControllerParameter> animatorParameters = new List<AnimatorControllerParameter>(parametersCount);
for (int i = 0; i < parametersCount; i++)
{
AnimatorControllerParameter parameter = animatorController.parameters[i];
if (animatorParamAttribute.AnimatorParamType == null || parameter.type == animatorParamAttribute.AnimatorParamType)
{
animatorParameters.Add(parameter);
}
}
switch (property.propertyType)
{
case SerializedPropertyType.Integer:
DrawPropertyForInt(rect, property, label, animatorParameters);
break;
case SerializedPropertyType.String:
DrawPropertyForString(rect, property, label, animatorParameters);
break;
default:
DrawDefaultPropertyAndHelpBox(rect, property, string.Format(InvalidTypeWarningMessage, property.name), MessageType.Warning);
break;
}
EditorGUI.EndProperty();
}
private static void DrawPropertyForInt(Rect rect, SerializedProperty property, GUIContent label, List<AnimatorControllerParameter> animatorParameters)
{
int paramNameHash = property.intValue;
int index = 0;
for (int i = 0; i < animatorParameters.Count; i++)
{
if (paramNameHash == animatorParameters[i].nameHash)
{
index = i + 1; // +1 because the first option is reserved for (None)
break;
}
}
string[] displayOptions = GetDisplayOptions(animatorParameters);
int newIndex = EditorGUI.Popup(rect, label.text, index, displayOptions);
int newValue = newIndex == 0 ? 0 : animatorParameters[newIndex - 1].nameHash;
if (property.intValue != newValue)
{
property.intValue = newValue;
}
}
private static void DrawPropertyForString(Rect rect, SerializedProperty property, GUIContent label, List<AnimatorControllerParameter> animatorParameters)
{
string paramName = property.stringValue;
int index = 0;
for (int i = 0; i < animatorParameters.Count; i++)
{
if (paramName.Equals(animatorParameters[i].name, System.StringComparison.Ordinal))
{
index = i + 1; // +1 because the first option is reserved for (None)
break;
}
}
string[] displayOptions = GetDisplayOptions(animatorParameters);
int newIndex = EditorGUI.Popup(rect, label.text, index, displayOptions);
string newValue = newIndex == 0 ? null : animatorParameters[newIndex - 1].name;
if (!property.stringValue.Equals(newValue, System.StringComparison.Ordinal))
{
property.stringValue = newValue;
}
}
private static string[] GetDisplayOptions(List<AnimatorControllerParameter> animatorParams)
{
string[] displayOptions = new string[animatorParams.Count + 1];
displayOptions[0] = "(None)";
for (int i = 0; i < animatorParams.Count; i++)
{
displayOptions[i + 1] = animatorParams[i].name;
}
return displayOptions;
}
private static AnimatorController GetAnimatorController(SerializedProperty property, string animatorName)
{
object target = PropertyUtility.GetTargetObjectWithProperty(property);
FieldInfo animatorFieldInfo = ReflectionUtility.GetField(target, animatorName);
if (animatorFieldInfo != null &&
animatorFieldInfo.FieldType == typeof(Animator))
{
Animator animator = animatorFieldInfo.GetValue(target) as Animator;
if (animator != null)
{
AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController;
return animatorController;
}
}
PropertyInfo animatorPropertyInfo = ReflectionUtility.GetProperty(target, animatorName);
if (animatorPropertyInfo != null &&
animatorPropertyInfo.PropertyType == typeof(Animator))
{
Animator animator = animatorPropertyInfo.GetValue(target) as Animator;
if (animator != null)
{
AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController;
return animatorController;
}
}
MethodInfo animatorGetterMethodInfo = ReflectionUtility.GetMethod(target, animatorName);
if (animatorGetterMethodInfo != null &&
animatorGetterMethodInfo.ReturnType == typeof(Animator) &&
animatorGetterMethodInfo.GetParameters().Length == 0)
{
Animator animator = animatorGetterMethodInfo.Invoke(target, null) as Animator;
if (animator != null)
{
AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController;
return animatorController;
}
}
return null;
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 98ff8cb1bcefae740a68d9a5c5ee3563
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,47 @@
using UnityEngine;
using UnityEditor;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(CurveRangeAttribute))]
public class CurveRangePropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
float propertyHeight = property.propertyType == SerializedPropertyType.AnimationCurve
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
return propertyHeight;
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
// Check user error
if (property.propertyType != SerializedPropertyType.AnimationCurve)
{
string message = string.Format("Field {0} is not an AnimationCurve", property.name);
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
return;
}
var curveRangeAttribute = (CurveRangeAttribute)attribute;
var curveRanges = new Rect(
curveRangeAttribute.Min.x,
curveRangeAttribute.Min.y,
curveRangeAttribute.Max.x - curveRangeAttribute.Min.x,
curveRangeAttribute.Max.y - curveRangeAttribute.Min.y);
EditorGUI.CurveField(
rect,
property,
curveRangeAttribute.Color == EColor.Clear ? Color.green : curveRangeAttribute.Color.GetColor(),
curveRanges,
label);
EditorGUI.EndProperty();
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 798b8c99fbc072a4b83ee387e472a2bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/CurveRangePropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,177 @@
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Reflection;
using System;
using System.Collections.Generic;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(DropdownAttribute))]
public class DropdownPropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
DropdownAttribute dropdownAttribute = (DropdownAttribute)attribute;
object values = GetValues(property, dropdownAttribute.ValuesName);
FieldInfo fieldInfo = ReflectionUtility.GetField(PropertyUtility.GetTargetObjectWithProperty(property), property.name);
float propertyHeight = AreValuesValid(values, fieldInfo)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
return propertyHeight;
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
DropdownAttribute dropdownAttribute = (DropdownAttribute)attribute;
object target = PropertyUtility.GetTargetObjectWithProperty(property);
object valuesObject = GetValues(property, dropdownAttribute.ValuesName);
FieldInfo dropdownField = ReflectionUtility.GetField(target, property.name);
if (AreValuesValid(valuesObject, dropdownField))
{
if (valuesObject is IList && dropdownField.FieldType == GetElementType(valuesObject))
{
// Selected value
object selectedValue = dropdownField.GetValue(target);
// Values and display options
IList valuesList = (IList)valuesObject;
object[] values = new object[valuesList.Count];
string[] displayOptions = new string[valuesList.Count];
for (int i = 0; i < values.Length; i++)
{
object value = valuesList[i];
values[i] = value;
displayOptions[i] = value == null ? "<null>" : value.ToString();
}
// Selected value index
int selectedValueIndex = Array.IndexOf(values, selectedValue);
if (selectedValueIndex < 0)
{
selectedValueIndex = 0;
}
NaughtyEditorGUI.Dropdown(
rect, property.serializedObject, target, dropdownField, label.text, selectedValueIndex, values, displayOptions);
}
else if (valuesObject is IDropdownList)
{
// Current value
object selectedValue = dropdownField.GetValue(target);
// Current value index, values and display options
int index = -1;
int selectedValueIndex = -1;
List<object> values = new List<object>();
List<string> displayOptions = new List<string>();
IDropdownList dropdown = (IDropdownList)valuesObject;
using (IEnumerator<KeyValuePair<string, object>> dropdownEnumerator = dropdown.GetEnumerator())
{
while (dropdownEnumerator.MoveNext())
{
index++;
KeyValuePair<string, object> current = dropdownEnumerator.Current;
if (current.Value?.Equals(selectedValue) == true)
{
selectedValueIndex = index;
}
values.Add(current.Value);
if (current.Key == null)
{
displayOptions.Add("<null>");
}
else if (string.IsNullOrWhiteSpace(current.Key))
{
displayOptions.Add("<empty>");
}
else
{
displayOptions.Add(current.Key);
}
}
}
if (selectedValueIndex < 0)
{
selectedValueIndex = 0;
}
NaughtyEditorGUI.Dropdown(
rect, property.serializedObject, target, dropdownField, label.text, selectedValueIndex, values.ToArray(), displayOptions.ToArray());
}
}
else
{
string message = string.Format("Invalid values with name '{0}' provided to '{1}'. Either the values name is incorrect or the types of the target field and the values field/property/method don't match",
dropdownAttribute.ValuesName, dropdownAttribute.GetType().Name);
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
private object GetValues(SerializedProperty property, string valuesName)
{
object target = PropertyUtility.GetTargetObjectWithProperty(property);
FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, valuesName);
if (valuesFieldInfo != null)
{
return valuesFieldInfo.GetValue(target);
}
PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, valuesName);
if (valuesPropertyInfo != null)
{
return valuesPropertyInfo.GetValue(target);
}
MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, valuesName);
if (methodValuesInfo != null &&
methodValuesInfo.ReturnType != typeof(void) &&
methodValuesInfo.GetParameters().Length == 0)
{
return methodValuesInfo.Invoke(target, null);
}
return null;
}
private bool AreValuesValid(object values, FieldInfo dropdownField)
{
if (values == null || dropdownField == null)
{
return false;
}
if ((values is IList && dropdownField.FieldType == GetElementType(values)) ||
(values is IDropdownList))
{
return true;
}
return false;
}
private Type GetElementType(object values)
{
Type valuesType = values.GetType();
Type elementType = ReflectionUtility.GetListElementType(valuesType);
return elementType;
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: dd080b36769bcd94d909fc0431cf25e0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/DropdownPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,38 @@
using UnityEngine;
using UnityEditor;
using System;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
public class EnumFlagsPropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
Enum targetEnum = PropertyUtility.GetTargetObjectOfProperty(property) as Enum;
return (targetEnum != null)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
Enum targetEnum = PropertyUtility.GetTargetObjectOfProperty(property) as Enum;
if (targetEnum != null)
{
Enum enumNew = EditorGUI.EnumFlagsField(rect, label.text, targetEnum);
property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
}
else
{
string message = attribute.GetType().Name + " can be used only on enums";
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: b750e1461c1126d4399459b90b31e75e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/EnumFlagsPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,195 @@
using UnityEngine;
using UnityEditor;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(ExpandableAttribute))]
public class ExpandablePropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue == null)
{
return GetPropertyHeight(property);
}
System.Type propertyType = PropertyUtility.GetPropertyType(property);
if (typeof(ScriptableObject).IsAssignableFrom(propertyType))
{
ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
if (scriptableObject == null)
{
return GetPropertyHeight(property);
}
if (property.isExpanded)
{
using (SerializedObject serializedObject = new SerializedObject(scriptableObject))
{
float totalHeight = EditorGUIUtility.singleLineHeight;
using (var iterator = serializedObject.GetIterator())
{
if (iterator.NextVisible(true))
{
do
{
SerializedProperty childProperty = serializedObject.FindProperty(iterator.name);
if (childProperty.name.Equals("m_Script", System.StringComparison.Ordinal))
{
continue;
}
bool visible = PropertyUtility.IsVisible(childProperty);
if (!visible)
{
continue;
}
float height = GetPropertyHeight(childProperty);
totalHeight += height;
}
while (iterator.NextVisible(false));
}
}
totalHeight += EditorGUIUtility.standardVerticalSpacing;
return totalHeight;
}
}
else
{
return GetPropertyHeight(property);
}
}
else
{
return GetPropertyHeight(property) + GetHelpBoxHeight();
}
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
if (property.objectReferenceValue == null)
{
EditorGUI.PropertyField(rect, property, label, false);
}
else
{
System.Type propertyType = PropertyUtility.GetPropertyType(property);
if (typeof(ScriptableObject).IsAssignableFrom(propertyType))
{
ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
if (scriptableObject == null)
{
EditorGUI.PropertyField(rect, property, label, false);
}
else
{
// Draw a foldout
Rect foldoutRect = new Rect()
{
x = rect.x,
y = rect.y,
width = EditorGUIUtility.labelWidth,
height = EditorGUIUtility.singleLineHeight
};
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, toggleOnLabelClick: true);
// Draw the scriptable object field
Rect propertyRect = new Rect()
{
x = rect.x,
y = rect.y,
width = rect.width,
height = EditorGUIUtility.singleLineHeight
};
EditorGUI.PropertyField(propertyRect, property, label, false);
// Draw the child properties
if (property.isExpanded)
{
DrawChildProperties(rect, property);
}
}
}
else
{
string message = $"{typeof(ExpandableAttribute).Name} can only be used on scriptable objects";
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
}
property.serializedObject.ApplyModifiedProperties();
EditorGUI.EndProperty();
}
private void DrawChildProperties(Rect rect, SerializedProperty property)
{
ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
if (scriptableObject == null)
{
return;
}
Rect boxRect = new Rect()
{
x = 0.0f,
y = rect.y + EditorGUIUtility.singleLineHeight,
width = rect.width * 2.0f,
height = rect.height - EditorGUIUtility.singleLineHeight
};
GUI.Box(boxRect, GUIContent.none);
using (new EditorGUI.IndentLevelScope())
{
SerializedObject serializedObject = new SerializedObject(scriptableObject);
serializedObject.Update();
using (var iterator = serializedObject.GetIterator())
{
float yOffset = EditorGUIUtility.singleLineHeight;
if (iterator.NextVisible(true))
{
do
{
SerializedProperty childProperty = serializedObject.FindProperty(iterator.name);
if (childProperty.name.Equals("m_Script", System.StringComparison.Ordinal))
{
continue;
}
bool visible = PropertyUtility.IsVisible(childProperty);
if (!visible)
{
continue;
}
float childHeight = GetPropertyHeight(childProperty);
Rect childRect = new Rect()
{
x = rect.x,
y = rect.y + yOffset,
width = rect.width,
height = childHeight
};
NaughtyEditorGUI.PropertyField(childRect, childProperty, true);
yOffset += childHeight;
}
while (iterator.NextVisible(false));
}
}
serializedObject.ApplyModifiedProperties();
}
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: d1ddb7194615bdc4e8b2088c8d165d8b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,77 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(InputAxisAttribute))]
public class InputAxisPropertyDrawer : PropertyDrawerBase
{
private static readonly string AssetPath = Path.Combine("ProjectSettings", "InputManager.asset");
private const string AxesPropertyPath = "m_Axes";
private const string NamePropertyPath = "m_Name";
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
return (property.propertyType == SerializedPropertyType.String)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
if (property.propertyType == SerializedPropertyType.String)
{
var inputManagerAsset = AssetDatabase.LoadAssetAtPath(AssetPath, typeof(object));
var inputManager = new SerializedObject(inputManagerAsset);
var axesProperty = inputManager.FindProperty(AxesPropertyPath);
var axesSet = new HashSet<string>();
axesSet.Add("(None)");
for (var i = 0; i < axesProperty.arraySize; i++)
{
var axis = axesProperty.GetArrayElementAtIndex(i).FindPropertyRelative(NamePropertyPath).stringValue;
axesSet.Add(axis);
}
var axes = axesSet.ToArray();
string propertyString = property.stringValue;
int index = 0;
// check if there is an entry that matches the entry and get the index
// we skip index 0 as that is a special custom case
for (int i = 1; i < axes.Length; i++)
{
if (axes[i].Equals(propertyString, System.StringComparison.Ordinal))
{
index = i;
break;
}
}
// Draw the popup box with the current selected index
int newIndex = EditorGUI.Popup(rect, label.text, index, axes);
// Adjust the actual string value of the property based on the selection
string newValue = newIndex > 0 ? axes[newIndex] : string.Empty;
if (!property.stringValue.Equals(newValue, System.StringComparison.Ordinal))
{
property.stringValue = newValue;
}
}
else
{
string message = string.Format("{0} supports only string fields", typeof(InputAxisAttribute).Name);
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 0de9d3dfe2d466a458be838edf361645
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/InputAxisPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,88 @@
using UnityEngine;
using UnityEditor;
using System;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(LayerAttribute))]
public class LayerPropertyDrawer : 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()
{
return UnityEditorInternal.InternalEditorUtility.layers;
}
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 = LayerMask.LayerToName(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 = LayerMask.NameToLayer(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);
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 7278ba0893ab7d940b5f944e5b1cf1a7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/LayerPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,108 @@
using UnityEditor;
using UnityEngine;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(MinMaxSliderAttribute))]
public class MinMaxSliderPropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
return (property.propertyType == SerializedPropertyType.Vector2 || property.propertyType == SerializedPropertyType.Vector2Int)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
MinMaxSliderAttribute minMaxSliderAttribute = (MinMaxSliderAttribute)attribute;
if (property.propertyType == SerializedPropertyType.Vector2 || property.propertyType == SerializedPropertyType.Vector2Int)
{
EditorGUI.BeginProperty(rect, label, property);
float indentLength = NaughtyEditorGUI.GetIndentLength(rect);
float labelWidth = EditorGUIUtility.labelWidth + NaughtyEditorGUI.HorizontalSpacing;
float floatFieldWidth = EditorGUIUtility.fieldWidth;
float sliderWidth = rect.width - labelWidth - 2.0f * floatFieldWidth;
float sliderPadding = 5.0f;
Rect labelRect = new Rect(
rect.x,
rect.y,
labelWidth,
rect.height);
Rect sliderRect = new Rect(
rect.x + labelWidth + floatFieldWidth + sliderPadding - indentLength,
rect.y,
sliderWidth - 2.0f * sliderPadding + indentLength,
rect.height);
Rect minFloatFieldRect = new Rect(
rect.x + labelWidth - indentLength,
rect.y,
floatFieldWidth + indentLength,
rect.height);
Rect maxFloatFieldRect = new Rect(
rect.x + labelWidth + floatFieldWidth + sliderWidth - indentLength,
rect.y,
floatFieldWidth + indentLength,
rect.height);
// Draw the label
EditorGUI.LabelField(labelRect, label.text);
// Draw the slider
EditorGUI.BeginChangeCheck();
if (property.propertyType == SerializedPropertyType.Vector2)
{
Vector2 sliderValue = property.vector2Value;
EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue);
sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x);
sliderValue.x = Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y));
sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y);
sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue);
if (EditorGUI.EndChangeCheck())
{
property.vector2Value = sliderValue;
}
}
else if (property.propertyType == SerializedPropertyType.Vector2Int)
{
Vector2Int sliderValue = property.vector2IntValue;
float xValue = sliderValue.x;
float yValue = sliderValue.y;
EditorGUI.MinMaxSlider(sliderRect, ref xValue, ref yValue, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue);
sliderValue.x = EditorGUI.IntField(minFloatFieldRect, (int)xValue);
sliderValue.x = (int)Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y));
sliderValue.y = EditorGUI.IntField(maxFloatFieldRect, (int)yValue);
sliderValue.y = (int)Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue);
if (EditorGUI.EndChangeCheck())
{
property.vector2IntValue = sliderValue;
}
}
EditorGUI.EndProperty();
}
else
{
string message = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 or Vector2Int fields";
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 27011af81554b5b4489b155f09275475
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/MinMaxSliderPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,154 @@
using UnityEngine;
using UnityEditor;
using System.Reflection;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(ProgressBarAttribute))]
public class ProgressBarPropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute<ProgressBarAttribute>(property);
var maxValue = GetMaxValue(property, progressBarAttribute);
return IsNumber(property) && IsNumber(maxValue)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
if (!IsNumber(property))
{
string message = string.Format("Field {0} is not a number", property.name);
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
return;
}
ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute<ProgressBarAttribute>(property);
var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value);
var maxValue = GetMaxValue(property, progressBarAttribute);
if (maxValue != null && IsNumber(maxValue))
{
var fillPercentage = value / CastToFloat(maxValue);
var barLabel = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;
var barColor = progressBarAttribute.Color.GetColor();
var labelColor = Color.white;
var indentLength = NaughtyEditorGUI.GetIndentLength(rect);
Rect barRect = new Rect()
{
x = rect.x + indentLength,
y = rect.y,
width = rect.width - indentLength,
height = EditorGUIUtility.singleLineHeight
};
DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor);
}
else
{
string message = string.Format(
"The provided dynamic max value for the progress bar is not correct. Please check if the '{0}' is correct, or the return type is float/int",
nameof(progressBarAttribute.MaxValueName));
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
{
if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName))
{
return progressBarAttribute.MaxValue;
}
else
{
object target = PropertyUtility.GetTargetObjectWithProperty(property);
FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, progressBarAttribute.MaxValueName);
if (valuesFieldInfo != null)
{
return valuesFieldInfo.GetValue(target);
}
PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, progressBarAttribute.MaxValueName);
if (valuesPropertyInfo != null)
{
return valuesPropertyInfo.GetValue(target);
}
MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, progressBarAttribute.MaxValueName);
if (methodValuesInfo != null &&
(methodValuesInfo.ReturnType == typeof(float) || methodValuesInfo.ReturnType == typeof(int)) &&
methodValuesInfo.GetParameters().Length == 0)
{
return methodValuesInfo.Invoke(target, null);
}
return null;
}
}
private void DrawBar(Rect rect, float fillPercent, string label, Color barColor, Color labelColor)
{
if (Event.current.type != EventType.Repaint)
{
return;
}
var fillRect = new Rect(rect.x, rect.y, rect.width * fillPercent, rect.height);
EditorGUI.DrawRect(rect, new Color(0.13f, 0.13f, 0.13f));
EditorGUI.DrawRect(fillRect, barColor);
// set alignment and cache the default
var align = GUI.skin.label.alignment;
GUI.skin.label.alignment = TextAnchor.UpperCenter;
// set the color and cache the default
var c = GUI.contentColor;
GUI.contentColor = labelColor;
// calculate the position
var labelRect = new Rect(rect.x, rect.y - 2, rect.width, rect.height);
// draw~
EditorGUI.DropShadowLabel(labelRect, label);
// reset color and alignment
GUI.contentColor = c;
GUI.skin.label.alignment = align;
}
private bool IsNumber(SerializedProperty property)
{
bool isNumber = property.propertyType == SerializedPropertyType.Float || property.propertyType == SerializedPropertyType.Integer;
return isNumber;
}
private bool IsNumber(object obj)
{
return (obj is float) || (obj is int);
}
private float CastToFloat(object obj)
{
if (obj is int)
{
return (int)obj;
}
else
{
return (float)obj;
}
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 0bcbc424b10864b4eb6e3bcfb276cdf9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ProgressBarPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,94 @@
using UnityEditor;
using UnityEngine;
namespace NaughtyAttributes.Editor
{
public abstract class PropertyDrawerBase : PropertyDrawer
{
public sealed override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
// Check if visible
bool visible = PropertyUtility.IsVisible(property);
if (!visible)
{
return;
}
// Validate
ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes<ValidatorAttribute>(property);
foreach (var validatorAttribute in validatorAttributes)
{
validatorAttribute.GetValidator().ValidateProperty(property);
}
// Check if enabled and draw
EditorGUI.BeginChangeCheck();
bool enabled = PropertyUtility.IsEnabled(property);
using (new EditorGUI.DisabledScope(disabled: !enabled))
{
OnGUI_Internal(rect, property, PropertyUtility.GetLabel(property));
}
// Call OnValueChanged callbacks
if (EditorGUI.EndChangeCheck())
{
PropertyUtility.CallOnValueChangedCallbacks(property);
}
}
protected abstract void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label);
sealed override public float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
bool visible = PropertyUtility.IsVisible(property);
if (!visible)
{
return 0.0f;
}
return GetPropertyHeight_Internal(property, label);
}
protected virtual float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, includeChildren: true);
}
protected float GetPropertyHeight(SerializedProperty property)
{
SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute<SpecialCaseDrawerAttribute>(property);
if (specialCaseAttribute != null)
{
return specialCaseAttribute.GetDrawer().GetPropertyHeight(property);
}
return EditorGUI.GetPropertyHeight(property, includeChildren: true);
}
public virtual float GetHelpBoxHeight()
{
return EditorGUIUtility.singleLineHeight * 2.0f;
}
public void DrawDefaultPropertyAndHelpBox(Rect rect, SerializedProperty property, string message, MessageType messageType)
{
float indentLength = NaughtyEditorGUI.GetIndentLength(rect);
Rect helpBoxRect = new Rect(
rect.x + indentLength,
rect.y,
rect.width - indentLength,
GetHelpBoxHeight());
NaughtyEditorGUI.HelpBox(helpBoxRect, message, MessageType.Warning, context: property.serializedObject.targetObject);
Rect propertyRect = new Rect(
rect.x,
rect.y + GetHelpBoxHeight(),
rect.width,
GetPropertyHeight(property));
EditorGUI.PropertyField(propertyRect, property, true);
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 574f5fa6033f26342816a8a5f39749e5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/PropertyDrawerBase.cs
uploadId: 480834

View File

@@ -0,0 +1,80 @@
using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;
using System;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(ResizableTextAreaAttribute))]
public class ResizableTextAreaPropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.String)
{
float labelHeight = EditorGUIUtility.singleLineHeight;
float textAreaHeight = GetTextAreaHeight(property.stringValue);
return labelHeight + textAreaHeight;
}
else
{
return GetPropertyHeight(property) + GetHelpBoxHeight();
}
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
if (property.propertyType == SerializedPropertyType.String)
{
Rect labelRect = new Rect()
{
x = rect.x,
y = rect.y,
width = rect.width,
height = EditorGUIUtility.singleLineHeight
};
EditorGUI.LabelField(labelRect, label.text);
EditorGUI.BeginChangeCheck();
Rect textAreaRect = new Rect()
{
x = labelRect.x,
y = labelRect.y + EditorGUIUtility.singleLineHeight,
width = labelRect.width,
height = GetTextAreaHeight(property.stringValue)
};
string textAreaValue = EditorGUI.TextArea(textAreaRect, property.stringValue);
if (EditorGUI.EndChangeCheck())
{
property.stringValue = textAreaValue;
}
}
else
{
string message = typeof(ResizableTextAreaAttribute).Name + " can only be used on string fields";
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
private int GetNumberOfLines(string text)
{
string content = Regex.Replace(text, @"\r\n|\n\r|\r|\n", Environment.NewLine);
string[] lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
return lines.Length;
}
private float GetTextAreaHeight(string text)
{
float height = (EditorGUIUtility.singleLineHeight - 3.0f) * GetNumberOfLines(text) + 3.0f;
return height;
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 6e27ffd9a96b58c46bb74cc93de3e06f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ResizableTextAreaPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,99 @@
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Text.RegularExpressions;
using System;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(SceneAttribute))]
public class ScenePropertyDrawer : PropertyDrawerBase
{
private const string SceneListItem = "{0} ({1})";
private const string ScenePattern = @".+\/(.+)\.unity";
private const string TypeWarningMessage = "{0} must be an int or a string";
private const string BuildSettingsWarningMessage = "No scenes in the build settings";
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
bool validPropertyType = property.propertyType == SerializedPropertyType.String || property.propertyType == SerializedPropertyType.Integer;
bool anySceneInBuildSettings = GetScenes().Length > 0;
return (validPropertyType && anySceneInBuildSettings)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
string[] scenes = GetScenes();
bool anySceneInBuildSettings = scenes.Length > 0;
if (!anySceneInBuildSettings)
{
DrawDefaultPropertyAndHelpBox(rect, property, BuildSettingsWarningMessage, MessageType.Warning);
return;
}
string[] sceneOptions = GetSceneOptions(scenes);
switch (property.propertyType)
{
case SerializedPropertyType.String:
DrawPropertyForString(rect, property, label, scenes, sceneOptions);
break;
case SerializedPropertyType.Integer:
DrawPropertyForInt(rect, property, label, sceneOptions);
break;
default:
string message = string.Format(TypeWarningMessage, property.name);
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
break;
}
EditorGUI.EndProperty();
}
private string[] GetScenes()
{
return EditorBuildSettings.scenes
.Where(scene => scene.enabled)
.Select(scene => Regex.Match(scene.path, ScenePattern).Groups[1].Value)
.ToArray();
}
private string[] GetSceneOptions(string[] scenes)
{
return scenes.Select((s, i) => string.Format(SceneListItem, s, i)).ToArray();
}
private static void DrawPropertyForString(Rect rect, SerializedProperty property, GUIContent label, string[] scenes, string[] sceneOptions)
{
int index = IndexOf(scenes, property.stringValue);
int newIndex = EditorGUI.Popup(rect, label.text, index, sceneOptions);
string newScene = scenes[newIndex];
if (!property.stringValue.Equals(newScene, StringComparison.Ordinal))
{
property.stringValue = scenes[newIndex];
}
}
private static void DrawPropertyForInt(Rect rect, SerializedProperty property, GUIContent label, string[] sceneOptions)
{
int index = property.intValue;
int newIndex = EditorGUI.Popup(rect, label.text, index, sceneOptions);
if (property.intValue != newIndex)
{
property.intValue = newIndex;
}
}
private static int IndexOf(string[] scenes, string scene)
{
var index = Array.IndexOf(scenes, scene);
return Mathf.Clamp(index, 0, scenes.Length - 1);
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 7f5ed440d4f429e42a5da7bc5df48fd8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ScenePropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,110 @@
using UnityEngine;
using UnityEditor;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(ShowAssetPreviewAttribute))]
public class ShowAssetPreviewPropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.ObjectReference)
{
Texture2D previewTexture = GetAssetPreview(property);
if (previewTexture != null)
{
return GetPropertyHeight(property) + GetAssetPreviewSize(property).y;
}
else
{
return GetPropertyHeight(property);
}
}
else
{
return GetPropertyHeight(property) + GetHelpBoxHeight();
}
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
if (property.propertyType == SerializedPropertyType.ObjectReference)
{
Rect propertyRect = new Rect()
{
x = rect.x,
y = rect.y,
width = rect.width,
height = EditorGUIUtility.singleLineHeight
};
EditorGUI.PropertyField(propertyRect, property, label);
Texture2D previewTexture = GetAssetPreview(property);
if (previewTexture != null)
{
Rect previewRect = new Rect()
{
x = rect.x + NaughtyEditorGUI.GetIndentLength(rect),
y = rect.y + EditorGUIUtility.singleLineHeight,
width = rect.width,
height = GetAssetPreviewSize(property).y
};
GUI.Label(previewRect, previewTexture);
}
}
else
{
string message = property.name + " doesn't have an asset preview";
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
private Texture2D GetAssetPreview(SerializedProperty property)
{
if (property.propertyType == SerializedPropertyType.ObjectReference)
{
if (property.objectReferenceValue != null)
{
Texture2D previewTexture = AssetPreview.GetAssetPreview(property.objectReferenceValue);
return previewTexture;
}
return null;
}
return null;
}
private Vector2 GetAssetPreviewSize(SerializedProperty property)
{
Texture2D previewTexture = GetAssetPreview(property);
if (previewTexture == null)
{
return Vector2.zero;
}
else
{
int targetWidth = ShowAssetPreviewAttribute.DefaultWidth;
int targetHeight = ShowAssetPreviewAttribute.DefaultHeight;
ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute<ShowAssetPreviewAttribute>(property);
if (showAssetPreviewAttribute != null)
{
targetWidth = showAssetPreviewAttribute.Width;
targetHeight = showAssetPreviewAttribute.Height;
}
int width = Mathf.Clamp(targetWidth, 0, previewTexture.width);
int height = Mathf.Clamp(targetHeight, 0, previewTexture.height);
return new Vector2(width, height);
}
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 24dee3fc91cfe94438de1e3c158f187f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ShowAssetPreviewPropertyDrawer.cs
uploadId: 480834

View File

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

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: b0ed20ed34d732246a98cde96351fccb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs
uploadId: 480834

View File

@@ -0,0 +1,62 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(TagAttribute))]
public class TagPropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
return (property.propertyType == SerializedPropertyType.String)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
if (property.propertyType == SerializedPropertyType.String)
{
// generate the taglist + custom tags
List<string> tagList = new List<string>();
tagList.Add("(None)");
tagList.Add("Untagged");
tagList.AddRange(UnityEditorInternal.InternalEditorUtility.tags);
string propertyString = property.stringValue;
int index = 0;
// check if there is an entry that matches the entry and get the index
// we skip index 0 as that is a special custom case
for (int i = 1; i < tagList.Count; i++)
{
if (tagList[i].Equals(propertyString, System.StringComparison.Ordinal))
{
index = i;
break;
}
}
// Draw the popup box with the current selected index
int newIndex = EditorGUI.Popup(rect, label.text, index, tagList.ToArray());
// Adjust the actual string value of the property based on the selection
string newValue = newIndex > 0 ? tagList[newIndex] : string.Empty;
if (!property.stringValue.Equals(newValue, System.StringComparison.Ordinal))
{
property.stringValue = newValue;
}
}
else
{
string message = string.Format("{0} supports only string fields", typeof(TagAttribute).Name);
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
EditorGUI.EndProperty();
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 3df4c068c970ab6498df7a60efbde311
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129996
packageName: NaughtyAttributes
packageVersion: 2.1.4
assetPath: Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/TagPropertyDrawer.cs
uploadId: 480834